Spring Cloud Gateway URI解码失败

时间:2019-06-20 20:28:57

标签: spring-cloud

我使用Spring Cloud Greenwich二进制文件编写了一个网关应用程序。 URL中出现特殊字符时出现问题。当请求URI包含特殊字符时,请求失败,并且在Spring网关中出现以下异常。

localhost:8080/myresource/WG_splchar_%26%5E%26%25%5E%26%23%25%24%5E%26%25%26*%25%2B)!%24%23%24%25%26%5E_new

当我在url上方点击时,Spring失败并出现以下异常。我无法弄清楚为什么它是无效的序列,以及如何处理这些序列。

 java.lang.IllegalArgumentException: Invalid encoded sequence "%^&#%$^&%&*%+)!$#$%&^_new"
at org.springframework.util.StringUtils.uriDecode(StringUtils.java:741) ~[spring-core-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.http.server.DefaultPathContainer.parsePathSegment(DefaultPathContainer.java:126) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.http.server.DefaultPathContainer.createFromUrlPath(DefaultPathContainer.java:111) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.http.server.PathContainer.parsePath(PathContainer.java:76) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory.lambda$apply$2(PathRoutePredicateFactory.java:79) ~[spring-cloud-gateway-core-2.1.0.RC3.jar:2.1.0.RC3]
at org.springframework.cloud.gateway.support.ServerWebExchangeUtils.lambda$toAsyncPredicate$1(ServerWebExchangeUtils.java:128) ~[spring-cloud-gateway-core-2.1.0.RC3.jar:2.1.0.RC3]
at org.springframework.cloud.gateway.handler.AsyncPredicate.lambda$and$1(AsyncPredicate.java:35) ~[spring-cloud-gateway-core-2.1.0.RC3.jar:2.1.0.RC3]
at org.springframework.cloud.gateway.handler.RoutePredicateHandlerMapping.lambda$null$2(RoutePredicateHandlerMapping.java:112) ~[spring-cloud-gateway-core-2.1.0.RC3.jar:2.1.0.RC3]
at reactor.core.publisher.MonoFilterWhen$MonoFilterWhenMain.onNext(MonoFilterWhen.java:116) [reactor-core-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at reactor.core.publisher.Operators$ScalarSubscription.request(Operators.java:2070) [reactor-core-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at reactor.core.publisher.MonoFilterWhen$MonoFilterWhenMain.onSubscribe(MonoFilterWhen.java:103) [reactor-core-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at reactor.core.publisher.MonoJust.subscribe(MonoJust.java:54) [reactor-core-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at reactor.core.publisher.MonoFilterWhen.subscribe(MonoFilterWhen.java:56) [reactor-core-3.2.5.RELEASE.jar:3.2.5.RELEASE]

1 个答案:

答案 0 :(得分:1)

我已经回答了另一个问题,不想重新输入。答案的实质是完全相同的。

使用Spring Cloud utils编写执行此方法的单元测试。这是最新的。您可以尝试传入或多或少关注的字符串,以查找损坏的位置。使用二进制搜索找出损坏的地方。确保不要在编码字符的中间拆分字符串,否则会给自己带来误报。当它说您的序列无效时,我希望您遇到%99之类的字符,其中99是不会映射到任何有效字符(我只是在补一个字符)

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/StringUtils.html#uriDecode-java.lang.String-java.nio.charset.Charset-

顺便说一句

此编码字符串来自何处?您公司的某人是否创建了自己的解决方案来对该字符串进行编码?您接受用户数据吗?很有可能是由负责生成此字符串的任何人通过将自己的编码器设为Homeroll来错误地对其进行编码的。

交替

spring.cloud.gateway.routes[7].predicates[0]=Path=/test/{testId}/test1/test_%26%5E%26%25%5E%26%25%26*%25%2B)!

当我看到这个时,我看到的路径已经已编码。例如,您已将&符号替换为%26

您是否尝试输入尚未编码的路径?

例如 spring.cloud.gateway.routes[7].predicates[0]=Path=/test/{testId}/test1/test_&^&%^ <我仅使用此图表手工对其进行了部分解码。 https://www.w3schools.com/tags/ref_urlencode.asp