token工具类
内置解析token方法
public class JwtUtils {
//签名密钥
private static String key = "userlogin";
//签名失效时间
// private static Long failureTime = Long.valueOf(604800000);
/**
* 设置认证token
*
* @Author:
* @Date: 10:10 2020/12/11
*/
public static String createJwt( Map<String, Object> map,Long failureTime) {
//设置失效时间
long now = System.currentTimeMillis(); //毫秒
long exp = now + failureTime;
//创建JwtBuilder
JwtBuilder jwtBuilder = Jwts.builder()
.setIssuedAt(new Date())
//设置签名防止篡改
.signWith(SignatureAlgorithm.HS256, key);
//根据map设置claims
for (Map.Entry<String, Object> entry : map.entrySet()) {
jwtBuilder.claim(entry.getKey(), entry.getValue());
}
jwtBuilder.setExpiration(new Date(exp));
//创建token
String token = jwtBuilder.compact();
return token;
}
/**
* 解析token
*
* @param token
* @return
*/
public static Claims parseJwt(String token) {
Claims claims = Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody();
return claims;
}
public static void main(String[] args) {
Claims claims = JwtUtils.parseJwt("");
System.out.println(claims);
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
// public Long getFailureTime() {
// return failureTime;
// }
//
// public void setFailureTime(Long failureTime) {
// this.failureTime = failureTime;
// }
@Override
public String toString() {
return "JwtUtils{" +
"key='" + key + '\'' +
// ", failureTime=" + failureTime +
'}';
}
/**
* 配置jwt
* @param
* @Author:
* @Date: 10:44 2020/12/11
*/
@Bean
public JwtUtils jwtUtils(){
return new JwtUtils();
}
}