|
|
@ -25,12 +25,10 @@ import java.util.stream.Collectors;
|
|
|
|
@Component
|
|
|
|
@Component
|
|
|
|
@Slf4j
|
|
|
|
@Slf4j
|
|
|
|
public class JwtTokenUtil {
|
|
|
|
public class JwtTokenUtil {
|
|
|
|
private static final String CLAIM_KEY_USERNAME = "sub";
|
|
|
|
private static final long NEVER_EXPIRE = 315360000; // 没有永不过期的api: 让时钟偏移十年
|
|
|
|
private static final String CLAIM_KEY_CREATED = "created";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${jwt.secret}")
|
|
|
|
@Value("${jwt.secret}")
|
|
|
|
private String secret;
|
|
|
|
private String secret;
|
|
|
|
private Integer expiration = 30;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 生成token
|
|
|
|
* 生成token
|
|
|
@ -40,27 +38,19 @@ public class JwtTokenUtil {
|
|
|
|
@Deprecated
|
|
|
|
@Deprecated
|
|
|
|
public String generateToken(UserDetails userDetails){
|
|
|
|
public String generateToken(UserDetails userDetails){
|
|
|
|
Map<String, Object> claims = new HashMap<>();
|
|
|
|
Map<String, Object> claims = new HashMap<>();
|
|
|
|
claims.put(CLAIM_KEY_USERNAME,userDetails.getUsername());
|
|
|
|
// 添加 payload
|
|
|
|
claims.put(CLAIM_KEY_CREATED,new Date());
|
|
|
|
//claims.put(CLAIM_KEY_USERNAME,userDetails.getUsername());
|
|
|
|
|
|
|
|
//claims.put(CLAIM_KEY_CREATED,new Date());
|
|
|
|
return generateToken(claims);
|
|
|
|
return generateToken(claims);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Deprecated
|
|
|
|
@Deprecated
|
|
|
|
private String generateToken(Map<String,Object> claims){
|
|
|
|
private String generateToken(Map<String,Object> claims){
|
|
|
|
return Jwts.builder()
|
|
|
|
return Jwts.builder()
|
|
|
|
.setClaims(claims)
|
|
|
|
.setClaims(claims)
|
|
|
|
.setExpiration(getExpirationDate())
|
|
|
|
|
|
|
|
.signWith(SignatureAlgorithm.HS512,secret)
|
|
|
|
.signWith(SignatureAlgorithm.HS512,secret)
|
|
|
|
.compact();
|
|
|
|
.compact();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 生成token过期时间
|
|
|
|
|
|
|
|
* @return 过期时间
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private Date getExpirationDate() {
|
|
|
|
|
|
|
|
return new Date(System.currentTimeMillis() + expiration * 1000);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 从token中获取用户名
|
|
|
|
* 从token中获取用户名
|
|
|
|
* @param token 前端传入的token
|
|
|
|
* @param token 前端传入的token
|
|
|
@ -88,10 +78,11 @@ public class JwtTokenUtil {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
claims = Jwts.parser()
|
|
|
|
claims = Jwts.parser()
|
|
|
|
.setSigningKey(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"))
|
|
|
|
.setSigningKey(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"))
|
|
|
|
|
|
|
|
.setAllowedClockSkewSeconds(NEVER_EXPIRE)
|
|
|
|
.parseClaimsJws(token)
|
|
|
|
.parseClaimsJws(token)
|
|
|
|
.getBody();
|
|
|
|
.getBody();
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.info("jwt解析出错:{}",token);
|
|
|
|
log.warn("token 解析出错:{}",e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return claims;
|
|
|
|
return claims;
|
|
|
|
}
|
|
|
|
}
|
|
|
|