日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

springboot解決Invalid character found in the request target 異常

作者:融極 更新時間: 2022-07-18 編程語言

概述

現象

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 異常


原文鏈接:https://blog.csdn.net/tianzhonghaoqing/article/details/125806385

欄目分類
最近更新