tianwu.sun 9 miesięcy temu
rodzic
commit
081bf0d4d6

+ 0 - 25
src/main/java/com/bootdo/common/token/TokenUtil.java

@@ -89,31 +89,6 @@ public final class TokenUtil {
         return AesUtil.encrypt(JsonUtil.toJson(token), CommonConfUtil.getConf("aes.key"), CommonConfUtil.getConf("aes.iv"));
     }
 
-    /**
-     * 对Token对象进行签名,并且生成token对象
-     * @return 返回签名后的token字符串
-     * @throws Exception 异常信息
-     */
-    public String createCurrTokenString() throws Exception {
-        //生成token信息
-        UserDO user = ShiroUtils.getUser();
-        if(ObjectUtils.isEmpty(user)){
-            return null;
-        }
-        Payload payload = new Payload();
-        payload.setUserId(user.getUserId());
-        payload.setUsername(user.getUsername());
-        payload.setPassword(user.getPassword());
-
-        Token tokenObj = new Token();
-        tokenObj.setPayload(payload);
-        Header header = new Header();
-        header.setTimestamp(System.currentTimeMillis() / 1000);
-        tokenObj.setHeader(header);
-        String token = createTokenString(tokenObj);
-        return token;
-    }
-
     /**
      * 校验token是否被篡改
      *

+ 22 - 24
src/main/java/com/bootdo/system/config/AuthorizationInterceptor.java

@@ -1,11 +1,9 @@
 package com.bootdo.system.config;
 
-
-
-import com.bootdo.common.constant.Constants;
 import com.bootdo.common.token.Token;
 import com.bootdo.common.token.TokenUtil;
 import com.bootdo.common.utils.IPUtils;
+import com.bootdo.common.utils.ShiroUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.springframework.stereotype.Component;
@@ -35,30 +33,30 @@ public class AuthorizationInterceptor implements HandlerInterceptor {
     @Override
     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
         try {
-            String tokenStr = request.getHeader(Constants.LOGIN_TOKEN_KEY);
-            if(ObjectUtils.isEmpty(tokenStr)){
-                tokenStr = request.getParameter(Constants.LOGIN_TOKEN_KEY);
+            String username = ShiroUtils.getUser().getUsername();
+            if(ObjectUtils.isEmpty(username)){
+                return true;
             }
-            if(ObjectUtils.isEmpty(tokenStr)){
-                tokenStr = tokenUtil.createCurrTokenString();
-                if(ObjectUtils.isEmpty(tokenStr)){
-                    logger.error("preHandle =====================tokenStr is null=======================");
-                }
+            String cacheToken =  tokenUtil.getLoginUser(username);
+            if(ObjectUtils.isEmpty(cacheToken)) {
+                logger.error("preHandle =====================cacheToken is null=======================A");
+                return true;
             }
-            if(!ObjectUtils.isEmpty(tokenStr)){
-                Token token = tokenUtil.getToken(tokenStr);
-                if (token == null) {
-                    logger.error("preHandle =====================token is null=======================");
-                } else {
-                    String ip = null;
-                    try {
-                        ip = IPUtils.getIpAddr(request);
-                    } catch (Exception e){
-                        e.printStackTrace();
-                    }
-                    tokenUtil.saveLoginUser(token.getPayload().getUsername(), tokenStr, ip,System.currentTimeMillis(),"1");
-                }
+            String tokenStr = cacheToken.split("_")[0];
+
+            Token token = tokenUtil.getToken(tokenStr);
+            if (ObjectUtils.isEmpty(token)) {
+                logger.error("preHandle =====================token is null=======================B");
+                return true;
+            }
+
+            String ip = null;
+            try {
+                ip = IPUtils.getIpAddr(request);
+            } catch (Exception e){
+                e.printStackTrace();
             }
+            tokenUtil.saveLoginUser(username, tokenStr, ip, System.currentTimeMillis(),"1");
 
         } catch (Exception e) {
             e.printStackTrace();

+ 0 - 13
src/main/java/com/bootdo/system/controller/LoginController.java

@@ -188,19 +188,6 @@ public class LoginController extends BaseController {
 
     }
 
-    /**
-     * 获取当前token
-     */
-    @GetMapping(value = "/getCurrToken")
-    @ResponseBody
-    public String getCurrToken() {
-        try {
-           return tokenUtil.createCurrTokenString();
-        } catch (Exception e) {
-            logger.error("获取当前token失败>>>> ", e);
-            return null;
-        }
-    }
 
     @GetMapping("/logout")
     String logout() {

+ 11 - 6
src/main/java/com/bootdo/system/service/impl/SessionServiceImpl.java

@@ -22,6 +22,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import com.bootdo.common.utils.RedisUtil;
+import org.springframework.util.ObjectUtils;
 
 /**
  * 待完善
@@ -115,13 +116,17 @@ public class SessionServiceImpl implements SessionService {
 
         Session session = sessionDAO.readSession(sessionId);
 
-        SimplePrincipalCollection principalCollection = (SimplePrincipalCollection) session
-                .getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
-        UserDO userDO = (UserDO) principalCollection.getPrimaryPrincipal();
-        //删除登录的用户
-        redisUtil.hdel(Constants.ONLINE_USER, userDO.getUsername());
+        if(!ObjectUtils.isEmpty(session)){
+            SimplePrincipalCollection principalCollection = (SimplePrincipalCollection) session
+                    .getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
+            UserDO userDO = (UserDO) principalCollection.getPrimaryPrincipal();
+            //删除登录的用户
+            redisUtil.hdel(Constants.ONLINE_USER, userDO.getUsername());
+
+            session.stop();
 
-        sessionDAO.delete(session);
+            sessionDAO.delete(session);
+        }
 
         return true;
     }