From 8854c80bcc9ef8e3b58f00865e1ba4239b8cda02 Mon Sep 17 00:00:00 2001 From: Art Date: Sun, 5 Sep 2021 09:51:41 +0300 Subject: [PATCH] 28. JWT Token Provider - Part 3 (#2) --- support-portal-backend/pom.xml | 8 ++++ .../backend/config/JwtConfig.java | 25 +++++++++++ .../backend/utility/JwtTokenProvider.java | 42 ++++++++++++++----- 3 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/config/JwtConfig.java diff --git a/support-portal-backend/pom.xml b/support-portal-backend/pom.xml index 0efe824..d1d53c3 100644 --- a/support-portal-backend/pom.xml +++ b/support-portal-backend/pom.xml @@ -53,6 +53,13 @@ lombok true + + + org.apache.commons + commons-lang3 + 3.12.0 + + org.springframework.boot spring-boot-starter-test @@ -68,6 +75,7 @@ com.github.javafaker javafaker 1.0.2 + test diff --git a/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/config/JwtConfig.java b/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/config/JwtConfig.java new file mode 100644 index 0000000..d097e6f --- /dev/null +++ b/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/config/JwtConfig.java @@ -0,0 +1,25 @@ +package net.shyshkin.study.fullstack.supportportal.backend.config; + +import com.auth0.jwt.JWT; +import com.auth0.jwt.JWTVerifier; +import com.auth0.jwt.algorithms.Algorithm; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import static net.shyshkin.study.fullstack.supportportal.backend.constant.SecurityConstants.GET_ARRAYS_LLC; + +@Configuration +public class JwtConfig { + + @Bean + public JWTVerifier jwtVerifier(@Value("${app.jwt.secret}") String secret) { + + Algorithm algorithm = Algorithm.HMAC256(secret); + return JWT.require(algorithm) + .withIssuer(GET_ARRAYS_LLC) + .build(); //Reusable verifier instance + } + + +} diff --git a/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/utility/JwtTokenProvider.java b/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/utility/JwtTokenProvider.java index d587c7a..99db12e 100644 --- a/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/utility/JwtTokenProvider.java +++ b/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/utility/JwtTokenProvider.java @@ -4,11 +4,18 @@ import com.auth0.jwt.JWT; import com.auth0.jwt.JWTVerifier; import com.auth0.jwt.algorithms.Algorithm; import com.auth0.jwt.exceptions.JWTVerificationException; +import lombok.RequiredArgsConstructor; import net.shyshkin.study.fullstack.supportportal.backend.domain.UserPrincipal; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; +import org.springframework.stereotype.Service; +import javax.servlet.http.HttpServletRequest; import java.util.Arrays; import java.util.Date; import java.util.List; @@ -16,8 +23,12 @@ import java.util.stream.Collectors; import static net.shyshkin.study.fullstack.supportportal.backend.constant.SecurityConstants.*; +@Service +@RequiredArgsConstructor public class JwtTokenProvider { + private final JWTVerifier jwtVerifier; + @Value("${app.jwt.secret}") private String secret; @@ -40,10 +51,29 @@ public class JwtTokenProvider { .collect(Collectors.toList()); } + public Authentication getAuthentication(String username, List authorities, HttpServletRequest request) { + var userPassAuthToken = new UsernamePasswordAuthenticationToken(username, null, authorities); + userPassAuthToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); + return userPassAuthToken; + } + + public boolean isTokenValid(String username, String token) { + return StringUtils.isNotBlank(username) && !isTokenExpired(token); + } + + public String getSubject(String token) { + return jwtVerifier.verify(token).getSubject(); + } + + private boolean isTokenExpired(String token) { + Date expirationDate = jwtVerifier.verify(token).getExpiresAt(); + return expirationDate.before(new Date()); + } + private String[] getClaimsFromToken(String token) { - JWTVerifier verifier = getJwtVerifier(); + try { - return verifier.verify(token) + return jwtVerifier.verify(token) .getClaim(AUTHORITIES) .asArray(String.class); } catch (JWTVerificationException exception) { @@ -51,14 +81,6 @@ public class JwtTokenProvider { } } - private JWTVerifier getJwtVerifier() { - - Algorithm algorithm = Algorithm.HMAC256(secret); - return JWT.require(algorithm) - .withIssuer(GET_ARRAYS_LLC) - .build(); //Reusable verifier instance - } - private String[] getClaimsFromUser(UserPrincipal userPrincipal) { return userPrincipal.getAuthorities() .stream()