|
|
@@ -0,0 +1,96 @@
|
|
|
+package com.util;
|
|
|
+
|
|
|
+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;
|
|
|
+import com.model.po.TokenData;
|
|
|
+import com.model.po.User;
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
+import org.aspectj.lang.annotation.Aspect;
|
|
|
+import org.aspectj.lang.annotation.Before;
|
|
|
+import org.aspectj.lang.annotation.Pointcut;
|
|
|
+import org.springframework.core.annotation.Order;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Aspect
|
|
|
+@Component
|
|
|
+public class JwtTokenUtil {
|
|
|
+ //公共密钥
|
|
|
+ public static String SECRET = "YouRuanKeJiBi";
|
|
|
+
|
|
|
+ @Pointcut("@annotation(com.controller.CheckToken)")
|
|
|
+ public void CheckToken() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public TokenData createToke(User user){
|
|
|
+
|
|
|
+ //签发时间
|
|
|
+ Date iaDate = new Date();
|
|
|
+
|
|
|
+ //过期时间
|
|
|
+ Calendar nowTime = Calendar.getInstance();
|
|
|
+ nowTime.add(Calendar.MINUTE, 30);
|
|
|
+ Date expiresDate = nowTime.getTime();
|
|
|
+
|
|
|
+ //转换成时间戳
|
|
|
+ String res;
|
|
|
+ long ts = expiresDate.getTime();
|
|
|
+ res = String.valueOf(ts);
|
|
|
+ System.out.println("time:"+ ts);
|
|
|
+
|
|
|
+ //设置header信息
|
|
|
+ Map<String, Object> map = new HashMap<String, Object>();
|
|
|
+ map.put("alg", "HS256");
|
|
|
+ map.put("typ", "JWT");
|
|
|
+ String token = "";
|
|
|
+
|
|
|
+ try {
|
|
|
+ token = JWT.create()
|
|
|
+ .withHeader(map)
|
|
|
+ .withClaim("id", user.getId())
|
|
|
+ .withClaim("name", user.getName())
|
|
|
+ .withClaim("department", user.getDepartment())
|
|
|
+ .withClaim("post", user.getPost())
|
|
|
+ .withClaim("role", user.getRole())
|
|
|
+ .withExpiresAt(expiresDate)
|
|
|
+ .withIssuedAt(iaDate)
|
|
|
+ .sign(Algorithm.HMAC256(SECRET));
|
|
|
+ System.out.println("token:::::::::::" + token);
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ TokenData tokenData = new TokenData();
|
|
|
+ tokenData.setToken(token);
|
|
|
+ tokenData.setTimes(res);
|
|
|
+ return tokenData ;
|
|
|
+ }
|
|
|
+
|
|
|
+ //解密
|
|
|
+ @Before("CheckToken()")
|
|
|
+ @Order(1)
|
|
|
+ public static Map<String, Claim> verifyToken(JoinPoint joinPoint) throws Exception {
|
|
|
+ DecodedJWT jwt = null;
|
|
|
+
|
|
|
+ Object[] arg = joinPoint.getArgs();
|
|
|
+ String token = (String) arg[0];
|
|
|
+ System.out.println("token:::::" + token);
|
|
|
+
|
|
|
+ JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
|
|
|
+
|
|
|
+ try {
|
|
|
+ jwt = verifier.verify(token);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("登录凭证已过期,请重新登录");
|
|
|
+ }
|
|
|
+ return jwt.getClaims();
|
|
|
+ }
|
|
|
+}
|