1. 概述
Spring Security 提供了几种将请求模式配置为不安全或允许所有访问的机制. 取决于这些机制中的哪一种 - 这可能意味着根本不在该路径上运行安全过滤器链, 或者运行过滤器链并允许访问
2. access="permitAll"
使用 access ="permitAll" 设置元素将配置授权, 以便在该特定路径上允许所有请求:
<intercept-url pattern="/login*" access="permitAll" />
或者, 通过 Java 配置:
http.authorizeRequests().antMatchers("/login*").permitAll();
这是在不禁用安全过滤器的情况下实现的 - 这些过滤器仍在运行, 因此任何与 Spring Security 相关的功能仍然可用.
3. filters="none"
这是 Spring 3.1 之前的功能, 已在 Spring 3.1 中弃用并替换.
filters 属性完全在该特定请求路径上禁用 Spring Security 过滤器链:
<intercept-url pattern="/login*" filters="none" />
当请求的处理需要 Spring Security 的某些功能时, 这可能会导致问题.
由于这是一个不推荐使用的功能 Spring 版本高于 3.0, 使用它与 Spring 3.1 将导致启动时的运行时异常:
- SEVERE: Context initialization failed
- org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
- Configuration problem: The use of "filters='none'" is no longer supported.
- Please define a separate <http> element for the pattern you want to exclude
- and use the attribute "security='none'".
- Offending resource: class path resource [webSecurityConfig.xml]
- at o.s.b.f.p.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
- 4. security="none"
正如我们在上面的错误消息中看到的那样, Spring 3.1 用一个新的表达式替换 filters ="none" - security ="none".
scope 也发生了变化 - 不再在元素级别指定. Spring 3.1 允许定义多个元素 - 每个元素都有自己的安全过滤器链配置. 因此, 新的安全属性现在属于元素级别.
在实践中, 这将看起来像:
<http pattern="/resources/**" security="none"/>
或者使用 Java 配置:
Web.ignoring().antMatchers("/resources/**");
而不是旧的:
<intercept-url pattern="/resources/**" filters="none"/>
与 filters ="none" 类似, 这也将完全禁用该请求路径的安全过滤器链. 因此, 当在应用程序中处理请求时, Spring Security 功能将不可用. 对于上面的示例而言, 这不是问题, 其主要涉及提供静态资源 - 其中不进行实际处理. 但是, 如果以某种方式以编程方式处理请求 - 那么安全功能 (例如, 需求通道, 访问当前用户或调用安全方法) 将不可用.
出于同样的原因, 没有必要在已经使用 security ="none" 配置的元素上指定其他属性, 因为该请求路径是不安全的, 并且将简单地忽略属性.
或者, access ='IS_AUTHENTICATED_ANONYMOUSLY'可用于允许匿名访问.
5. 注意事项: security="none"
当使用多个元素时, 有些配置了 security ="none", 请记住, 定义这些元素的顺序很重要. 我们希望首先使用特定的路径, 然后在最后使用通用模式.
另请注意, 如果元素未指定模式, 则默认情况下, 映射到通用匹配模式 - "/**". 所以, 这个元素必须是最后的. 如果元素的顺序不正确, 则安全过滤器链的创建将失败:
- Caused by: java.lang.IllegalArgumentException: A universal match pattern ('/**')
- is defined before other patterns in the filter chain, causing them to be ignored.
- Please check the ordering in your <security:http> namespace or FilterChainProxy bean configuration
- at o.s.s.c.h.DefaultFilterChainValidator.checkPathOrder(DefaultFilterChainValidator.java:49)
- at o.s.s.c.h.DefaultFilterChainValidator.validate(DefaultFilterChainValidator.java:39)
六, 结论
本文讨论了允许使用 Spring Security 访问路径的选项 - 重点关注 filters ="none",security ="none" 和 access ="permitAll" 之间的差异.
像往常一样, 这些例子可以在 GitHub 上找到.
来源: https://www.cnblogs.com/xjknight/p/10892027.html