Search in sources :

Example 16 with JWTVerifier

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);
}
Also used : JWTVerifier(com.auth0.jwt.JWTVerifier) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Test(org.junit.jupiter.api.Test)

Example 17 with JWTVerifier

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;
    }
}
Also used : Algorithm(com.auth0.jwt.algorithms.Algorithm) JWTVerifier(com.auth0.jwt.JWTVerifier) JWTDecodeException(com.auth0.jwt.exceptions.JWTDecodeException)

Example 18 with JWTVerifier

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;
}
Also used : Algorithm(com.auth0.jwt.algorithms.Algorithm) JWTVerifier(com.auth0.jwt.JWTVerifier) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT)

Example 19 with JWTVerifier

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);
    }
}
Also used : JWT(com.auth0.jwt.JWT) FilterChain(javax.servlet.FilterChain) ServletException(javax.servlet.ServletException) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Collection(java.util.Collection) HttpServletResponse(javax.servlet.http.HttpServletResponse) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) OncePerRequestFilter(org.springframework.web.filter.OncePerRequestFilter) IOException(java.io.IOException) ArrayList(java.util.ArrayList) GrantedAuthority(org.springframework.security.core.GrantedAuthority) List(java.util.List) HttpServletRequest(javax.servlet.http.HttpServletRequest) JWTVerifier(com.auth0.jwt.JWTVerifier) Algorithm(com.auth0.jwt.algorithms.Algorithm) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) JWTVerifier(com.auth0.jwt.JWTVerifier) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT)

Example 20 with JWTVerifier

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();
}
Also used : JWTVerifier(com.auth0.jwt.JWTVerifier) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT)

Aggregations

JWTVerifier (com.auth0.jwt.JWTVerifier)115 Algorithm (com.auth0.jwt.algorithms.Algorithm)104 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)100 Test (org.junit.Test)42 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)30 IOException (java.io.IOException)23 JWTVerifier (com.auth0.jwt.interfaces.JWTVerifier)18 RSAPublicKey (java.security.interfaces.RSAPublicKey)15 JWTDecodeException (com.auth0.jwt.exceptions.JWTDecodeException)14 Claim (com.auth0.jwt.interfaces.Claim)10 Date (java.util.Date)9 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 HashMap (java.util.HashMap)8 ECKey (java.security.interfaces.ECKey)7 ServletException (javax.servlet.ServletException)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 TokenExpiredException (com.auth0.jwt.exceptions.TokenExpiredException)5 RSAKeyProvider (com.auth0.jwt.interfaces.RSAKeyProvider)5 URL (java.net.URL)5 KeyFactory (java.security.KeyFactory)5