token
1,前段
request.headers.Authentication = "bearer: "+item;
2,后端
取出
String authentication = request.getHeader("Authentication");
创建和解析
/**
*创建token
* @return token
*/
public String createToken(String uuid){
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
String token = JWT.create()
.withIssuer("魏湛郴")
.withSubject("登录验证")
.withClaim("uuid",uuid)
.sign(algorithm);
return token;
}
/**
* 解析token
* 解析失败直接异常
* @param token
*/
public DecodedJWT verifyToken(String token){
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
JWTVerifier verifier = JWT.require(algorithm)
.withIssuer("魏湛郴")
.withSubject("登录验证")
.build(); //Reusable verifier instance
DecodedJWT jwt = verifier.verify(token);
return jwt;
}