From 97624a399af2392fad51cfaed3a2e9ff9cd19931 Mon Sep 17 00:00:00 2001 From: heminjian502 Date: Thu, 11 Aug 2022 10:39:39 +0800 Subject: [PATCH] =?UTF-8?q?app=E7=99=BB=E5=BD=95=E5=A4=B1=E6=95=88?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application.yml | 2 ++ .../web/service/SysLoginService.java | 4 ++-- .../system/service/impl/UserTokenService.java | 24 +++++++++++++++++++ .../service/impl/WechatServiceImpl.java | 4 ++-- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index 095d274..32a9e8e 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -90,6 +90,8 @@ token: secret: abcdefghijklmnopqrstuvwxyz # 令牌有效期(默认30分钟) expireTime: 30 + # app令牌有效期(默认30day) + appExpireTime: 43200 # MyBatis配置 mybatis: diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java index 953ef7a..302bc0a 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java @@ -195,7 +195,7 @@ public class SysLoginService sysUser.setHospitalId(tDoctor.getHospitalId()); loginUser.setUserId(sysUser.getUserId()); loginUser.setUser(sysUser); - String token = userTokenService.createToken(loginUser); + String token = userTokenService.createTokenForApp(loginUser); AsyncManager.me().execute(AsyncFactory.recordLogininfor(sysUser.getUserName(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"))); AjaxResult ajaxResult = AjaxResult.success(); ajaxResult.put(Constants.TOKEN, token); @@ -211,7 +211,7 @@ public class SysLoginService LoginUser loginUser = new LoginUser(); loginUser.setUserId(sysUser.getUserId()); loginUser.setUser(sysUser); - String token = userTokenService.createToken(loginUser); + String token = userTokenService.createTokenForApp(loginUser); AsyncManager.me().execute(AsyncFactory.recordLogininfor(sysUser.getUserName(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"))); return token; } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/UserTokenService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/UserTokenService.java index 3a22965..8943ba9 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/UserTokenService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/UserTokenService.java @@ -41,6 +41,9 @@ public class UserTokenService @Value("${token.expireTime}") private int expireTime; + @Value("${token.appExpireTime}") + private int appExpireTime; + protected static final long MILLIS_SECOND = 1000; protected static final long MILLIS_MINUTE = 60 * MILLIS_SECOND; @@ -118,6 +121,18 @@ public class UserTokenService return createToken(claims); } + public String createTokenForApp(LoginUser loginUser) + { + String token = IdUtils.fastUUID(); + loginUser.setToken(token); + setUserAgent(loginUser); + refreshTokenForApp(loginUser); + + Map claims = new HashMap<>(); + claims.put(Constants.LOGIN_USER_KEY, token); + return createToken(claims); + } + /** * 验证令牌有效期,相差不足20分钟,自动刷新缓存 * @@ -148,6 +163,15 @@ public class UserTokenService redisCache.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES); } + public void refreshTokenForApp(LoginUser loginUser) + { + loginUser.setLoginTime(System.currentTimeMillis()); + loginUser.setExpireTime(loginUser.getLoginTime() + appExpireTime * MILLIS_MINUTE); + // 根据uuid将loginUser缓存 + String userKey = getTokenKey(loginUser.getToken()); + redisCache.setCacheObject(userKey, loginUser, appExpireTime, TimeUnit.MINUTES); + } + /** * 设置用户代理信息 * diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatServiceImpl.java index 03ec121..53c3c1a 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatServiceImpl.java @@ -244,7 +244,7 @@ public class WechatServiceImpl implements IWechatService { user.setSex(tPatient.getSex()); user.setPhonenumber(tPatient.getPhone()); loginUser.setUser(user); - String token = userTokenService.createToken(loginUser); + String token = userTokenService.createTokenForApp(loginUser); return token; } @@ -263,7 +263,7 @@ public class WechatServiceImpl implements IWechatService { user.setDoctorId(tDoctor.getId()); user.setHospitalId(tDoctor.getHospitalId()); loginUser.setUser(user); - String token = userTokenService.createToken(loginUser); + String token = userTokenService.createTokenForApp(loginUser); return token; } }