package com.mes.metering.util;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @program: wx-template
* @description
* @author: sgy
* @create: 2022-05-02 18:17
**/
public class TokenUtils {
//设置过期时间
private static final long EXPIRE_DATE=30*60*100000;
//token秘钥
private static final String TOKEN_SECRET = "ZCfasfhuaUUHufguGuwu2020BQWE";
// private static final String TOKEN_SECRET = "56";
public static String token (String username,String password,String flag){
String token = "";
try {
//过期时间
Date date = new Date(System.currentTimeMillis()+EXPIRE_DATE);
//秘钥及加密算法
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
//设置头部信息
Map<String,Object> header = new HashMap<>();
header.put("typ","JWT");
header.put("alg","HS256");
//携带username,password信息,生成签名
token = JWT.create()
.withHeader(header)
.withClaim("username",username)
.withClaim("flag",flag)
.withClaim("password",password).withExpiresAt(date)
.sign(algorithm);
}catch (Exception e){
e.printStackTrace();
return null;
}
return token;
}
public static boolean verify(String token){
/**
* @desc 验证token,通过返回true
* @params [token]需要校验的串
**/
try {
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
JWTVerifier verifier = JWT.require(algorithm).build();
DecodedJWT jwt = verifier.verify(token);
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
public static String parseJWT(String token){
/**
* @desc 解密token,返回一个map
* @params [token]需要校验的串
**/
DecodedJWT decodeToken = JWT.decode(token);
return decodeToken.getClaim("flag").asString();
}
public static String parseMobile(String token){
/**
* @desc 解密token,返回一个map
* @params [token]需要校验的串
**/
DecodedJWT decodeToken = JWT.decode(token);
return decodeToken.getClaim("username").asString();
}
public static boolean isJwtExpired(String token){
/**
* @desc 判断token是否过期
* @author lj
*/
try {
DecodedJWT decodeToken = JWT.decode(token);
return decodeToken.getExpiresAt().before(new Date());
} catch(Exception e){
return true;
}
}
public static void main(String[] args) {
String username ="zhangsan";
String password = "123";
String flag="1";
String token = token(username,password,flag);
System.out.println(token);
// boolean b = verify("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwYXNzd29yZCI6IjEyMyIsImV4cCI6MTY1MTY2NjgxMiwidXNlcm5hbWUiOiJ6aGFuZ3NhbiJ9.ufQzpVE0scbG1xznt1WdZvCiZgCkyNEfRzk_jpeFMw4");
// System.out.println(b);
// String st = parseJWT("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwYXNzd29yZCI6IjEyMyIsImV4cCI6MTY1MTY2NjgxMiwidXNlcm5hbWUiOiJ6aGFuZ3NhbiJ9.ufQzpVE0scbG1xznt1WdZvCiZgCkyNEfRzk_jpeFMw4");
// System.out.println(st);
// Boolean c = isJwtExpired("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwYXNzd29yZCI6IjEyMyIsImV4cCI6MTY1MTY2NjgxMiwidXNlcm5hbWUiOiJ6aGFuZ3NhbiJ9.ufQzpVE0scbG1xznt1WdZvCiZgCkyNEfRzk_jpeFMw4");
// System.out.println(c);
}
}
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.8.2</version>
</dependency>版权声明:本文为moty2011原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。