瀏覽代碼

feat: 用户信息写入redis时记录登录日志

weijianghai 2 年之前
父節點
當前提交
c414e284c7

+ 2 - 2
src/main/java/com/nokia/controller/VerificationController.java

@@ -38,7 +38,7 @@ public class VerificationController {
     @Transactional(rollbackFor = Exception.class)
     @PostMapping("/verification")
     public R verifySystem(@Valid @RequestBody TokenVo tokenEntity) {
-        User userEntity = aclService.verifySystem(tokenEntity.getToken(), tokenEntity.getFromSystem());
+        User userEntity = aclService.verifySystem(tokenEntity);
         log.debug("输出用户信息: {}", userEntity);
         Map<String, String> map = new HashMap<>();
         if (tokenEntity.getFromSystem().equalsIgnoreCase("top")) {
@@ -63,7 +63,7 @@ public class VerificationController {
         if (userEntity == null) {
             return R.error().data(map).message("用户不存在");
         } else {
-            aclService.logVerification(tokenEntity, userEntity);
+//            aclService.logVerification(tokenEntity, userEntity);
             switch (tokenEntity.getSystem().trim().toLowerCase()) {
                 case "liucheng":
                     return R.ok().data(new LiuchengUserVo(userEntity));

+ 12 - 4
src/main/java/com/nokia/service/AclService.java

@@ -62,10 +62,15 @@ public class AclService {
         verificationLogDao.insert(verificationLog);
     }
 
-    public User verifySystem(String token, String fromSystem) {
-        User userEntity = null;
+    public User verifySystem(TokenVo tokenVo) {
+        String token = tokenVo.getToken();
+        String fromSystem = tokenVo.getFromSystem();
+        String system = tokenVo.getSystem();
+        // 为不同system生成不同key
+        String key = system + ":" + token;
+        User userEntity;
         // 从redis中查询
-        userEntity = (User) redisTemplate.opsForValue().get(token);
+        userEntity = (User) redisTemplate.opsForValue().get(key);
         if (userEntity != null) {
             log.debug("redis查询成功: {}", userEntity);
             // 2.1 可以查到用户信息,重置redis中的用户信息,刷新到期时间
@@ -84,7 +89,10 @@ public class AclService {
                 userEntity = userDao.getByLoginName(tokenFlagVo.getLoginName());
                 if (userEntity != null) {
                     // 仅当用户信息不为空时,将用户信息存入redis
-                    redisTemplate.opsForValue().set(token, userEntity, timeoutSeconds, TimeUnit.SECONDS);
+                    redisTemplate.opsForValue().set(key, userEntity, timeoutSeconds, TimeUnit.SECONDS);
+                    // redis保存新token信息,记录登录日志
+                    logVerification(tokenVo, userEntity);
+                    log.info("{}登录{}", userEntity.getLoginName(), tokenVo.getSystem());
                 }
                 // 返回用户信息
                 return userEntity;