package com.hxzk.util;
|
|
import com.auth0.jwt.JWT;
|
import com.auth0.jwt.algorithms.Algorithm;
|
import com.auth0.jwt.interfaces.Claim;
|
import com.auth0.jwt.interfaces.DecodedJWT;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Component;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
@Component
|
public class TokenUtil {
|
@Value("hfghjhrtiyerjiofmwefnwefiewhiof212424")
|
private String secretKey;
|
/**
|
* 加密token.
|
*/
|
public String getToken(String userId) {
|
//这个是放到负载payLoad 里面,魔法值可以使用常量类进行封装.
|
String token = JWT
|
.create()
|
.withClaim("userId" ,userId)
|
// .withClaim("userRole", userRole)
|
.withClaim("timeStamp", System.currentTimeMillis())
|
.sign(Algorithm.HMAC256(secretKey));
|
return token;
|
}
|
/**
|
* 解析token.
|
* {
|
* "userId": "weizhong",
|
* "userRole": "ROLE_ADMIN",
|
* "timeStamp": "134143214"
|
* }
|
*/
|
public String parseToken(String token) {
|
HashMap<String, String> map = new HashMap<String, String>();
|
DecodedJWT decodedjwt = JWT.require(Algorithm.HMAC256(secretKey))
|
.build().verify(token);
|
Claim userId = decodedjwt.getClaim("userId");
|
// Claim userRole = decodedjwt.getClaim("userRole");
|
// Claim timeStamp = decodedjwt.getClaim("timeStamp");
|
// map.put("userId", userId.asString());
|
// map.put("userRole", userRole.asString());
|
// map.put("timeStamp", timeStamp.asLong().toString());
|
return userId.asString();
|
}
|
}
|