liukangdong
2024-06-12 a11bc1bed7953b88213330582c2085f60b5a73b1
server/system_gateway/src/main/java/com/doumee/config/jwt/JwtTokenUtil.java
@@ -2,6 +2,7 @@
import com.alibaba.fastjson.JSONObject;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.utils.Constants;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
@@ -33,7 +34,7 @@
        if(payloads == null){
            return  null;
        }
         payloads.setLoginDate(new Date());
        payloads.setLoginDate(new Date());
        Map<String,Object> map = new HashMap<>();
        map.put("id",payloads.getId());
//        Map<String,Object> map =   BeanUtil.beanToMap(payloads);
@@ -75,7 +76,8 @@
    public Boolean isTokenExpired(String token) {
        try {
            LoginUserInfo claims = getClaimsFromToken(token);
            Date expiration = claims.getLoginDate();
            Date expiration = //claims.getLoginDate();
            new Date(claims.getLoginDate().getTime() + jwtProperties.getExpiration());
            return expiration.before(new Date());
        } catch (Exception e) {
            //验证JWT签名失败等同于令牌过期
@@ -96,12 +98,25 @@
            claims.setLoginDate(new Date());
            refreshedToken = generateTokenDo(claims);
            if(refreshedToken!=null){
                redisTemplate.delete(token);//删除老的token
                redisTemplate.delete(Constants.REDIS_TOKEN_KEY+token);//删除老的token
            }
        } catch (Exception e) {
            refreshedToken = null;
        }
        return refreshedToken;
    }
    /**
     * 退出登陆
     *
     * @param token 原令牌
     * @return 新令牌
     */
    public void logout(String token) {
        try {
            redisTemplate.delete(Constants.REDIS_TOKEN_KEY+token);//删除老的token
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
@@ -131,7 +146,7 @@
                .setExpiration(expirationDate)
                .signWith(SignatureAlgorithm.HS512, jwtProperties.getSecret())
                .compact();
        redisTemplate.opsForValue().set(token,JSONObject.toJSONString(userInfo),jwtProperties.getExpiration(), TimeUnit.MILLISECONDS);
        redisTemplate.opsForValue().set(Constants.REDIS_TOKEN_KEY+token,JSONObject.toJSONString(userInfo),jwtProperties.getExpiration(), TimeUnit.MILLISECONDS);
        return token;
    }
@@ -144,7 +159,7 @@
    private LoginUserInfo getClaimsFromToken(String token) {
        LoginUserInfo claims;
        try {
            String userInfo = (String) redisTemplate.opsForValue().get(token);
            String userInfo = (String) redisTemplate.opsForValue().get(Constants.REDIS_TOKEN_KEY+token);
            claims = JSONObject.toJavaObject(JSONObject.parseObject(userInfo),LoginUserInfo.class);
        } catch (Exception e) {
            claims = null;