2012-12-10 93 views
3

我下面这个教程: http://www.mkyong.com/spring-security/spring-security-hello-world-example/Spring Security的过滤器有多个URL拦截映射

spring-security-xml

<http auto-config="true"> 
    <intercept-url pattern="/welcome*" access="ROLE_USER" /> 
</http> 

而在web.xml中,我们必须定义实际滤波器

<!-- Spring Security --> 
<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class> 
       org.springframework.web.filter.DelegatingFilterProxy 
      </filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

所以我没有得到这个,我们在两个地方将截取映射到2个URL。到/welcome*/*。为什么我们需要这两个?我在这里错过了什么吗?

回答

9

DelegatingFilterProxy不是Spring Security类。它来自Spring Web包。

标准Servlet的代理2.3过滤器,委托给 实现Filter接口的Spring管理bean。在web.xml中支持 “targetBeanName”过滤器init-param,在Spring应用程序上下文中指定目标bean的名称 。

当您使用

<http auto-config="true"> 

</http> 

春季安全创建(隐含的)与名springSecurityFilterChain(这就是为什么你在web.xml<filter-name>springSecurityFilterChain</filter-name>)和所有请求(/*)豆,它处理的(Spring Security中)。

然后你配置Spring Security并给它更具体的URL(/*welcome)。

<intercept-url pattern="/welcome*" access="ROLE_USER" /> 

这就像说:

  • 所有URL请求(/*)应该由Spring Security的调查
  • 当URL匹配/welcome*校长应该有ROLE_USER作用。

如果您的应用程序需要更高级的安全处理,您可以自己创建该过滤器链接bean并手动配置所有过滤器。

实施例:

<!-- Filter Chain --> 
<bean id="springSecurityFilterChain" 
     class="org.springframework.security.web.FilterChainProxy"> 
    <constructor-arg> 
     <list> 
      <sec:filter-chain pattern="/favicon.ico" 
           filters="none"/> 

      <sec:filter-chain pattern="/img/**" 
           filters="none"/> 

      <sec:filter-chain pattern="/**" 
       filters="bannedIPsFilter, <!-- custom filter --> 
         channelProcessingFilter, 
         securityContextPersistenceFilter, 
         concurrentSessionFilter, 
         logoutFilter, 
         secondAuthenticationFilter, <!-- custom filter --> 
         openIDAuthenticationFilter, 
         usernamePasswordAuthenticationFilter, 
         anonymousAuthenticationFilter, 
         captchaFilter, <!-- custom filter --> 
         sessionManagementFilter, 
         exceptionTranslationFilter, 
         filterSecurityInterceptor, 
         switchUserProcessingFilter" 
        /> 
     </list> 
    </constructor-arg> 
</bean> 
+1

好的和彻底的答案 - 必须对OP有帮助。 –

+0

谢谢你的好话! –

+0

很棒的回答。当一个URL匹配多个过滤器链映射时会发生什么。第一个是唯一使用的吗? –

0

springSecurityFilterChain是该过滤器链中的所有弹簧安全过滤器的外观。它在web.xml中被注册为独立的servlet过滤器。

/welcome* - 是特定于spring-security的“内部”过滤器的配置,它不存在于web.xml中,并且servlet容器不知道它的任何内容。