springboot中出现Invalid character found in the request target异常的解决方法分享

乔欣 SpringBoot 发布时间:2023-03-12 21:28:11 阅读数:12318 1
今天SpringBoot出现"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属性 

Invalid character found in the request target的解决方法


//在启动类中
//添加ConfigurableServletWebServerFactory Bean对象。

@SpringBootApplication
@EnableScheduling
@EnableFeignClients
public class TestClassApplication {
 public static void main(String[] args) {
 ApplicationContext context = SpringApplication.run(TestClassApplication.class, args);
 SpringContextUtil.setApplicationContext(context);
 }

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

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/SpringBoot/202303/6016.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者