From 4fc49f3f06e9bb6275045d32245469816b451a4c Mon Sep 17 00:00:00 2001
From: liukangdong <898885815@qq.com>
Date: 星期二, 23 七月 2024 09:57:26 +0800
Subject: [PATCH] Merge branch 'master' of http://139.186.142.91:10010/r/productDev/zbomyoujia

---
 server/service/src/main/java/com/doumee/config/Jwt/WebMvcConfig.java |   85 ++++++++++++++++++++----------------------
 1 files changed, 41 insertions(+), 44 deletions(-)

diff --git a/server/service/src/main/java/com/doumee/config/Jwt/WebMvcConfig.java b/server/service/src/main/java/com/doumee/config/Jwt/WebMvcConfig.java
index 5d72f5a..c5872f9 100644
--- a/server/service/src/main/java/com/doumee/config/Jwt/WebMvcConfig.java
+++ b/server/service/src/main/java/com/doumee/config/Jwt/WebMvcConfig.java
@@ -65,27 +65,23 @@
                 HandlerMethod handlerMethod = (HandlerMethod) handler;
 
                 Class<?> beanType = handlerMethod.getBeanType();
-
-//                Method method = handlerMethod.getMethod();
-
-                // 鏈� @LoginRequired 娉ㄨВ锛岄渶瑕佺櫥褰曡璇� 瀹㈡埛绔娇鐢�
-                if (beanType.isAnnotationPresent(LoginRequired.class) || handlerMethod.hasMethodAnnotation(LoginRequired.class)) {
-                    //鑾峰彇token
-                    String token = request.getHeader(JwtTokenUtil.HEADER_KEY);  // 浠� http 璇锋眰澶翠腑鍙栧嚭 token
-                    if (StringUtils.isNotBlank(token)) {
-                        checkLogin(request,response);
-                    } else {
-                        throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"鏈櫥褰�");
-                    }
-                    // 鏈� @UserLoginRequired 娉ㄨВ锛岄渶瑕佺櫥褰曡璇� 鍐呴儴浜哄憳浣跨敤
-                } else if(beanType.isAnnotationPresent(UserLoginRequired.class) || handlerMethod.hasMethodAnnotation(UserLoginRequired.class)){
-                        //ERP 涓氬姟娉ㄨВ
-                        String token = request.getHeader(JwtTokenUtil.HEADER_KEY);
-                        if (StringUtils.isNotBlank(token)) {
-                            checkPersonnelLogin(request,response);
-                        } else {
-                            throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"鏈櫥褰�");
-                        }
+                Boolean checkFlag = false;
+                if(!( handlerMethod.hasMethodAnnotation(LoginRequired.class) || handlerMethod.hasMethodAnnotation(UserLoginRequired.class))
+                ){
+                    return true;
+                }
+                String token = request.getHeader(JwtTokenUtil.HEADER_KEY);
+                if(StringUtils.isBlank(token)){
+                    throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"鏈櫥褰�");
+                }
+                if (handlerMethod.hasMethodAnnotation(LoginRequired.class)) {
+                        checkFlag = checkLogin(request,response,token);
+                }
+                if(!checkFlag && handlerMethod.hasMethodAnnotation(UserLoginRequired.class)){
+                        checkFlag = checkPersonnelLogin(request,response,token);
+                }
+                if(!checkFlag){
+                    throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"鏈櫥褰�");
                 }
                 return true;
             }
@@ -94,20 +90,14 @@
     }
 
 
-    public Boolean checkLogin(HttpServletRequest request, HttpServletResponse response){
-        String token = request.getHeader(JwtTokenUtil.HEADER_KEY);
+    public Boolean checkLogin(HttpServletRequest request, HttpServletResponse response,String token){
         try {
-            //鑾峰彇璐﹀彿ID
-            Long memberId = JwtTokenUtil.getJwtPayLoad(token).getUserId();
-            String tokenRedis = (String) redisTemplate.opsForValue().get(ZTConstants.CUSTOMER+"_"+memberId);
-            if(StringUtils.isNotBlank(tokenRedis)){
-                if(!tokenRedis.equals(token)){
-                    throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME.getCode(),"闀挎椂闂存湭鎿嶄綔,璇烽噸鏂扮櫥褰�");
-                }
-            }else{
-                throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME.getCode(),"闀挎椂闂存湭鎿嶄綔,璇烽噸鏂扮櫥褰�");
+            String tokenRedis = (String) redisTemplate.opsForValue().get(ZTConstants.CUSTOMER+"_"+token);
+            if(StringUtils.isBlank(tokenRedis)){
+                return false;
             }
-            Member member = dao.queryForObject(" select  *  from `member` where id  = ?  limit 1  ", new BeanPropertyRowMapper<>(Member.class),memberId );
+            Long memberId = getTokenId(token);
+            Member member = dao.queryForObject(" select  *  from `member` where id  = ?  limit 1  ", new BeanPropertyRowMapper<>(Member.class),memberId);
             if(Objects.isNull(member)){
                 throw new BusinessException(ResponseStatus.DATA_EMPTY);
             }
@@ -119,25 +109,21 @@
             }
             request.setAttribute(JwtTokenUtil.UserId_Name, memberId);
             request.setAttribute(JwtTokenUtil.UserType, ZTConstants.CUSTOMER);
+            request.setAttribute(JwtTokenUtil.HEADER_KEY, ZTConstants.CUSTOMER+"_"+token);
+            request.setAttribute(JwtTokenUtil.UserInfo, tokenRedis);
             return true;
         } catch (IllegalArgumentException | JwtException e) {
             throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"鏈櫥褰�");
         }
     }
 
-    public Boolean checkPersonnelLogin(HttpServletRequest request, HttpServletResponse response){
-        String token = request.getHeader(JwtTokenUtil.HEADER_KEY);
+    public Boolean checkPersonnelLogin(HttpServletRequest request, HttpServletResponse response,String token){
         try {
-            //鑾峰彇璐﹀彿ID
-            Long userId = JwtTokenUtil.getJwtPayLoad(token).getUserId();
-            String tokenRedis = (String) redisTemplate.opsForValue().get(ZTConstants.BUSINESS+"_"+userId);
-            if(StringUtils.isNotBlank(tokenRedis)){
-                if(!tokenRedis.equals(token)){
-                    throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME.getCode(),"闀挎椂闂存湭鎿嶄綔,璇烽噸鏂扮櫥褰�");
-                }
-            }else{
-                throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME.getCode(),"闀挎椂闂存湭鎿嶄綔,璇烽噸鏂扮櫥褰�");
+            String tokenRedis = (String) redisTemplate.opsForValue().get(ZTConstants.BUSINESS+"_"+token);
+            if(StringUtils.isBlank(tokenRedis)){
+                return false;
             }
+            Long userId = getTokenId(token);
             Users users = dao.queryForObject(" select  *  from `users` where id  = ?  limit 1  ", new BeanPropertyRowMapper<>(Users.class),userId);
             if(Objects.isNull(users)){
                 throw new BusinessException(ResponseStatus.DATA_EMPTY);
@@ -150,6 +136,8 @@
             }
             request.setAttribute(JwtTokenUtil.UserId_Name, userId);
             request.setAttribute(JwtTokenUtil.UserType, ZTConstants.BUSINESS);
+            request.setAttribute(JwtTokenUtil.HEADER_KEY, ZTConstants.CUSTOMER+"_"+token);
+            request.setAttribute(JwtTokenUtil.UserInfo, tokenRedis);
             return true;
         } catch (IllegalArgumentException | JwtException e) {
             throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"鏈櫥褰�");
@@ -161,5 +149,14 @@
         return new RestTemplate();
     }
 
+    public Long getTokenId(String token){
+        try {
+            Integer lastIndex = token.lastIndexOf("_")+1;
+            Long tokenId = Long.valueOf(token.substring(lastIndex));
+            return tokenId;
+        }catch (Exception e){
+            throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"鏈櫥褰�");
+        }
+    }
 
 }

--
Gitblit v1.9.3