Search in sources :

Example 91 with Token

use of com.auth0.json.mgmt.Token in project seckill by yt-King.

the class JWTUtils method isExpire.

/**
 * 判断过期
 * @param token
 * @return
 */
public static boolean isExpire(String token) {
    DecodedJWT jwt = null;
    jwt = JWT.decode(token);
    return System.currentTimeMillis() > jwt.getExpiresAt().getTime();
}
Also used : DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT)

Example 92 with Token

use of com.auth0.json.mgmt.Token in project iesi by metadew.

the class JwtService method generateAuthenticationResponse.

public AuthenticationResponse generateAuthenticationResponse(Authentication authentication) {
    Algorithm algorithm = Algorithm.HMAC256(secret);
    LocalDateTime now = LocalDateTime.now(clock);
    LocalDateTime expiresAt = now.plus(accessTokenExpiryDate, ChronoUnit.SECONDS);
    String token = JWT.create().withIssuer(ISSUER).withSubject(authentication.getName()).withIssuedAt(Timestamp.valueOf(now)).withExpiresAt(Timestamp.valueOf(expiresAt)).withClaim("uuid", ((IesiUserDetails) authentication.getPrincipal()).getId().toString()).sign(algorithm);
    UserDto userDto = userService.get(((IesiUserDetails) authentication.getPrincipal()).getId()).orElseThrow(() -> new UsernameNotFoundException(String.format("Cannot find user %s (%s)", ((IesiUserDetails) authentication.getPrincipal()).getId().toString(), ((IesiUserDetails) authentication.getPrincipal()).getUsername())));
    return new AuthenticationResponse(token, ChronoUnit.SECONDS.between(now, expiresAt), userDto.getRoles());
}
Also used : LocalDateTime(java.time.LocalDateTime) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDto(io.metadew.iesi.server.rest.user.UserDto) IesiUserDetails(io.metadew.iesi.server.rest.configuration.security.IesiUserDetails) Algorithm(com.auth0.jwt.algorithms.Algorithm) AuthenticationResponse(io.metadew.iesi.server.rest.user.AuthenticationResponse)

Example 93 with Token

use of com.auth0.json.mgmt.Token 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 94 with Token

use of com.auth0.json.mgmt.Token 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 95 with Token

use of com.auth0.json.mgmt.Token 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)

Aggregations

DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)276 Algorithm (com.auth0.jwt.algorithms.Algorithm)147 Test (org.junit.Test)120 JWTVerifier (com.auth0.jwt.JWTVerifier)97 Date (java.util.Date)78 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)62 IOException (java.io.IOException)59 Claim (com.auth0.jwt.interfaces.Claim)49 HashMap (java.util.HashMap)40 VoidRequest (com.auth0.net.VoidRequest)31 RSAPublicKey (java.security.interfaces.RSAPublicKey)31 Test (org.junit.jupiter.api.Test)30 JWTDecodeException (com.auth0.jwt.exceptions.JWTDecodeException)28 JWTCreator (com.auth0.jwt.JWTCreator)21 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)21 JWT (com.auth0.jwt.JWT)20 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)19 UnsupportedEncodingException (java.io.UnsupportedEncodingException)18 Instant (java.time.Instant)18 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)17