huangzqa 4 роки тому
батько
коміт
79564b6821

+ 6 - 0
pom.xml

@@ -67,6 +67,12 @@
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>
+
+        <dependency>
+            <groupId>org.junit.platform</groupId>
+            <artifactId>junit-platform-launcher</artifactId>
+            <scope>test</scope>
+        </dependency>
         <!-- dcuc登陆 -->
         <dependency>
             <groupId>com.dragonsoft</groupId>

+ 8 - 5
src/main/java/com/dragonsoft/dcuc/approvegateway/filter/BimUserInfoPreFilter.java

@@ -65,7 +65,10 @@ public class BimUserInfoPreFilter extends ZuulFilter {
     public Object run() throws ZuulException {
         RequestContext ctx = RequestContext.getCurrentContext();
         HttpServletRequest request = ctx.getRequest();
-        if (isNeedUserInfo(request)) {
+        String contextPath = serverProperties.getServlet().getContextPath();
+        String requestUri = request.getRequestURI();
+
+        if (isNeedUserInfo(contextPath, requestUri)) {
             return null;
         }
 
@@ -108,12 +111,12 @@ public class BimUserInfoPreFilter extends ZuulFilter {
     /**
      * 是否需要用户信息
      *
-     * @param request 请求
+     * @param contextPath 上下文
+     * @param requestUri  请求路径
      * @return 是否需要
      */
-    private boolean isNeedUserInfo(HttpServletRequest request) {
-        String contextPath = serverProperties.getServlet().getContextPath();
-        String requestUri = request.getRequestURI();
+    public boolean isNeedUserInfo(String contextPath, String requestUri) {
+
         logger.debug("Security filter origin uri:{}", requestUri);
 
         // 去除上下文

+ 39 - 0
src/test/java/com/dragonsoft/dcuc/approvegateway/filter/BimUserInfoPreFilterTest.java

@@ -0,0 +1,39 @@
+package com.dragonsoft.dcuc.approvegateway.filter;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.web.ServerProperties;
+import org.springframework.boot.test.context.SpringBootTest;
+
+/**
+ * <p>
+ *
+ * </p>
+ *
+ * @author huangzqa
+ * @date 2021/6/21
+ */
+@SpringBootTest
+class BimUserInfoPreFilterTest {
+
+    @Autowired
+    private BimUserInfoPreFilter bimUserInfoPreFilter;
+
+    @Autowired
+    private ServerProperties serverProperties;
+
+    @Test
+    void isNeedUserInfo() {
+        String contextPath = serverProperties.getServlet().getContextPath();
+
+        boolean needUserInfo = bimUserInfoPreFilter.isNeedUserInfo(contextPath, contextPath + "/api/v1/process-types");
+
+        Assertions.assertFalse(needUserInfo);
+
+        boolean needUserInfo1 = bimUserInfoPreFilter.isNeedUserInfo(contextPath, contextPath + "/user/info");
+
+        Assertions.assertTrue(needUserInfo1);
+
+    }
+}