Преглед изворни кода

filter 中认证, aop 中鉴权

reghao пре 2 година
родитељ
комит
b350c2b79b

+ 2 - 23
dfs-store/src/main/java/cn/reghao/dfs/store/aop/AuthUserAspect.java

@@ -1,11 +1,6 @@
 package cn.reghao.dfs.store.aop;
 
 import cn.reghao.dfs.store.util.UserContext;
-import cn.reghao.jutil.web.ServletUtil;
-import cn.reghao.tnb.account.api.constant.TokenType;
-import cn.reghao.tnb.account.api.dto.AccountInfo;
-import cn.reghao.tnb.account.api.iface.AuthService;
-import org.apache.dubbo.config.annotation.DubboReference;
 import org.aspectj.lang.ProceedingJoinPoint;
 import org.aspectj.lang.annotation.Around;
 import org.aspectj.lang.annotation.Aspect;
@@ -22,10 +17,6 @@ import java.lang.reflect.Method;
 @Component
 @Aspect
 public class AuthUserAspect {
-    @DubboReference(check = false)
-    private AuthService authService;
-    private final String cookieKey = "USERDATA";
-
     @Pointcut("@annotation(cn.reghao.dfs.store.aop.AuthUser)")
     public void loginPointCut(){
     }
@@ -36,21 +27,9 @@ public class AuthUserAspect {
         MethodSignature ms = (MethodSignature)point.getSignature();
         Method method = aClass.getDeclaredMethod(ms.getName(),ms.getParameterTypes());
         AuthUser authUser = method.getAnnotation(AuthUser.class);
-
-        String sessId = ServletUtil.getCookie(cookieKey);
-        AccountInfo accountInfo = null;
-        if (sessId != null) {
-            accountInfo = authService.getAccountInfo(TokenType.cookie.getValue(), sessId);
-        } else {
-            sessId = ServletUtil.getBearerToken();
-            if (sessId != null) {
-                accountInfo = authService.getAccountInfo(TokenType.token.getValue(), sessId);
-            }
-        }
-
+        long userId = UserContext.getUser();
         boolean auth = authUser.value();
-        if (auth && accountInfo != null) {
-            UserContext userContext = new UserContext(accountInfo.getUserId());
+        if (auth && userId != -1) {
             return point.proceed(point.getArgs());
         }
 

+ 2 - 2
dfs-store/src/main/java/cn/reghao/dfs/store/config/web/WebConfig.java

@@ -45,9 +45,9 @@ public class WebConfig extends WebMvcConfigurationSupport {
     }
 
     @Bean
-    public FilterRegistrationBean<Filter> jwtTokenFilter() {
+    public FilterRegistrationBean<Filter> filterRegistrationBean(TokenFilter tokenFilter) {
         FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
-        registrationBean.setFilter(new TokenFilter());
+        registrationBean.setFilter(tokenFilter);
         registrationBean.addUrlPatterns("*");
         return registrationBean;
     }

+ 25 - 10
dfs-store/src/main/java/cn/reghao/dfs/store/inerceptor/TokenFilter.java

@@ -1,20 +1,26 @@
 package cn.reghao.dfs.store.inerceptor;
 
 import cn.reghao.dfs.store.util.UserContext;
-import lombok.extern.slf4j.Slf4j;
+import cn.reghao.jutil.web.ServletUtil;
+import cn.reghao.tnb.account.api.constant.TokenType;
+import cn.reghao.tnb.account.api.dto.AccountInfo;
+import cn.reghao.tnb.account.api.iface.AuthService;
+import org.apache.dubbo.config.annotation.DubboReference;
 import org.springframework.stereotype.Component;
 
 import javax.servlet.*;
-import javax.servlet.http.HttpServletRequest;
 import java.io.IOException;
 
 /**
  * @author reghao
- * @date 2022-08-25 21:14:23
+ * @date 2023-08-25 16:14:23
  */
-@Slf4j
 @Component
 public class TokenFilter implements Filter {
+    @DubboReference(check = false)
+    private AuthService authService;
+    private final String cookieKey = "USERDATA";
+
     @Override
     public void init(FilterConfig filterConfig) throws ServletException {
     }
@@ -23,15 +29,24 @@ public class TokenFilter implements Filter {
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
             throws IOException, ServletException {
         long userId = -1L;
-        /*HttpServletRequest httpServletRequest = (HttpServletRequest) request;
-        String auth = httpServletRequest.getHeader("Authorization");
-        if (auth != null) {
+        String sessId = ServletUtil.getCookie(cookieKey);
+        AccountInfo accountInfo = null;
+        if (sessId != null) {
+            accountInfo = authService.getAccountInfo(TokenType.cookie.getValue(), sessId);
+        } else {
+            sessId = ServletUtil.getBearerToken();
+            if (sessId != null) {
+                accountInfo = authService.getAccountInfo(TokenType.token.getValue(), sessId);
+            }
         }
 
-        try (UserContext context = new UserContext(userId)) {
-        }*/
+        if (accountInfo != null) {
+            userId = accountInfo.getUserId();
+        }
 
-        chain.doFilter(request, response);
+        try (UserContext context = new UserContext(userId)) {
+            chain.doFilter(request, response);
+        }
     }
 
     @Override