起因:测试在输入框[](中括号),然后get请求报错
报错:Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
分析:本地Swagger请求后台无错误。和前段联调发现该错误。
错误原因:在tomcat中是遵循的是URI编码标准(RFC 3986),该规范只允许包含 a-zA-Z 0-9 - _ . ~ 以及所有保留字符。所以我们需要放行。
解决办法:
1.前端转译,比如空格在url地址中就是20%
2.后端tomcat解除拦截
以下是springboot解决方式,如果是spring3.0则修改tomcat配置
@Configuration
public class TomcatConfig {
@Bean
public ConfigurableServletWebServerFactory webServerFactory(){
TomcatServletWebServerFactory factory =new TomcatServletWebServerFactory();
factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
@Override
public void customize(Connector connector) {
connector.setProperty("relaxedQueryChars","|[]");
}
});
return factory;
}
}