package com.boot.utils;import com.boot.constant.TokenConstant;import io.jsonwebtoken.Claims;import io.jsonwebtoken.Jwts;import io.jsonwebtoken.SignatureAlgorithm;import lombok.extern.slf4j.Slf4j;import java.util.Date;import java.util.Map;import java.util.UUID;import java.util.concurrent.ConcurrentHashMap;@Slf4jpublic class JwtUtil {private static final String ACCESS_SECRET="security-jwt-accesstoken";private static final String REFRESH_SECRET="security-jwt-refreshtoken";private static final Long ACCESS_TOKEN_EXPIRATION=60 *1000 * 60L;private static final Long REFRESH_TOKEN_EXPIRATION=60 *1000 * 180L;public static String getUUID(){String token = UUID.randomUUID().toString().replaceAll("-", "");return token;}public static Map<String,String> createAccessTokenAndRefreshToken(String subject){Map<String,String> tokenMap=new ConcurrentHashMap<>();String accessToken = createAccessToken(subject);String refreshToken = createRefreshToken(subject);tokenMap.put(TokenConstant.ACCESSTOKEN,accessToken);tokenMap.put(TokenConstant.REFRESHTOKEN,refreshToken);return tokenMap;}public static String createAccessToken(String subject){long nowMillis = System.currentTimeMillis();Date now = new Date(nowMillis);long expMillis = nowMillis + ACCESS_TOKEN_EXPIRATION; return Jwts.builder().setId(getUUID()).setSubject(subject) .setIssuer("youzhengjie") .setIssuedAt(now).setExpiration(new Date(expMillis)).signWith(SignatureAlgorithm.HS256,ACCESS_SECRET).compact();}public static Claims parseAccessToken(String accessToken) {Claims claims = null;try {claims = Jwts.parser().setSigningKey(ACCESS_SECRET) .parseClaimsJws(accessToken) .getBody();} catch (Exception ex) {ex.printStackTrace();}return claims;}public static String createRefreshToken(String subject){long nowMillis = System.currentTimeMillis();Date now = new Date(nowMillis);long expMillis = nowMillis + REFRESH_TOKEN_EXPIRATION; return Jwts.builder().setId(getUUID()).setSubject(subject) .setIssuer("youzhengjie") .setIssuedAt(now).setExpiration(new Date(expMillis)).signWith(SignatureAlgorithm.HS256,REFRESH_SECRET).compact();}public static Claims parseRefreshToken(String refreshToken) {Claims claims = null;try {claims = Jwts.parser().setSigningKey(REFRESH_SECRET).parseClaimsJws(refreshToken).getBody();} catch (Exception ex) {ex.printStackTrace();}return claims;}public static boolean canRefresh(String refreshToken){Claims claims = parseRefreshToken(refreshToken);return !isRefreshTokenExpired(claims);} public static Map<String, String> toRefreshToken(String refreshToken){if(!canRefresh(refreshToken)){return null;}Claims claims = parseRefreshToken(refreshToken);String subject = claims.getSubject();return createAccessTokenAndRefreshToken(subject);} public static boolean isAccessTokenExpired(Claims claims) {Date expiredDate = claims.getExpiration();return expiredDate.before(new Date()); }public static boolean isRefreshTokenExpired(Claims claims) {Date expiredDate = claims.getExpiration();return expiredDate.before(new Date()); }public static void main(String[] args) {String userid="123123999777";Map<String, String> tokenMap = createAccessTokenAndRefreshToken(userid);String accesstoken = tokenMap.get(TokenConstant.ACCESSTOKEN);String refreshtoken = tokenMap.get(TokenConstant.REFRESHTOKEN);log.warn(accesstoken);log.warn(refreshtoken);Claims c1 = parseAccessToken(accesstoken);Claims c2 = parseRefreshToken(refreshtoken);String s1 = c1.getSubject();System.out.println(s1);String s2 = c2.getSubject();System.out.println(s2);Map<String, String> refreshTokenMap = toRefreshToken(refreshtoken);String accesstoken2 = refreshTokenMap.get(TokenConstant.ACCESSTOKEN);String refreshtoken2 = refreshTokenMap.get(TokenConstant.REFRESHTOKEN);log.warn("------------------");log.warn(accesstoken2);log.warn(refreshtoken2);Claims claims1 = parseAccessToken(accesstoken2);Claims claims2 = parseRefreshToken(refreshtoken2);System.out.println(claims1.getSubject());System.out.println(claims2.getSubject());}}