Search in sources :

Example 56 with Algorithm

use of com.auth0.jwt.Algorithm in project code by lastwhispers.

the class UserController method login.

/**
 * http://localhost:8080/user/login?username=admin&password=123456
 *
 * @param username
 * @param password
 * @return
 */
@GetMapping("/login")
public String login(@RequestParam String username, @RequestParam String password) {
    Algorithm algorithm = Algorithm.HMAC256(JWT_KEY);
    String token = JWT.create().withClaim(CURRENT_USER, username).withClaim(UID, 1).withExpiresAt(new Date(System.currentTimeMillis() + 3600000)).sign(algorithm);
    return token;
}
Also used : Algorithm(com.auth0.jwt.algorithms.Algorithm) Date(java.util.Date)

Example 57 with Algorithm

use of com.auth0.jwt.Algorithm in project jobs by damingerdai.

the class JWTUtil method sign.

/**
 * 生成签名,5min后过期
 * @param username 用户名
 * @param secret 用户的密码
 * @return 加密的token
 */
public static String sign(String username, String secret) {
    Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
    Algorithm algorithm = Algorithm.HMAC256(secret);
    // 附带username信息
    return JWT.create().withClaim("username", username).withExpiresAt(date).sign(algorithm);
}
Also used : Algorithm(com.auth0.jwt.algorithms.Algorithm) Date(java.util.Date)

Example 58 with Algorithm

use of com.auth0.jwt.Algorithm in project jobs by damingerdai.

the class JwtTool method verifyToken.

public static boolean verifyToken(String token, String secret) {
    try {
        Algorithm algorithm = Algorithm.HMAC256(secret);
        var verifier = JWT.require(algorithm).build();
        var jwt = verifier.verify(token);
        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
        return false;
    }
}
Also used : Algorithm(com.auth0.jwt.algorithms.Algorithm)

Example 59 with Algorithm

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

Example 60 with Algorithm

use of com.auth0.jwt.Algorithm in project bitrxc-server by bitrxc.

the class TokenManager method createTokenForAdmin.

// 管理员端token生成
public String createTokenForAdmin(String phone) {
    Date expirationTime = new Date(System.currentTimeMillis() + this.tokenExpiration);
    // 私钥及加密算法
    Algorithm algorithm = Algorithm.HMAC256(tokenSignKey);
    // 设置头信息
    Map<String, Object> header = new HashMap<>();
    header.put("typ", "JWT");
    header.put("alg", "HS256");
    return JWT.create().withHeader(header).withExpiresAt(expirationTime).withClaim("phone", phone).sign(algorithm);
}
Also used : HashMap(java.util.HashMap) Algorithm(com.auth0.jwt.algorithms.Algorithm) Date(java.util.Date)

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