对于最新的稳定版本,请使用 Spring Security 6.3.1! |
对于最新的稳定版本,请使用 Spring Security 6.3.1! |
|
本节以 Servlet 体系结构和实现为基础,深入探讨授权在基于 Servlet 的应用程序中的工作方式。
FilterSecurityInterceptor
为实例提供授权。
它作为安全过滤器之一插入到 FilterChainProxy 中。HttpServletRequest
下图显示了 :FilterSecurityInterceptor
从 SecurityContextHolder 获取身份验证。从 、 和 创建一个 FilterInvocation
,并将其传递给 .它传递 to 来获取 s。它将 、 和 s 传递给 。如果授权被拒绝,则抛出 an。
在本例中,ExceptionTranslationFilter
处理 .如果授予访问权限,则继续使用 FilterChain
,从而允许应用程序正常处理。FilterSecurityInterceptor
FilterSecurityInterceptor
HttpServletRequest
HttpServletResponse
FilterChain
FilterSecurityInterceptor
FilterInvocation
SecurityMetadataSource
ConfigAttribute
Authentication
FilterInvocation
ConfigAttribute
AccessDecisionManager
AccessDeniedException
AccessDeniedException
FilterSecurityInterceptor
默认情况下,Spring Security 的授权要求对所有请求进行身份验证。 以下列表显示了显式配置:
我们可以通过按优先级顺序添加更多规则来配置 Spring Security 以具有不同的规则:
-
Java
-
XML
-
Kotlin
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
// ...
.authorizeRequests(authorize -> authorize (1)
.requestMatchers("/resources/**", "/signup", "/about").permitAll() (2)
.requestMatchers("/admin/**").hasRole("ADMIN") (3)
.requestMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')") (4)
.anyRequest().denyAll() (5)
);
return http.build();
}
<http> (1)
<!-- ... -->
(2)
<intercept-url pattern="/resources/**" access="permitAll"/>
<intercept-url pattern="/signup" access="permitAll"/>
<intercept-url pattern="/about" access="permitAll"/>
<intercept-url pattern="/admin/**" access="hasRole('ADMIN')"/> (3)
<intercept-url pattern="/db/**" access="hasRole('ADMIN') and hasRole('DBA')"/> (4)
<intercept-url pattern="/**" access="denyAll"/> (5)
</http>
@Bean
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
http {
authorizeRequests { (1)
authorize("/resources/**", permitAll) (2)
authorize("/signup", permitAll)
authorize("/about", permitAll)
authorize("/admin/**", hasRole("ADMIN")) (3)
authorize("/db/**", "hasRole('ADMIN') and hasRole('DBA')") (4)
authorize(anyRequest, denyAll) (5)
}
}
return http.build()
}
1 | 指定了多个授权规则。 每条规则都按其声明的顺序进行考虑。 |
2 | 我们指定了任何用户都可以访问的多个 URL 模式。 具体而言,如果 URL 以“/resources/”开头、等于“/signup”或等于“/about”,则任何用户都可以访问请求。 |
3 | 任何以“/admin/”开头的 URL 都将仅限于角色为“ROLE_ADMIN”的用户。
您会注意到,由于我们正在调用该方法,因此我们不需要指定“ROLE_”前缀。hasRole |
4 | 任何以“/db/”开头的 URL 都要求用户同时具有“ROLE_ADMIN”和“ROLE_DBA”。
您会注意到,由于我们使用的是表达式,因此我们不需要指定“ROLE_”前缀。hasRole |
5 | 任何尚未匹配的 URL 都将被拒绝访问。 如果您不想意外忘记更新授权规则,这是一个很好的策略。 |
|
1 | 指定了多个授权规则。 每条规则都按其声明的顺序进行考虑。 |
2 | 我们指定了任何用户都可以访问的多个 URL 模式。 具体而言,如果 URL 以“/resources/”开头、等于“/signup”或等于“/about”,则任何用户都可以访问请求。 |
3 | 任何以“/admin/”开头的 URL 都将仅限于角色为“ROLE_ADMIN”的用户。
您会注意到,由于我们正在调用该方法,因此我们不需要指定“ROLE_”前缀。hasRole |
4 | 任何以“/db/”开头的 URL 都要求用户同时具有“ROLE_ADMIN”和“ROLE_DBA”。
您会注意到,由于我们使用的是表达式,因此我们不需要指定“ROLE_”前缀。hasRole |
5 | 任何尚未匹配的 URL 都将被拒绝访问。 如果您不想意外忘记更新授权规则,这是一个很好的策略。 |
使用 Dispatcher 类型配置 FilterSecurityInterceptor
默认情况下,适用于每个请求。
这意味着,如果从已过滤的请求中调度请求,则将对已调度的请求执行相同的授权检查。
在某些情况下,您可能不希望对某些调度程序类型应用授权:FilterSecurityInterceptor
FilterSecurityInterceptor
-
Java
-
XML
@Bean
SecurityFilterChain web(HttpSecurity http) throws Exception {
http
.authorizeRequests((authorize) -> authorize
.dispatcherTypeMatchers(DispatcherType.ASYNC, DispatcherType.ERROR).permitAll()
.anyRequest.authenticated()
)
// ...
return http.build();
}
<http auto-config="true">
<intercept-url request-matcher-ref="dispatcherTypeMatcher" access="permitAll" />
<intercept-url pattern="/**" access="authenticated"/>
</http>
<b:bean id="dispatcherTypeMatcher" class="org.springframework.security.web.util.matcher.DispatcherTypeRequestMatcher">
<b:constructor-arg value="ASYNC"/>
<b:constructor-arg value="ERROR"/>
</b:bean>