Search in sources :

Example 61 with Algorithm

use of com.auth0.jwt.Algorithm in project tutorials by jhkim105.

the class AuthenticationTokenUtil method generateToken.

public String generateToken(AuthUser authUser) {
    Date now = new Date();
    JWTCreator.Builder builder = JWT.create().withClaim(CLAIM_USER_ID, authUser.getUserId()).withClaim(CLAIM_USERNAME, authUser.getUsername()).withClaim(CLAIM_SCOPES, authUser.getScopes()).withIssuedAt(now);
    if (this.expiry != 0)
        builder.withExpiresAt(DateUtils.addSeconds(now, expiry));
    String token = builder.sign(algorithm);
    log.debug("token:{}", token);
    return token;
}
Also used : JWTCreator(com.auth0.jwt.JWTCreator) ToString(lombok.ToString) Date(java.util.Date)

Example 62 with Algorithm

use of com.auth0.jwt.Algorithm in project tutorials by jhkim105.

the class JwtAuthenticationTokenService method generateToken.

public String generateToken(AuthUser authUser, Date expireDate) {
    Date now = new Date();
    JWTCreator.Builder builder = JWT.create().withClaim("id", authUser.getId()).withClaim("username", authUser.getUsername()).withClaim("authority", authUser.getAuthority()).withIssuedAt(now);
    if (expireDate != null)
        builder.withExpiresAt(expireDate);
    String token = builder.sign(algorithm);
    log.debug("token:{}", token);
    return token;
}
Also used : JWTCreator(com.auth0.jwt.JWTCreator) Date(java.util.Date)

Example 63 with Algorithm

use of com.auth0.jwt.Algorithm 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 64 with Algorithm

use of com.auth0.jwt.Algorithm in project su-sunday-cloud by illeagalName.

the class SecurityUtils method createToken.

/**
 * 创建token
 *
 * @param payload 负载信息
 * @param secret  加密串
 * @param date    过期时间
 * @return String
 */
public static String createToken(Map<String, String> payload, String secret, Date date) {
    try {
        // 秘钥及加密算法
        Algorithm algorithm = Algorithm.HMAC256(secret);
        JWTCreator.Builder builder = JWT.create();
        if (DataUtils.isNotEmpty(payload)) {
            payload.forEach(builder::withClaim);
        }
        // 携带userId,userName信息,生成签名
        return builder.withHeader(HEADER).withIssuedAt(new Date()).withExpiresAt(date).withSubject("vip").withIssuer("sunday").sign(algorithm);
    } catch (Exception e) {
        log.error("生成token异常", e);
        return null;
    }
}
Also used : JWTCreator(com.auth0.jwt.JWTCreator) Algorithm(com.auth0.jwt.algorithms.Algorithm) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 65 with Algorithm

use of com.auth0.jwt.Algorithm in project stream-java by GetStream.

the class Auth method buildFrontendToken.

public static Token buildFrontendToken(String secret, String userID, Date expiresAt) {
    final Algorithm algorithm = Algorithm.HMAC256(secret);
    JWTCreator.Builder builder = JWT.create().withClaim("user_id", userID);
    if (expiresAt != null) {
        builder = builder.withExpiresAt(expiresAt);
    }
    return new Token(builder.sign(algorithm));
}
Also used : JWTCreator(com.auth0.jwt.JWTCreator) Token(io.getstream.core.http.Token) Algorithm(com.auth0.jwt.algorithms.Algorithm)

Aggregations

Algorithm (com.auth0.jwt.algorithms.Algorithm)206 Test (org.junit.Test)160 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)90 JWTVerifier (com.auth0.jwt.JWTVerifier)79 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)79 ECDSAAlgorithmTest (com.auth0.jwt.algorithms.ECDSAAlgorithmTest)61 Date (java.util.Date)57 ECDSAKeyProvider (com.auth0.jwt.interfaces.ECDSAKeyProvider)51 RSAPublicKey (java.security.interfaces.RSAPublicKey)36 ECPublicKey (java.security.interfaces.ECPublicKey)34 RSAKeyProvider (com.auth0.jwt.interfaces.RSAKeyProvider)31 IOException (java.io.IOException)30 JWTCreator (com.auth0.jwt.JWTCreator)28 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)25 ECPrivateKey (java.security.interfaces.ECPrivateKey)23 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)21 HashMap (java.util.HashMap)17 UnsupportedEncodingException (java.io.UnsupportedEncodingException)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 JsonObject (com.google.gson.JsonObject)15