1、增加pom.xml配置
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.7.0</version>
</dependency>
2、jwt操作工具类
public class JwtUtils {
//秘钥
static final String SECERT = "秘钥";
static final long EXPIRATION_TIMEMILLIS = 60000 * 60 * 8; //默认8小时验证
/**
* 签发JWT
*
* @param id
* @param subject 可以是JSON数据 尽可能少
* @param ttlMillis 过期时间
* @return String
*/
public static String createJWT(String id, String subject, long ttlMillis) {
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
SecretKey secretKey = generalKey();
Map<String, Object> claims = new HashMap<>();
claims.put("uid", "123456");
claims.put("user_name", "admin");
claims.put("nick_name", "X-rapido");
JwtBuilder builder = Jwts.builder()
.setClaims(claims)
.setId(id)
.setSubject(subject) // 主题
.setIssuer("user") // 签发者
.setIssuedAt(now) // 签发时间
.signWith(signatureAlgorithm, secretKey); // 签名算法以及密匙
if (ttlMillis >= 0) {
long expMillis = nowMillis + ttlMillis;
Date expDate = new Date(expMillis);
builder.setExpiration(expDate); // 过期时间
} else {
long expMillis = nowMillis + EXPIRATION_TIMEMILLIS;
Date expDate = new Date(expMillis);
builder.setExpiration(expDate); // 过期时间
}
return builder.compact();
}
/**
* 验证JWT
*
* @param jwtStr
* @return
*/
public static BasePageData validateJWT(String jwtStr) {
BasePageData checkResult = new BasePageData();
Claims claims = null;
try {
claims = parseJWT(jwtStr);
checkResult.setCode(WebResponseCode.APPSUCCESS);
checkResult.setData(claims);
checkResult.setMsg("验证成功");
} catch (ExpiredJwtException e) {
checkResult.setCode(WebResponseCode.APPFAIL);
checkResult.setData(null);
checkResult.setMsg("验证过期");
} catch (SignatureException e) {
checkResult.setCode(WebResponseCode.APPFAIL);
checkResult.setData(null);
checkResult.setMsg("签名异常");
} catch (Exception e) {
checkResult.setCode(WebResponseCode.APPFAIL);
checkResult.setData(null);
checkResult.setMsg("用户名 或者 密码不正确");
}
return checkResult;
}
/**
* 获取秘钥
*
* @return
*/
public static SecretKey generalKey() {
Base64.Decoder decoder = Base64.getDecoder();
byte[] encodedKey = decoder.decode(SECERT);//秘钥
SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
return key;
}
/**
* 解析JWT字符串
*
* @param jwt
* @return
* @throws Exception
*/
public static Claims parseJWT(String jwt) throws Exception {
SecretKey secretKey = generalKey();
return Jwts.parser()
.setSigningKey(secretKey)
.parseClaimsJws(jwt)
.getBody();
}
public static void main(String[] args) {
System.out.println("##########nowDate=" + DateUtil.getStringDate());
String jwt = createJWT("1", "jack", 7000);
System.out.println("===========jwt=" + jwt);
System.out.println("n解密n");
BasePageData resulet = validateJWT(jwt);
System.out.println("#########resulet#" + JsonUtils.toJson(resulet));
System.out.println("n过期时间验证- 延迟5s - n");
try {
Thread.sleep(5000);
System.out.println("##########nowDate=" + DateUtil.getStringDate());
} catch (Exception e) {
e.printStackTrace();
}
BasePageData resulet_later = validateJWT(jwt);
System.out.println("#########resulet#" + JsonUtils.toJson(resulet_later));
}
}
参考资料:
https://www.cnblogs.com/wangshouchang/p/9551748.html
https://www.jianshu.com/p/fe67b4bb6f2c
来源:oschina
链接:https://my.oschina.net/u/3204029/blog/4297769