是否可以在web.xml中使用基于配置文件的过滤器

时间:2012-08-02 09:51:20

标签: java servlets spring-security web.xml

是否可以在web.xml中使用基于配置文件的过滤器?例如

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
         <init-param>
            <param-name>spring.profiles.active</param-name>
            <param-value>secured</param-value>
        </init-param>
    </filter>

我知道这对于servlet是可行的,但我似乎无法让它适用于过滤器。

谢谢

1 个答案:

答案 0 :(得分:4)

原始答案

过滤器使用从ContextLoaderListener加载的ApplicationContext,因此不使用过滤器的<init-param>。相反,您将需要使用激活ContextLoaderListener的配置文件的方法之一。一种方法是使用如下所示的a:

<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>secured</param-value>
</context-param>

跟进

根据评论跟进。由于web.xml将始终加载始终尝试加载其委托的DelegatingFilterProxy,因此无法使用Spring配置文件省略Filter。如果代表丢失,您将收到错误。相反,您可以创建一个禁用Spring Security的配置文件,如下所示:

<b:beans profile="default,secured">
  <http auto-config="true" use-expressions="true">
    <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
  </http>
  <authentication-manager>
    <authentication-provider>
      <user-service>
        <user name="user" password="password" authorities="ROLE_USER" />
      </user-service>
    </authentication-provider>
  </authentication-manager>
</b:beans>
<b:beans profile="insecure">
  <http security="none" />
</b:beans>

然后,您可以通过激活web.xml中的不安全配置文件来禁用Spring Security,如下所示。

<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>insecure</param-value>
</context-param>

如果您没有使用Spring Security,则可以通过创建一个过滤器来禁用过滤器,该过滤器不执行任何操作,只是继续将FilterChain置于禁用的配置文件中。例如:

public class DoFilterChainFilter implements Filter {
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        chain.doFilter(request, response);
    }
    public void destroy() { }
    public void init(FilterConfig filterConfig) throws ServletException { }
}
相关问题