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

标签 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 是可能的,但我似乎无法让它适用于过滤器。

谢谢

最佳答案

原始答案

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

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

跟进

根据评论进行跟进。使用 Spring 配置文件无法省略 Filter,因为 web.xml 将始终加载 DelegatingFilterProxy,而 DelegatingFilterProxy 始终尝试加载其委托(delegate)。如果委托(delegate)丢失,您将收到错误消息。相反,您可以创建一个禁用 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,您可以通过创建一个不执行任何操作的过滤器来禁用过滤器,但继续过滤器链并将其放置在禁用的配置文件中。例如:

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 { }
}

关于java - 是否可以在 web.xml 中使用基于配置文件的过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11775012/

相关文章:

java - Tomcat服务器使用Servlet时显示404错误

java - 如何发送请求并进行响应,并返回给客户端?

spring - Grails Spring Security OAuth2提供者对具有正确承载 token 的资源的请求重定向到登录

spring - 如何提供来自文件的 spring boot application.yml 属性值

Java 输出格式,缩进代码为 :

java - 如何在 ZK 中记录 UI 事件?

java - 如何在Java SWT中创建不显示的图像

Java servlet 无法接收大文件

java - 使用 java config 在单个应用程序中使用多种身份验证机制

java - 使用 Selenium Webdriver 测试 sessionStorage