java - 由 : java.net.URISyntaxException 引起:索引 111 处的查询中存在非法字符:由于 URI 中的 {} 导致错误

标签 java spring-boot netflix-zuul

我发现该错误是由于 URI 中的大括号下方造成的 {2019-06-11T00:00:00%20TO%202019-06-11T23:59:59}

我还尝试添加 FilterRegistrationBean,但没有成功

@Bean
public FilterRegistrationBean filterRegistrationBean() {
    FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
    characterEncodingFilter.setForceEncoding(false);
    characterEncodingFilter.setEncoding("UTF-8");
    characterEncodingFilter.setForceRequestEncoding(false);
    characterEncodingFilter.setForceResponseEncoding(false);
    registrationBean.setFilter(characterEncodingFilter);
    registrationBean.addUrlPatterns("/*");
    return registrationBean;
}




private static class EmbeddedTomcatCustomizer implements 
        WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

    @Override
    public void customize(TomcatServletWebServerFactory factory) {
        factory.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
            connector.setAttribute("relaxedPathChars", "<>[\\]^`:{|}");
            connector.setAttribute("relaxedQueryChars", "<>[\\]^`:{|}");
        });
    }
}

最佳答案

最安全的选择是使用 percent encoding{} 进行编码如 %7B%7D。根据RFC-1738这两个字符被认为是不安全的,在处理时可能会导致问题:

Unsafe:

Characters can be unsafe for a number of reasons. The space character is unsafe because significant spaces may disappear and insignificant spaces may be introduced when URLs are transcribed or typeset or subjected to the treatment of word-processing programs. The characters "<" and ">" are unsafe because they are used as the delimiters around URLs in free text; the quote mark (""") is used to delimit URLs in some systems. The character "#" is unsafe and should always be encoded because it is used in World Wide Web and in other systems to delimit a URL from a fragment/anchor identifier that might follow it. The character "%" is unsafe because it is used for encodings of other characters. Other characters are unsafe because gateways and other transport agents are known to sometimes modify such characters. These characters are "{", "}", "|", "", "^", "~", "[", "]", and "`".

All unsafe characters must always be encoded within a URL. For example, the character "#" must be encoded within URLs even in systems that do not normally deal with fragment or anchor identifiers, so that if the URL is copied into another system that does use them, it will not be necessary to change the URL encoding.

关于java - 由 : java.net.URISyntaxException 引起:索引 111 处的查询中存在非法字符:由于 URI 中的 {} 导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56559768/

相关文章:

java - Eclipse 中的 "compiler compliance level"是什么?

java - Hibernate 的 createCriteria() 是否清理输入?

java - Android Studio - 权限拒绝,需要 android.permission.RECEIVE_SMS 错误

java - 遍历 ListView 中的所有 TextView

java - Spring Boot-MVC-RestTemplate : Where to initialize a RestTemplate for a MVC application to consume remote RESTful services

gradle - 将 sourceSet 和依赖项添加到从 main 继承的 gradle 构建脚本中

spring-boot - 如何配置 Zuul 网关(监听两个端口)以拒绝基于端口和 url 组合的请求?

spring boot 处理大请求体 - 通过多线程处理批量

netflix-zuul - Spring Cloud Brixton.RELEASE zuul

java - 在 Spring Cloud 应用程序中实现重试