1

我有一个带有 Spring 3.2.5 的 Spring MVC 应用程序,它上面有一个过滤器,可以对请求进行更改。这个过滤器有一个我试图通过 web.xml 中的 init-param 设置的参数。我似乎无法让 web.xml 在我的 bean 上设置值,并且无法弄清楚它在 spring bean 中的存储位置(如果有的话)。我没有将过滤器 bean 添加到我的上下文中,而只是添加到我的 web.xml 中。

我的 web.xml:

<filter>
    <filter-name>myFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
        <description>
            ...
        </description>
        <param-name>allowMethods</param-name>
        <param-value>GET, POST</param-value>
    </init-param>
</filter>

我的过滤器代码:

公共类 MyFilter 扩展 OncePerRequestFilter {

private static final Logger logger = LoggerFactory.getLogger(MyFilter.class);

protected String allowMethods;


@Override
protected void doFilterInternal(HttpServletRequest request,
                                HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException {
        // Do filter stuff, and try printing allowMethods via the logger
}

/**
 * @return the allowMethods
 */
public String getAllowMethods()
{
    return allowMethods;
}

/**
 * @param allowMethods the allowMethods to set as a comma separated list
 * for request types.  For example, "GET" or "GET, PUT", or "GET, PUT, POST, DELETE"
 */
public void setAllowMethods(String allowMethods)
{
    logger.info("Setting CORS filter to allow cross-site scripting with methods: {}", allowMethods);
    this.allowMethods = allowMethods;
}

}

我没有看到 setter 消息,该值未存储在 Spring 保存的过滤器配置中,并且我无法覆盖任何看起来会给我想要的信息的方法(它们被声明为final)。

如何将 init-param 映射到我的过滤器。我必须使用 applicationContext 来做到这一点吗?如果是这样,使用 Spring 执行此操作的约定是什么?

谢谢

4

0 回答 0