Search in sources :

Example 21 with JWTVerifier

use of com.auth0.jwt.JWTVerifier in project micro-service-examples by jetlinks.

the class JwtAuthSupplier method preHandle.

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    String token = request.getHeader(HttpHeaders.AUTHORIZATION);
    if (StringUtils.isEmpty(token) || !token.startsWith("jwt")) {
        return true;
    }
    JWTVerifier verifier = jwt.createVerifier();
    DecodedJWT jwt = verifier.verify(token.substring(4));
    ContextUtils.currentContext().put(Authentication.class, factory.create().json(jwt.getSubject()).build());
    return true;
}
Also used : JWTVerifier(com.auth0.jwt.interfaces.JWTVerifier) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT)

Example 22 with JWTVerifier

use of com.auth0.jwt.JWTVerifier in project kin-rsocket-broker by huangjianqin.

the class JwtAuthenticationService method auth.

@Override
public RSocketAppPrincipal auth(String credentials) {
    if (Objects.isNull(credentials) || credentials.isEmpty()) {
        return null;
    }
    // bearer jwt_token
    credentials = credentials.substring(credentials.lastIndexOf(" ") + 1);
    int tokenHashCode = credentials.hashCode();
    RSocketAppPrincipal principal = jwtVerifyCache.getIfPresent(tokenHashCode);
    for (JWTVerifier verifier : verifiers) {
        try {
            principal = new JwtPrincipal(verifier.verify(credentials));
            jwtVerifyCache.put(tokenHashCode, principal);
            break;
        } catch (JWTVerificationException ignore) {
        // do nothing
        }
    }
    return principal;
}
Also used : JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) RSocketAppPrincipal(org.kin.rsocket.auth.RSocketAppPrincipal) JWTVerifier(com.auth0.jwt.interfaces.JWTVerifier)

Example 23 with JWTVerifier

use of com.auth0.jwt.JWTVerifier in project study by bage2014.

the class JWTTest method main.

public static void main(String[] args) throws IllegalArgumentException, UnsupportedEncodingException {
    // jjwt();
    String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
    try {
        Algorithm algorithm = Algorithm.HMAC256("your-256-bit-secret");
        // Algorithm algorithm = Algorithm.HMAC256("your-256-bit-secret".getBytes("UTF-8"));
        JWTVerifier verifier = JWT.require(algorithm).build();
        DecodedJWT jwt = verifier.verify(token);
        System.out.println(jwt);
    } catch (JWTVerificationException exception) {
        // Invalid signature/claims
        exception.printStackTrace();
    }
}
Also used : JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) Algorithm(com.auth0.jwt.algorithms.Algorithm) SignatureAlgorithm(io.jsonwebtoken.SignatureAlgorithm) JWTVerifier(com.auth0.jwt.JWTVerifier) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT)

Example 24 with JWTVerifier

use of com.auth0.jwt.JWTVerifier in project doorkeeper by limbo-world.

the class JwtTest method testOther.

@Test(expected = SignatureVerificationException.class)
public void testOther() {
    String token = JWT.create().withExpiresAt(// 设置过期时间
    DateUtils.addHours(new Date(), 1)).withSubject("sss").withAudience(// 设置接受方信息,一般时登录用户
    "user1").sign(Algorithm.HMAC256("2222"));
    String userId = JWT.decode(token).getAudience().get(0);
    JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256("111111")).build();
    DecodedJWT verify = jwtVerifier.verify(token);
    System.out.println(JacksonUtil.toJSONString(verify));
}
Also used : JWTVerifier(com.auth0.jwt.interfaces.JWTVerifier) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Date(java.util.Date) Test(org.junit.Test)

Example 25 with JWTVerifier

use of com.auth0.jwt.JWTVerifier in project doorkeeper by limbo-world.

the class JWTUtil method verifyToken.

public static boolean verifyToken(String token, String secret) {
    JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(secret)).build();
    jwtVerifier.verify(token);
    return true;
}
Also used : JWTVerifier(com.auth0.jwt.interfaces.JWTVerifier)

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