java生成tonken工具类
package com.cgnpc.imsqs.utils;
import cn.hutool.core.bean.BeanUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTCreationException;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.cgnpc.cud.core.exception.BaseServiceException;
import com.cgnpc.framework.exception.BusinessException;
import com.cgnpc.imsqs.app.util.AppUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
@Slf4j
public class TokenUtil {
private static final long EXPIRE_TIME = 7 * 24 * 60 * 60 * 1000;
private static final String TOKEN_SECRET = "l122adasw532df";
public static String sign(Map<String,Object> map) {
String token = null;
try {
Date expireAt = new Date(System.currentTimeMillis() + EXPIRE_TIME);
token = JWT.create()
.withIssuer("auth0")
.withClaim("userNo", map)
.withExpiresAt(expireAt)
.sign(Algorithm.HMAC256(TOKEN_SECRET));
} catch (IllegalArgumentException | JWTCreationException je) {
log.error("令牌创建失败!{}",je);
}
return token;
}
public static Boolean verify(String token) {
try {
JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET))
.withIssuer("auth0").build();
DecodedJWT decodedJWT = jwtVerifier.verify(token);
log.info("认证通过:");
log.info("userId: {} 过期时间:{}", TokenUtil.getUser(token), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(decodedJWT.getExpiresAt()));
} catch (IllegalArgumentException | JWTVerificationException e) {
log.error("token 验证异常:{}", e);
return false;
}
return true;
}
public static AppUser getUser(String token) {
AppUser appUser;
if(token == "" || token == null){
return null;
}
try {
DecodedJWT jwt = JWT.decode(token);
Map<String,Object> userNo = jwt.getClaim("userNo").asMap();
appUser = BeanUtil.fillBeanWithMap(userNo,new AppUser(),false);
} catch (JWTDecodeException e) {
log.error("获取用户信息异常:{}", e);
throw new BusinessException("获取用户信息异常");
}
return appUser;
}
public static AppUser getCurrentUser(){
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String sign = request.getAttribute("sign").toString();
if(StringUtils.isEmpty(sign)){
throw new BaseServiceException("获取app用户加密信息异常");
}
return getUser(sign);
}
public static String getToken(){
String sign = "";
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
try{
sign = request.getAttribute("sign").toString();
}catch (Exception e){
sign = "";
}
return sign;
}
public static void main(String[] args) {
System.out.println(System.currentTimeMillis()/1000);
}
}