FilterRegistrationBean的URL模式不起作用。

huangapple go评论55阅读模式
英文:

FilterRegistrationBean url pattern doesn't work

问题

我正在注册一个过滤器,如下所示,用于日志输出:

我想要的 URL 模式:/*/api/*

@Bean
public CommonsRequestLoggingFilter commonsRequestLoggingFilter() {
    CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();
    filter.setIncludeClientInfo(true);
    filter.setIncludeHeaders(true);
    filter.setIncludePayload(true);
    filter.setIncludeQueryString(true);
    filter.setMaxPayloadLength(1000);
    return filter;
}

@Bean
public FilterRegistrationBean filterRegistrationBean() {
    final FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    registrationBean.addUrlPatterns("/*/api/*");

    return registrationBean;
}
英文:

I am registering a filter as shown below For log output

i want url pattern : /*/api/*


@Bean
public CommonsRequestLoggingFilter commonsRequestLoggingFilter() {
    CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();
    filter.setIncludeClientInfo(true);
    filter.setIncludeHeaders(true);
    filter.setIncludePayload(true);
    filter.setIncludeQueryString(true);
    filter.setMaxPayloadLength(1000);
    return filter;
}


     @Bean
    public FilterRegistrationBean filterRegistrationBean () {
        final FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.addUrlPatterns("/*/api/*");

        return registrationBean;
    }

答案1

得分: 6

由于代码注册了一个Servlet Filter,URL模式必须符合Servlets/Filters支持的URL映射,如Servlet规范中所指定的:

> 12.2 映射规范
>
> 在Web应用程序部署描述符中,以下语法用于定义映射:
>
> - 以 / 字符开头并以 /* 后缀结尾的字符串用于路径映射。
>
> - 以 *. 前缀开头的字符串用作扩展名映射。
>
> - 空字符串(“”)是一个特殊的URL模式,与应用程序的上下文根完全匹配,即形式为 http://host:port/<context-root>/ 的请求。在这种情况下,路径信息是 /,Servlet路径和上下文路径为空字符串(““)。
>
> - 仅包含 / 字符的字符串表示应用程序的“默认” Servlet。在这种情况下,Servlet路径是请求URI减去上下文路径,路径信息为null。
>
> - 所有其他字符串仅用于精确匹配。

因此,一个URL只能有一个通配符,并且它必须是第一个或最后一个:

  • /some/path/*
  • *.ext
英文:

Since the code registers a Servlet Filter, the URL pattern must conform to the URL mappings supported by Servlets/Filters, as specified in the Servlet Specification:

> 12.2 Specification of Mappings
>
> In the Web application deployment descriptor, the following syntax is used to define mappings:
>
> - A string beginning with a / character and ending with a /* suffix is used for path mapping.
>
> - A string beginning with a *. prefix is used as an extension mapping.
>
> - The empty string ("") is a special URL pattern that exactly maps to the application's context root, i.e., requests of the form http://host:port/&lt;context-root&gt;/. In this case the path info is / and the servlet path and context path is empty string (““).
>
> - A string containing only the / character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
>
> - All other strings are used for exact matches only.

So, a URL can have only one wildcard, and it must be first or last:

  • /some/path/*
  • *.ext

huangapple
  • 本文由 发表于 2020年10月25日 15:38:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64521412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定