JWT的使用
参考教程: JWT认证原理、流程整合springboot实战应用,前后端分离认证的解决方案!
创建JWTUtils工具类
@Component
public class JWTUtils {
private static final String salt = "!hfuw3y*&";
public static String getToken(Map<String,String> map){
Calendar instance = Calendar.getInstance();
instance.add(Calendar.DATE,7);
JWTCreator.Builder builder = JWT.create();
map.forEach((k,v)->{
builder.withClaim(k,v);
});
String token = builder.withExpiresAt(instance.getTime())
.sign(Algorithm.HMAC256(salt));
return token;
}
public static DecodedJWT verifyToken(String token){
DecodedJWT verify = JWT.require(Algorithm.HMAC256(salt)).build().verify(token);
return verify;
}
public static String getUserIdFromToken(String token){
DecodedJWT verify = JWT.require(Algorithm.HMAC256(salt)).build().verify(token