2
votes

There is a Zuul application that is being run by Spring-boot-2.1.4.RELEASE.

When I request as http://192.168.10.84:8080/app/rest/micro1/inquiry/printCarInquiryList/1?param={%22storeHouseId%22:%22%22,%22storeHouseName%22:%22%22,%22storeHouseType%22:%22%22,%22plaqueSeries%22:%22%22,%22productionYearFrom%22:%22-1%22,%22productionYearTo%22:%22-1%22,%22overallInquery%22:false,%22edited%22:false,%22confirmerCount%22:%22-1%22,%22reportStoreHouseType%22:%22-1%22,%22plaqueNumber%22:%22%22,%22motorNumber%22:%22%22,%22motorNumberOperator%22:%227%22,%22chassisNumber%22:%22%22,%22chassisNumberOperator%22:%227%22,%22storeHouseShowType%22:%221%22,%22havePlaque%22:%22-1%22,%22isFilterByStoreHouseStatus%22:true,%22storeHouseList%22:[%221518691%22],%22goodsStatus%22:null,%22statusId%22:%22-1%22,%22goodsDeleted%22:null,%22formType%22:1,%22searchFilter%22:%22%22,%22order%22:%22%20e.samapelItem.id%20%22,%22pageNumber%22:0,%22pageSize%22:%227%22,%22plaqueType%22:%22-1%22}

the following exception is raised:

Caused by: java.net.URISyntaxException: Illegal character in query at index 95: http://192.168.10.84:8080/app/rest/micro1/inquiry/printCarInquiryList/1?param={%id%22:%22%22,%22storeHouseName%22:%22%22,%22storeHouseType%22:%22%22,%22plaqueSeries%22:%22%22,%22productionYearFrom%22:%22-1%22,%22productionYearTo%22:%22-1%22,%22overallInquery%22:false,%22edited%22:false,%22confirmerCount%22:%22-1%22,%22reportStoreHouseType%22:%22-1%22,%22plaqueNumber%22:%22%22,%22motorNumber%22:%22%22,%22motorNumberOperator%22:%227%22,%22chassisNumber%22:%22%22,%22chassisNumberOperator%22:%227%22,%22storeHouseShowType%22:%221%22,%22havePlaque%22:%22-1%22,%22isFilterByStoreHouseStatus%22:true,%22storeHouseList%22:[%221518691%22],%22goodsStatus%22:null,%22statusId%22:%22-1%22,%22goodsDeleted%22:null,%22formType%22:1,%22searchFilter%22:%22%22,%22order%22:%22%20e.samapelItem.id%20%22,%22pageNumber%22:0,%22pageSize%22:%227%22,%22plaqueType%22:%22-1%22} at java.net.URI$Parser.fail(URI.java:2848) ~[na:1.8.0_181] at java.net.URI$Parser.checkChars(URI.java:3021) ~[na:1.8.0_181] at java.net.URI$Parser.parseHierarchical(URI.java:3111) ~[na:1.8.0_181] at java.net.URI$Parser.parse(URI.java:3053) ~[na:1.8.0_181] at java.net.URI.(URI.java:588) ~[na:1.8.0_181] at java.net.URI.create(URI.java:850) ~[na:1.8.0_181]

whereas there is the following configuration:

@Bean
    public ConfigurableServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                connector.setProperty("relaxedQueryChars", "|{}[]");
            }
        });
        return factory;
    }

where is wrong?

2
Configuring Tomcat with custom relaxedQueryChars has no effect on what characters are accepted by URI.create. It's URI.create that's failing. It looks to me like you haven't encoded your URI correctly.Andy Wilkinson
How do I fix the problem?reza ramezani matin
What's the query string supposed to be in its raw, unencoded form?Andy Wilkinson
@AndyWilkinson I do not undrestand your mean.reza ramezani matin
@AndyWilkinson I have changed the url to simple url like http://192.168.10.84:8080/app/rest/micro1/inquiry/printCarInquiryList/1?param={id:1}, again same exception is raised, the problem is character {reza ramezani matin

2 Answers

2
votes

The standard way to solve the problem is to decode the some characters that are sensitive such as { and so on, but when there is a application that has near to 1000 JSP files in which these characters are not encoded, this way is either impossible or hard to apply in all points in which these characters are used. Consequently the problem is solved by ZUUL Filter like this:

@Component
public class CharacterEncodingZuulFilter extends ZuulFilter {


    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 10000;
    }

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();

        Map<String, String[]> qp = new HashMap<>();
        String s = "";
        try {
            s = ctx.getRequest().getQueryString().replaceAll("\\{", URLEncoder.encode("{", "UTF-8"));
            s = s.replaceAll("}", URLEncoder.encode("}", "UTF-8"));
            s = s.replaceAll("\\[", URLEncoder.encode("[", "UTF-8"));
            s = s.replaceAll("]", URLEncoder.encode("]", "UTF-8"));

            HttpServletRequest request = ctx.getRequest();

            CustomRequestWrapper wrapped = new CustomRequestWrapper(request, qp, s);

            ctx.setRequest(wrapped);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return null;
    }
}
0
votes

You need to encode the { and } characters in the query portion of the URI. They should be replaced with %7B and %7D respectively.