| | |
| | | |
| | | import com.auth0.jwt.JWT; |
| | | import com.auth0.jwt.JWTCreator; |
| | | import com.auth0.jwt.JWTVerifier; |
| | | import com.auth0.jwt.algorithms.Algorithm; |
| | | import com.auth0.jwt.exceptions.TokenExpiredException; |
| | | import com.auth0.jwt.interfaces.DecodedJWT; |
| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import java.util.Date; |
| | | import java.util.Map; |
| | | import java.util.Optional; |
| | | import java.util.*; |
| | | |
| | | @Slf4j |
| | | @Component |
| | |
| | | |
| | | private static String secret; |
| | | |
| | | private static Algorithm algorithm; |
| | | public static Algorithm algorithm; |
| | | |
| | | @Value("${jwt.issuer: DJI}") |
| | | private void setIssuer(String issuer) { |
| | |
| | | JwtUtil.algorithm = Algorithm.HMAC256(secret); |
| | | } |
| | | |
| | | private JwtUtil() { |
| | | |
| | | } |
| | | |
| | | /** |
| | | * Create a token based on custom information. |
| | | * @param claims custom information |
| | | * @return token |
| | | */ |
| | | public static String createToken(Map<String, String> claims) { |
| | | public static String createToken(Map<String, ?> claims) { |
| | | return JwtUtil.createToken(claims, age, algorithm, subject, issuer); |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param claims |
| | | * @param age unit: s |
| | | * @param algorithm |
| | | * @param subject |
| | | * @param issuer |
| | | * @return |
| | | */ |
| | | public static String createToken(Map<String, ?> claims, Long age, Algorithm algorithm, String subject, String issuer) { |
| | | if (Objects.isNull(algorithm)) { |
| | | throw new IllegalArgumentException(); |
| | | } |
| | | |
| | | Date now = new Date(); |
| | | JWTCreator.Builder builder = JWT.create(); |
| | | // Add custom information to the token's payload segment. |
| | | claims.forEach(builder::withClaim); |
| | | String token = builder.withIssuer(issuer) |
| | | .withSubject(subject) |
| | | claims.forEach((k, v) -> { |
| | | if (Objects.nonNull(v.getClass().getClassLoader())) { |
| | | log.error("claim can't be set to a custom object."); |
| | | return; |
| | | } |
| | | if (v instanceof Map) { |
| | | builder.withClaim(k, (Map) v); |
| | | } else if (v instanceof List) { |
| | | builder.withClaim(k, (List) v); |
| | | } else { |
| | | builder.withClaim(k, String.valueOf(v)); |
| | | } |
| | | }); |
| | | |
| | | if (StringUtils.hasText(subject)) { |
| | | builder.withSubject(subject); |
| | | } |
| | | |
| | | if (StringUtils.hasText(issuer)) { |
| | | builder.withIssuer(issuer); |
| | | } |
| | | |
| | | if (Objects.nonNull(age)) { |
| | | builder.withExpiresAt(new Date(now.getTime() + age * 1000)); |
| | | } |
| | | |
| | | String token = builder |
| | | .withIssuedAt(now) |
| | | .withExpiresAt(new Date(now.getTime() + age)) |
| | | .withNotBefore(now) |
| | | .sign(algorithm); |
| | | log.debug("token created. " + token); |
| | |
| | | * @throws TokenExpiredException |
| | | */ |
| | | public static DecodedJWT verifyToken(String token) { |
| | | try { |
| | | JWTVerifier verifier = JWT.require(algorithm).build(); |
| | | return verifier.verify(token); |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage()); |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | return JWT.require(algorithm).build().verify(token); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return custom claim |
| | | */ |
| | | public static Optional<CustomClaim> parseToken(String token) { |
| | | DecodedJWT jwt = verifyToken(token); |
| | | return jwt == null ? Optional.empty() : Optional.of(new CustomClaim(jwt.getClaims())); |
| | | DecodedJWT jwt; |
| | | try { |
| | | jwt = verifyToken(token); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return Optional.empty(); |
| | | } |
| | | return Optional.of(new CustomClaim(jwt.getClaims())); |
| | | } |
| | | } |