public class JwtUtil {
private static final long EXPIRE_TIME = 86400000;
private static final String TOKEN_SECRET = "8a80c107466a0b9c01466cfd0003";
public static boolean verify(String token) {
try {
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
JWTVerifier verifier = JWT.require(algorithm).build();
DecodedJWT jwt = verifier.verify(token);
return true;
} catch (Exception exception) {
return false;
}
}
public static String getuimUserid(String token) {
try {
DecodedJWT jwt = JWT.decode(token);
return jwt.getClaim("uimUserid").asString();
} catch (JWTDecodeException e) {
return null;
}
}
public static String getuimUserName(String token) {
try {
DecodedJWT jwt = JWT.decode(token);
return jwt.getClaim("uimUserName").asString();
} catch (JWTDecodeException e) {
return null;
}
}
public static String sign(String uimUserid,String uimUserName) {
try {
Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
Map<String, Object> header = new HashMap<>(2);
header.put("typ", "JWT");
header.put("alg", "HS256");
return JWT.create()
.withHeader(header)
.withClaim("uimUserid", uimUserid)
.withClaim("uimUserName",uimUserName)
.withExpiresAt(date)
.sign(algorithm);
} catch (Exception e) {
return null;
}
}
}
|