27. JWT Token Provider - Part 2 (#2)
This commit is contained in:
@ -1,11 +1,18 @@
|
|||||||
package net.shyshkin.study.fullstack.supportportal.backend.utility;
|
package net.shyshkin.study.fullstack.supportportal.backend.utility;
|
||||||
|
|
||||||
import com.auth0.jwt.JWT;
|
import com.auth0.jwt.JWT;
|
||||||
|
import com.auth0.jwt.JWTVerifier;
|
||||||
import com.auth0.jwt.algorithms.Algorithm;
|
import com.auth0.jwt.algorithms.Algorithm;
|
||||||
|
import com.auth0.jwt.exceptions.JWTVerificationException;
|
||||||
import net.shyshkin.study.fullstack.supportportal.backend.domain.UserPrincipal;
|
import net.shyshkin.study.fullstack.supportportal.backend.domain.UserPrincipal;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static net.shyshkin.study.fullstack.supportportal.backend.constant.SecurityConstants.*;
|
import static net.shyshkin.study.fullstack.supportportal.backend.constant.SecurityConstants.*;
|
||||||
|
|
||||||
@ -26,7 +33,36 @@ public class JwtTokenProvider {
|
|||||||
.sign(Algorithm.HMAC512(secret));
|
.sign(Algorithm.HMAC512(secret));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<GrantedAuthority> getAuthorities(String token) {
|
||||||
|
String[] claims = getClaimsFromToken(token);
|
||||||
|
return Arrays.stream(claims)
|
||||||
|
.map(SimpleGrantedAuthority::new)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
private String[] getClaimsFromToken(String token) {
|
||||||
|
JWTVerifier verifier = getJwtVerifier();
|
||||||
|
try {
|
||||||
|
return verifier.verify(token)
|
||||||
|
.getClaim(AUTHORITIES)
|
||||||
|
.asArray(String.class);
|
||||||
|
} catch (JWTVerificationException exception) {
|
||||||
|
throw new JWTVerificationException(ACCESS_DENIED_MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
private String[] getClaimsFromUser(UserPrincipal userPrincipal) {
|
||||||
return new String[0];
|
return userPrincipal.getAuthorities()
|
||||||
|
.stream()
|
||||||
|
.map(GrantedAuthority::getAuthority)
|
||||||
|
.toArray(String[]::new);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user