写了一个基于springboot的restful接口,但按照设计,请求地址十分古怪,类似这样:
http://192.168.10.8:8080/?skey=fb5e8ea9249c4ac19dad5e2a341e09ce&filter[]=A3011300500,eq,35041&filter[]=A3001200400,ge,2021-01-01 00:00&filter[]=A3001200400,le,2021-02-01 00:00&satisfy=AND
- 1
可以发现,地址后面的参数中,含有多个"filter[]",里面有一对方框。结果请求的时候,根本无法到达控制器。浏览器直接显示:HTTP Status 400 – Bad Request。
写了拦截器、过滤器,想着将请求拦截下来,修改参数名。但请求也根本未到达这一层。不过请求的时候,程序是有反应的,给出了报错信息:
java.lang.IllegalArgumentException: Invalid character found in the request target [/?skey=fb5e8ea9249c4ac19dad5e2a341e09ce&filter[]=A3011300500,eq,35041&filter[]=A3001200400,ge,2021-01-01 00:00&filter[]=A3001200400,le,2021-02-01 00:00&satisfy=AND ]. The valid characters are defined in RFC 7230 and RFC 3986
at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:494) ~[tomcat-embed-core-9.0.60.jar:9.0.60]
- 1
- 2
估计是自带的tomcat版本是9,会拦截掉特殊字符。解决方法是将tomcat的配置注册到springboot启动类中:
import org.apache.catalina.connector.Connector;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TomcatConfig {
@Bean
public TomcatServletWebServerFactory webServerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.addConnectorCustomizers((Connector connector) -> {
connector.setProperty("relaxedPathChars", "\"<>[\\]^`{|}");
connector.setProperty("relaxedQueryChars", "\"<>[\\]^`{|}");
});
return factory;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18