Invalid character found in the request target in spring boot

后端 未结 6 702
自闭症患者
自闭症患者 2020-12-09 08:27

My application is started with java -jar with version 1.5.6.RELEASE of spring boot.
The content of one of my request has the character \"{\".When it is sended to serve

相关标签:
6条回答
  • 2020-12-09 09:03

    you will start your Spring boot app like this

    $ java -jar -Dtomcat.util.http.parser.HttpParser.requestTargetAllow=|{}
     demo-0.0.1-SNAPSHOT.jar
    

    or encode uri like this

    document.location = restUrl + "/print?reportParams= " + encodeURI(JSON.stringify(jsonData));
    
    0 讨论(0)
  • 2020-12-09 09:05

    Easy way just add this code in your main class

    System.setProperty("tomcat.util.http.parser.HttpParser.requestTargetAllow", "{}");

    0 讨论(0)
  • 2020-12-09 09:07

    According to https://tomcat.apache.org/tomcat-8.5-doc/config/systemprops.html, requestTargetAllow is deprecated. For me, the other solutions presented here did not work either. According to the Tomcat documentation I found a way to set the property relaxedQueryChars instead:

    @Bean
    public ConfigurableServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                connector.setProperty("relaxedQueryChars", "|{}[]");
            }
        });
        return factory;
    }
    
    0 讨论(0)
  • 2020-12-09 09:08

    For SpringBoot 1.5.17.RELEASE. The below code worked for me.

    @Configuration
    public class ServerConfig {
    
        @Bean
        public EmbeddedServletContainerFactory webServerFactory() {
            TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
            factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
                @Override
                public void customize(Connector connector) {
                    connector.setProperty("relaxedQueryChars", "|{}[]");
                }
            });
            return factory;
        }
    
    }
    
    0 讨论(0)
  • 2020-12-09 09:20

    For a Spring Boot (2.3) application, just add to properties file:

    server.tomcat.relaxed-query-chars=|,{,},[,]
    

    There is also the following key: server.tomcat.relaxed-path-chars

    0 讨论(0)
  • 2020-12-09 09:20

    Encode it to Base 64 string at request end and decode it at the controller end.


    Encode

    btoa(string)


    Decode:

    String arr = request.getParameter();
    arr = new String(Base64.getDecoder().decode(arr))
    
    0 讨论(0)
提交回复
热议问题