SpringBoot embedded Tomcat, to solve the problem of dealing with illegal character parameters

the environmental background of the problems and what methods you have tried

Tomcat added validation for http headers after 7.0.73, 8.0.39, 8.5.7.

specifically, some rules have been added to limit the normalization of HTTP headers.

org.apache.tomcat.util.http.parser.HttpParser-sharpIS_NOT_REQUEST_TARGET [] defines a bunch of not request target

if(IS_CONTROL[i] || i > 127 || i == 32 || i == 34 || i == 35 || i == 60 || i == 62 || i == 92 || i == 94 || i == 96 || i == 123 || i == 124 || i == 125) {
                IS_NOT_REQUEST_TARGET[i] = true;
            }

I don"t really want to use POST, and because it is embedded Tomcat, I still want to modify the configuration of springboot for the convenience of development.
I just looked at the configuration related to server.tomcat.*, but I haven"t found a solution to the problem of illegal characters-sharp-sharp-sharp problem description

.

related codes

/ / Please paste the code text below (do not replace the code with pictures)
< parent >

    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <!--<version>1.5.14.RELEASE</version>-->
    <version>2.0.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

what result do you expect? What is the error message actually seen?

request URL: http://localhost/xxx/ Chinese parameters

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

many people have given solutions, but they still feel inflexible and still want to configure them through springboot.

Apr.11,2021

just encode this "Chinese parameter".

for get requests
if your request is in Chinese with the get, parameter, for example, on the name= page, use encodeURIComponent (encodeURIComponent (url)), to re-encode the url as follows:
the front end uses encodeURIComponent (encodeURIComponent (url)), to re-encode the request.
backend: get the parameter value and decode it using URLDecoder.decode (s, "UTF-8"). This method is really feasible, and the respondent has always used it in the actual project.
give an example:
Front end:

<script type="text/javascript">
   var url = "/xxx/param/test";
     
     var name = "";
     
     name = encodeURIComponent(name);
     name = encodeURIComponent(name);//
     alert(name);
     url = url + "?name="+name;
     window.location.href = url;
     </script>

backend:

@Controller
@RequestMapping(value="/param")
public class ParamController extends BaseController<ParamEntity> {
    /**
     * @throws UnsupportedEncodingException 
     * 
     */
    @RequestMapping(value="/test",method=RequestMethod.GET)
    public String test(@RequestParam("name") String name) throws UnsupportedEncodingException{
        name = URLDecoder.decode(name, "UTF-8");//
        System.out.println(name);
        return "index";
    }
}

Note: the get request has Chinese parameters, and you can specify which encoding rules the container uses to decode the submitted parameters (someone replied to use this method, that is, to modify the URIEncoding parameter in the Connector in the tomcat configuration file), but this is not recommended. Secondary encoding is recommended.
Why do I need secondary coding? You can read the following blog posts:
eURIComponent Encoding twice


versions after springboot 2.0

import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;

/**
 * Created on 2019/2/18 17:41.
 *
 * @author Ethan
 * 

* java.lang.IllegalArgumentException: * Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986. *

*/ @Component public class PortalTomcatWebServerCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> { @Override public void customize(TomcatServletWebServerFactory factory) { factory.addConnectorCustomizers(connector -> connector.setAttribute("relaxedQueryChars", "{}[]|")); } }
Menu