jwt的依赖有不同,有些博客下用的是com.auth0 ,有的用的是io.jsonwebtoken (不清楚哪个好)
首先生产token,然后把token(XXX.YYY.ZZZ)随意修改几个字符,然后在用JWT解密,这个时候不是应该报错吗?
/** token秘钥,请勿泄露,请勿随便修改 backups:JKKLJOoasdlfj */
public static final String SECRET = "JKKLJOoasdlfj";
/** token 过期时间: 10天 */
public static final int calendarField = Calendar.DATE;
public static final int calendarInterval = 10;
* JWT生成Token.<br/>
* JWT构成: header, payload, signature
* @param user_id
* 登录成功后用户user_id, 参数user_id不可传空
public static String createToken(Long user_id) throws Exception {
Date iatDate = new Date();
// expire time
Calendar nowTime = Calendar.getInstance();
nowTime.add(calendarField, calendarInterval);
Date expiresDate = nowTime.getTime();
// header Map
Map<String, Object> map = new HashMap<>();
map.put("alg", "HS256");
map.put("typ", "JWT");
// build token
// param backups {iss:Service, aud:APP}
String token = JWT.create().withHeader(map) // header
.withClaim("iss", "Service") // payload
.withClaim("aud", "APP").withClaim("user_id", null == user_id ? null : user_id.toString())
.withIssuedAt(iatDate) // sign time
.withExpiresAt(expiresDate) // expire time
.sign(Algorithm.HMAC256(SECRET)); // signature
return token;
* 解密Token
* @param token
* @return
* @throws Exception
public static Map<String, Claim> verifyToken(String token) {
DecodedJWT jwt = null;
try {
JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
jwt = verifier.verify(token);
} catch (Exception e) {
// e.printStackTrace();
// token 校验失败, 抛出Token验证非法异常
return jwt.getClaims();
verfier.verify(token)方法不是应该报错吗? 因为token已经不是原来的token了?(对于JWT的token验证,有没有大神可以指点下我。 谢谢)
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
public class JwtToken {
private static String MY_SECRET = "mysecret";// 自己的加盐部分
public static String createToken() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("alg", "HS256");
map.put("typ", "JWT");
String token = JWT.create().withHeader(map)// header
.withClaim("name", "zwz")// payload
.withClaim("age", "18").sign(Algorithm.HMAC256(MY_SECRET));// 加密
return token;
public static void verifyToken(String token, String key) throws Exception {
JWTVerifier verifier = JWT.require(Algorithm.HMAC256(key)).build();
DecodedJWT jwt = verifier.verify(token);
Map<String, Claim> claims = jwt.getClaims();
System.out.println(claims.get("name").asString());
public static void main(String[] args) throws Exception {
String str = createToken();
System.out.println(str);
verifyToken(str, MY_SECRET);
---------------------
作者:faicm
来源:CSDN
原文:https://blog.csdn.net/faicm/article/details/80401478
版权声明:本文为博主原创文章,转载请附上博文链接!
我照着上面这篇博客的代码试了一遍,成功了。 依赖还是“com.auth0 ”的jwt, 很奇怪,这次修改token 就会报错:
com.auth0.jwt.exceptions.SignatureVerificationException: The Token's Signature resulted invalid when verified using the Algorithm: HmacSHA256
如果设置了过期时间,且验证时间过期了,那么也会报错(时间过期....)。
如果你用的是io.jsonwebtoken依赖,可以看看这篇博客:http://www.ibloger.net/article/3075.html
到现在,关于session,token,cookie 我也是云里雾里,也没有设置过cookie(没写过记住密码,感觉浏览器都做了。。) 之前是前后端分离,后台自定义根据uuid什么的生成个token,然后把token作为redis的key,value为包含用户信息的map,再加个过期时间。 而获取到了JWT的token,然后拿到https://jwt.io/ 感觉里面含有什么信息都显示出来了。。