转载

springboot解决Invalid character found in the request target 异常

现象

Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

原因

SpringBoot 2.0.0 以上都采用内置tomcat8.0以上版本,而tomcat8.0以上版本遵从RFC规范添加了对Url的特殊字符的限制,url中只允许包含英文字母(a-zA-Z)、数字(0-9)、-_.~四个特殊字符以及保留字符( ! * ’ ( ) ; : @ & = + $ , / ? # [ ] ) (262+10+4+18=84)这84个字符,请求中出现了{}大括号或者[],所以tomcat报错。设置RelaxedQueryChars允许此字符(建议),设置requestTargetAllows选项(Tomcat 8.5中不推荐)。 根据Tomcat文档,下面提供一种方法来设置松弛的QueryChars属性。

解决方案

在启动类中添加ConfigurableServletWebServerFactory Bean对象。
@SpringBootApplication
@EnableScheduling
@EnableFeignClients
public class ZhAlarmApplication {
 public static void main(String[] args) {
 ApplicationContext context = SpringApplication.run(ZhAlarmApplication.class, args);
 SpringContextUtil.setApplicationContext(context);
 }

 @Bean
 public ConfigurableServletWebServerFactory webServerFactory() {
 TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
 factory.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> connector.setProperty("relaxedQueryChars", "|{}[]\\"));
 return factory;
 }
}

参考

spring boot 中解决 Invalid character found in the request target 异常
正文到此结束
Loading...