簡體   English   中英

是否可以在web.xml中使用基於配置文件的過濾器

[英]Is it possible to use a profile based filter in your 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,后者始終嘗試加載其委托。 如果缺少代表,您將收到一個錯誤。 相反,您可以創建一個禁用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 { }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM