use of com.auth0.jwt.JWTVerifier in project spring-learning by moon-zhou.
the class JWTTest method testJWTVerify.
/**
* 验证JWT生成的token
*
* 为了方便测试,定义了类变量,整体用例可直接执行
*/
@Test
public void testJWTVerify() {
final JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(SIGN)).build();
final DecodedJWT decodedJWT = jwtVerifier.verify(token);
int decodeUserId = decodedJWT.getClaim(USER_ID).asInt();
String decodeUserName = decodedJWT.getClaim(USER_NAME).asString();
System.out.println("用户Id:" + decodeUserId);
System.out.println("用户名:" + decodeUserName);
System.out.println("过期时间:" + decodedJWT.getExpiresAt());
Assertions.assertEquals(userId, decodeUserId);
Assertions.assertEquals(userName, decodeUserName);
}
use of com.auth0.jwt.JWTVerifier in project waynboot-mall by wayn111.
the class JwtUtil method verify.
/**
* 校验token是否正确
*
* @param token 密钥
* @param secret 用户的密码
* @return 是否正确
*/
public static boolean verify(String token, String userId, String secret) {
try {
Algorithm algorithm = Algorithm.HMAC256(secret);
JWTVerifier verifier = JWT.require(algorithm).withClaim("userId", userId).build();
verifier.verify(token);
return true;
} catch (Exception exception) {
return false;
}
}
use of com.auth0.jwt.JWTVerifier in project seckill by yt-King.
the class JWTUtils method verify.
/**
* 校验token是否正确
* @param token 密钥
* @param password 用户的密码
* @return 是否正确
*/
public static boolean verify(String token, String username, String password) {
Algorithm algorithm = Algorithm.HMAC256(password);
JWTVerifier verifier = JWT.require(algorithm).withClaim("username", username).build();
DecodedJWT jwt = verifier.verify(token);
return true;
}
use of com.auth0.jwt.JWTVerifier in project Blockchain_LSImmo3.0_Backend by medsaad2000.
the class JWTAuthorizationFilter method doFilterInternal.
// pour chaque requete envoyée par user cette methode va executée en premier
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Headers", "Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, authorization");
response.addHeader("Access-Control-Expose-Headers", "Access-Control-Allow-Origin, Access-Control-Allow-Credentials, authorization");
response.addHeader("Access-Control-Allow-Methods", "GET,POST,PUT,PATCH,DELETE,");
if (request.getMethod().equals("OPTIONS")) {
response.setStatus(HttpServletResponse.SC_OK);
} else if (request.getRequestURI().equals("/login")) {
filterChain.doFilter(request, response);
return;
} else // ------ PUT, GET, POST ... requests ------
{
String jwtToken = request.getHeader(SecurityParams.JWT_HEADER_NAME);
if (jwtToken == null || !jwtToken.startsWith(SecurityParams.HEADER_PREFIX)) {
filterChain.doFilter(request, response);
return;
}
// ----- JWT decode ------- ----
// ----------- sign JWT ----------
JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SecurityParams.SECRET)).build();
// ---- remove prefix---------
String jwt = jwtToken.substring(SecurityParams.HEADER_PREFIX.length());
DecodedJWT decodeJWT = verifier.verify(jwt);
// ----- get username --------
String username = decodeJWT.getSubject();
// ------ get roles -------------
List<String> roles = decodeJWT.getClaims().get("roles").asList(String.class);
// ------ convert roles into grantedAuthorities -------
Collection<GrantedAuthority> authorities = new ArrayList<>();
roles.forEach(rn -> {
authorities.add(new SimpleGrantedAuthority(rn));
});
// ---------- user authentication ----------
UsernamePasswordAuthenticationToken user = new UsernamePasswordAuthenticationToken(username, null, authorities);
SecurityContextHolder.getContext().setAuthentication(user);
filterChain.doFilter(request, response);
}
}
use of com.auth0.jwt.JWTVerifier in project sports_hub_portal by Anastasiia-Rokytska.
the class JwtTokenService method verifyToken.
public String verifyToken(String token) {
JWTVerifier verifier = JWT.require(algorithm).build();
DecodedJWT decodedJWT = verifier.verify(token);
return decodedJWT.getSubject();
}
Aggregations