在Spring Security
3.0中,这是(可以理解的)常见的混淆源,因为使用
filters="none"会向的模式添加带有空过滤器列表的模式,
FilterChainProxy而使用
access属性会向的
FilterSecurityInterceptor模式添加安全性访问规则以保护URL
。
匹配过程为:
FilterChainProxy
将请求匹配到过滤器链- 如果过滤器链为非空,则请求将由
FilterSecurityInterceptor
这两个类都维护一个单独的匹配器的有序列表,它们确实按照定义它们的顺序应用,但是您需要了解,实际上在下面配置了两个不直接连接的单独的bean。
在一个简单的名称空间应用程序中,该
<http>块将单个过滤器链添加到
FilterChainProxywith模式
/**。
filters="none"您添加的任何样式都会将空的过滤器链放置在实际链之前。
在Spring Security
3.1中,这种情况已大大改善,因为您可以通过使用单独的
<http>块来配置单独的过滤器链,该块可以更直观地映射到Bean级别上实际发生的事情。该请求匹配过程也已经改善了很多,现在使用一个
RequestMatcher接口的一切。在组态
<http>块时,也可以使用它而不是模式。
因此,您最好的选择可能是升级。然后,您可以实现,以
RequestMatcher检查要查找的参数是否存在,例如
MyParamRequestMatcher,然后使用:
<http request-matcher-ref="myParamMatcher" security="none" /><bean:bean id="myParamMatcher" /><http> <!-- Define the default filter chain configuration here --></http>
请注意,使用URL模式对参数进行匹配通常不太安全,因为通过重新排序URL,添加虚假模式等很容易绕过。您的情况可能还可以,因为带有参数的版本允许不安全的访问,并且您有需要对其他情况进行身份验证的模式。
如果您希望使用3.0,最好的选择是避免使用
filters="none“(
isAnonymous()代替),并且可能使用正则表达式匹配而不是ant路径,以便您可以更轻松地匹配查询字符串。我再次重复定义此规则的规则几乎可以肯定地绕过这种方式,因此不要依赖它们来提高安全性,并确保默认情况下您是安全的。
更新:
作为对我使用正则表达式匹配和的建议的测试
permitAll,如果我像这样修改Spring Security的3.0.x分支中的“教程”示例应用程序,则:
<http use-expressions="true" path-type="regex"> <intercept-url pattern="A/secure/extreme/.*Z" access="hasRole('ROLE_SUPERVISOR')"/> <intercept-url pattern="A/secure/index.jsp?param=valueZ" access="permitAll" /> <intercept-url pattern="A/secure/.*Z" access="isAuthenticated()" /> <intercept-url pattern="/.*" access="permitAll" /> ...</http>然后,我得到了预期的行为:
[DEBUG,FilterChainProxy] Candidate is: '/secure/index.jsp?param=value'; pattern is /.*; matched=true[DEBUG,FilterChainProxy] /secure/index.jsp?param=value at position 1 of 12 in additional filter chain; firing Filter: 'ConcurrentSessionFilter'...[DEBUG,FilterChainProxy] /secure/index.jsp?param=value at position 11 of 12 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'[DEBUG,FilterChainProxy] /secure/index.jsp?param=value at position 12 of 12 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'[DEBUG,expressionbasedFilterInvocationSecuritymetadataSource] Candidate is: '/secure/index.jsp?param=value'; pattern is A/secure/extreme/.*Z; matched=false[DEBUG,expressionbasedFilterInvocationSecuritymetadataSource] Candidate is: '/secure/index.jsp?param=value'; pattern is A/secure/index.jsp?param=valueZ; matched=true[DEBUG,FilterSecurityInterceptor] Secure object: FilterInvocation: URL: /secure/index.jsp?param=value; Attributes: [permitAll]
其中显示
FilterChainProxy匹配项下的匹配项,
.*然后是
FilterSecurityInterceptor与参数匹配的确切URL。



