方法安全性
<method-security>
此元素是为Spring Security bean上的方法添加安全支持的主要手段。可以通过使用注解(在接口或类级别定义)或通过定义一组切点来保护方法。
<method-security> 属性
-
pre-post-enabled 启用 Spring Security 的预处理和后处理调用注解(@PreFilter, @PreAuthorize, @PostFilter, @PostAuthorize)对此应用程序上下文。默认值为 "true"。
-
secured-enabled 启用 Spring Security 的 @Secured 注解对此应用程序上下文。默认值为 "false"。
-
jsr250-enabled 启用 JSR-250 授权注解(@RolesAllowed, @PermitAll, @DenyAll)对此应用程序上下文。默认值为 "false"。
-
mode 如果设置为 "aspectj",则使用 AspectJ 拦截方法调用。
-
proxy-target-class 如果为 true,则使用基于类的代理而不是基于接口的代理。默认值为 "false"。
-
security-context-holder-strategy-ref 指定在检索
SecurityContext
时使用的SecurityContextHolderStrategy
。默认值为SecurityContextHolder.getContextHolderStrategy()
返回的值。 -
observation-registry-ref 对用于
FilterChain
及相关组件的ObservationRegistry
的引用
<method-security> 的子元素
<global-method-security>
此元素是为主要的 Spring Security bean 的方法添加安全支持的主要手段。可以通过使用注解(在接口或类级别定义)或通过定义一组使用 AspectJ 语法的切面来保护方法。
<global-method-security> 属性
-
access-decision-manager-ref 方法安全使用与web安全相同的
AccessDecisionManager
配置,但可以使用此属性进行覆盖。默认情况下,使用带有RoleVoter和AuthenticatedVoter的AffirmativeBased实现。 -
authentication-manager-ref 一个指向应该用于方法安全的
AuthenticationManager
的引用。 -
jsr250-annotations 指定是否使用JSR-250风格的属性(例如"RolesAllowed")。这将需要在类路径上包含javax.annotation.security类。将此设置为true还会向
AccessDecisionManager
添加一个Jsr250Voter
,因此如果你使用自定义实现并希望使用这些注解,则需要确保这样做。 -
metadata-source-ref 可以提供一个外部的
MethodSecurityMetadataSource
实例,它将优先于其他来源(如默认注解)。 -
mode 此属性可以设置为"aspectj",以指定应使用AspectJ而不是默认的Spring AOP。受保护的方法必须使用来自
spring-security-aspects
模块的AnnotationSecurityAspect
进行编织。
需要注意的是,AspectJ 遵循 Java 的规则,即接口上的注解不会被继承。这意味着在接口上定义了 Security 注解的方法将不会受到保护。相反,当你使用 AspectJ 时,必须将 Security 注解放在类上。
-
order 允许为方法安全拦截器设置建议的“order”。
-
pre-post-annotations 指定是否应为此应用程序上下文启用 Spring Security 的预处理和后处理调用注解(@PreFilter, @PreAuthorize, @PostFilter, @PostAuthorize)。默认为 "disabled"。
-
proxy-target-class 如果为 true,则使用基于类的代理而不是基于接口的代理。
-
run-as-manager-ref 对可选的
RunAsManager
实现的引用,该实现将由配置的MethodSecurityInterceptor
使用。 -
secured-annotations 指定是否应为此应用程序上下文启用 Spring Security 的 @Secured 注解。默认为 "disabled"。
<global-method-security> 的子元素
<after-invocation-provider>
此元素可用于装饰 AfterInvocationProvider
,以便由 <global-method-security>
命名空间维护的安全拦截器使用。您可以在 global-method-security
元素中定义零个或多个此类元素,每个元素都有一个 ref
属性,指向应用程序上下文中的 AfterInvocationProvider
bean 实例。
<after-invocation-provider> 的父元素
<after-invocation-provider> 属性
- ref 定义对实现
AfterInvocationProvider
的 Spring bean 的引用。
<pre-post-annotation-handling>
允许完全替换处理 Spring Security 的预处理和后处理注解(@PreFilter、@PreAuthorize、@PostFilter、@PostAuthorize)的默认基于表达式的机制。仅在启用了这些注解时适用。
<pre-post-annotation-handling> 的父元素
<pre-post-annotation-handling> 的子元素
<invocation-attribute-factory>
定义用于从标注的方法生成前置和后置调用元数据的 PrePostInvocationAttributeFactory 实例。
<invocation-attribute-factory> 的父元素
<invocation-attribute-factory> 属性
- ref 定义对 Spring bean Id 的引用。
<post-invocation-advice>
自定义 PostInvocationAdviceProvider
,将 ref 作为 PostInvocationAuthorizationAdvice
用于 <pre-post-annotation-handling> 元素。
<post-invocation-advice> 的父元素
<post-invocation-advice> 属性
- ref 定义对 Spring bean Id 的引用。
<pre-invocation-advice>
使用 ref 作为 <pre-post-annotation-handling> 元素的 PreInvocationAuthorizationAdviceVoter
,来自定义 PreInvocationAuthorizationAdviceVoter
。
<pre-invocation-advice> 的父元素
<pre-invocation-advice> 属性
- ref 定义对 Spring bean Id 的引用。
使用 {#nsa-protect-pointcut} 保护方法
<protect-pointcut>
你可以使用 <protect-pointcut>
元素在你的服务层中为整组方法和接口定义横切的安全约束,而不是通过 @Secured
注解在单个方法或类的基础上定义安全属性。你可以在 命名空间介绍 中找到一个示例。
<protect-pointcut> 的父元素
<protect-pointcut> 属性
-
access 访问配置属性列表,适用于与切入点匹配的所有方法,例如 "ROLE_A,ROLE_B"
-
expression 一个 AspectJ 表达式,包括
execution
关键字。例如,execution(int com.foo.TargetObject.countLength(String))
。
<intercept-methods>
可以在 bean 定义中使用,以向 bean 添加安全拦截器,并为 bean 的方法设置访问配置属性。
<intercept-methods> 属性
-
use-authorization-manager 使用 AuthorizationManager API 而不是 AccessDecisionManager(默认为 true)
-
authorization-manager-ref 可选的 AuthorizationManager bean ID,用于替代默认值(优先于 use-authorization-manager)
-
access-decision-manager-ref 可选的 AccessDecisionManager bean ID,用于创建的方法安全拦截器。
<intercept-methods> 的子元素
<method-security-metadata-source>
创建一个 MethodSecurityMetadataSource
实例
<method-security-metadata-source> 属性
-
id 一个 bean 标识符,用于在上下文的其他地方引用该 bean。
-
use-expressions 启用在
<intercept-url>
元素的 'access' 属性中使用表达式,而不是传统的配置属性列表。默认值为 'false'。如果启用,则每个属性应包含一个布尔表达式。如果表达式评估结果为 'true',则将授予访问权限。
<method-security-metadata-source> 的子元素
<protect>
定义一个受保护的方法及其适用的访问控制配置属性。我们强烈建议您不要将 "protect" 声明与任何通过 "global-method-security" 提供的服务混合使用。
<protect> 的父元素
<protect> 属性
-
access 访问配置属性列表,适用于该方法,例如 "ROLE_A,ROLE_B"。
-
method 方法名称