From 5d2d88a8ed4987f178f38f2162b9d25ed23c8cb7 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期日, 08 十月 2023 14:54:32 +0800 Subject: [PATCH] 修订令牌的有效期设定 --- src/main/java/com/moon/server/service/sys/TokenService.java | 42 +++++++++++++++--------------------------- 1 files changed, 15 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/moon/server/service/sys/TokenService.java b/src/main/java/com/moon/server/service/sys/TokenService.java index 40b0698..cdde911 100644 --- a/src/main/java/com/moon/server/service/sys/TokenService.java +++ b/src/main/java/com/moon/server/service/sys/TokenService.java @@ -9,9 +9,9 @@ import com.moon.server.helper.WebHelper; import com.moon.server.mapper.sys.TokenMapper; import com.moon.server.service.all.RedisService; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Date; @@ -25,16 +25,16 @@ */ @Service public class TokenService implements TokenMapper { - @Autowired + @Resource TokenMapper tokenMapper; - @Autowired + @Resource UserService usersService; - @Autowired + @Resource LoginService loginService; - @Autowired + @Resource RedisService redisService; @Override @@ -156,20 +156,9 @@ return false; } - // redis - String tokenKey = RedisCacheKey.signTokenKey(token); - if (redisService.hasKey(tokenKey)) { - return true; - } + TokenEntity te = getEntityByToken(token); - // db - TokenEntity te = selectOneByToken(token); - if (null != te) { - redisService.put(tokenKey, te, te.getDuration(), TimeUnit.MINUTES); - return true; - } - - return false; + return null != te; } /** @@ -177,7 +166,7 @@ */ public Boolean logout(String token, HttpServletRequest req, HttpServletResponse res) { TokenEntity te = getEntityByToken(token); - if (te == null) { + if (null == te) { return false; } @@ -218,10 +207,6 @@ * 鏍规嵁浠ょ墝鑾峰彇瀹炰綋 */ public TokenEntity getEntityByToken(String token) { - if (StringHelper.isNull(token)) { - return null; - } - String tokenKey = RedisCacheKey.signTokenKey(token); // redis @@ -233,7 +218,10 @@ // db TokenEntity te = selectOneByToken(token); if (null != te) { - redisService.put(tokenKey, te, te.getDuration(), TimeUnit.MINUTES); + long min = StringHelper.getMinuteDifference(te.getExpire()); + if (min > 0) { + redisService.put(tokenKey, te, min, TimeUnit.MINUTES); + } } return te; @@ -281,9 +269,9 @@ // db UserEntity ue = usersService.selectByToken(token); -// if (null != ue) { -// getEntityByToken(token); -// } + if (null != ue) { + getEntityByToken(token); + } return ue; } -- Gitblit v1.9.3