Explorar el Código

fix: 修复 bug;

Walker hace 1 año
padre
commit
d9ec556d6a

+ 7 - 3
src/main/java/com/ywt/gateway/filter/AuthGatewayFilterFactory.java

@@ -161,7 +161,7 @@ public class AuthGatewayFilterFactory extends AbstractGatewayFilterFactory<HostL
                                 }
                             }
                         case AUTH_TYPE_API:
-                            if (POST.equalsIgnoreCase(methodName))
+                            if (!POST.equalsIgnoreCase(methodName))
                                 throw new HttpMsgException(HttpStatus.METHOD_NOT_ALLOWED, HttpStatus.METHOD_NOT_ALLOWED.value(),
                                         HttpStatus.METHOD_NOT_ALLOWED.getReasonPhrase());
                             String authStr = Optional.ofNullable(headers.getFirst(KEY_AUTHORIZATION)).orElse("");
@@ -204,8 +204,12 @@ public class AuthGatewayFilterFactory extends AbstractGatewayFilterFactory<HostL
                                 headers.add(KEY_AUTH_DATA, authdata);
                             }
                             Map<String, String> authParamMap = new HashMap<>();
-                            authParamMap.putAll(authInfo.getParams());
-                            authParamMap.putAll(appInfo.getParams());
+                            if (authInfo.getParams() != null) {
+                                authParamMap.putAll(authInfo.getParams());
+                            }
+                            if (appInfo.getParams() != null) {
+                                authParamMap.putAll(appInfo.getParams());
+                            }
                             headers.add(KEY_AUTH_PARAM, JSONUtil.toJsonStr(authParamMap));
 
                             // 下发当前授权的 appid 至后端

+ 81 - 0
src/main/resources/application-routes.yml

@@ -0,0 +1,81 @@
+spring:
+  cloud:
+    gateway:
+      routes:
+        # 配置路由,可以配置多个
+        - id: BUSINESS-OAUTH2  # 自定义路由的ID 保持唯一
+          # 采用 LoadBalanceClient 方式请求,以 lb:// 开头,后面的是注册在 Nacos 上的服务名
+          #https://www.jianshu.com/p/9994b2da8645 LoadBalanceClient解释
+          uri: lb://business-oauth2
+          # Predicate 翻译过来是“谓词”的意思,必须,主要作用是匹配用户的请求,有很多种用法
+          predicates:
+            # 路径匹配,以 api 开头,直接配置是不生效的,看 filters 配置
+            - Path=/api/user/**
+          filters:
+            # 前缀过滤,默认配置下,我们的请求路径是 http://localhost:8888/business-oauth2/** 这时会路由到指定的服务
+            # 此处配置去掉 1 个路径前缀,再配置上面的 Path=/api/**,就能按照 http://localhost:8888/api/** 的方式访问了
+            - StripPrefix=1
+        - id: INTERNET-HOSP-WEB
+          uri: lb://internethospital-web
+          predicates:
+            - Path=/taihe/user/checkCardNo
+          filters:
+            - name: Auth
+              args:
+                url: '/'
+                #url匹配类型 regex: 正则, prefix: 前缀, full 全匹配
+                matchType: 'prefix'
+                replacePrefixUrl: false
+                auth: 'external-api'
+                authResponse401: true
+                #设置需要刷新的授权,当 auth 不为空,以 auth 的配置为主
+                refreshAuth: ''
+                setHeaders: [ ]
+        - id: HISAPI-TAIHE-COM
+          uri: lb://his-api-server
+          predicates:
+            - Host=hisapi.taihe.com
+          filters:
+            - name: Auth
+              args:
+                url: '/'
+                #url匹配类型 regex: 正则, prefix: 前缀, full 全匹配
+                matchType: 'prefix'
+                replacePrefixUrl: false
+                auth: 'external-api'
+                authResponse401: true
+                #设置需要刷新的授权,当 auth 不为空,以 auth 的配置为主
+                refreshAuth: ''
+                setHeaders: [ ]
+        - id: YWT-TEST-TAIHE-COM
+          uri: lb://ywt-api-server-test
+          predicates:
+            - Host=hisapi.taihe.com
+          filters:
+            - name: Auth
+              args:
+                url: '/'
+                #url匹配类型 regex: 正则, prefix: 前缀, full 全匹配
+                matchType: 'prefix'
+                replacePrefixUrl: false
+                auth: 'external-api'
+                authResponse401: true
+                #设置需要刷新的授权,当 auth 不为空,以 auth 的配置为主
+                refreshAuth: ''
+                setHeaders: [ ]
+        - id: YWT-TAIHE-COM
+          uri: lb://ywt-api-server
+          predicates:
+            - Host=hisapi.taihe.com
+          filters:
+            - name: Auth
+              args:
+                url: '/'
+                #url匹配类型 regex: 正则, prefix: 前缀, full 全匹配
+                matchType: 'prefix'
+                replacePrefixUrl: true
+                auth: 'external-api'
+                authResponse401: true
+                #设置需要刷新的授权,当 auth 不为空,以 auth 的配置为主
+                refreshAuth: ''
+                setHeaders: [ ]