此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Security 6.4.1spring-doc.cadn.net.cn

方法安全性

<方法安全性>

此元素是添加对 Spring Security bean 上保护方法的支持的主要方法。 可以通过使用 Comments(在接口或类级别定义)或定义一组切入点来保护方法。spring-doc.cadn.net.cn

<method-security> 属性

  • 启用 pre-post为此应用程序上下文启用 Spring Security 的调用前和调用后注释(@PreFilter、@PreAuthorize、@PostFilter @PostAuthorize)。 默认为 “true”。spring-doc.cadn.net.cn

  • 安全已启用为此应用程序上下文启用 Spring Security 的 @Secured 注释。 默认为 “false”。spring-doc.cadn.net.cn

  • 已启用 JSR250为此应用程序上下文启用 JSR-250 授权注释(@RolesAllowed、@PermitAll @DenyAll)。 默认为 “false”。spring-doc.cadn.net.cn

  • 代理目标类如果为 true,则将使用基于类的代理,而不是基于接口的代理。 默认为 “false”。spring-doc.cadn.net.cn

  • security-context-holder-strategy-ref (安全上下文持有者策略引用)指定检索SecurityContext时要使用的SecurityContextHolderStrategy。 默认为 SecurityContextHolder.getContextHolderStrategy() 返回的值。spring-doc.cadn.net.cn

<全局方法安全性>

此元素是添加对 Spring Security bean 上保护方法的支持的主要方法。 可以通过使用注释(在接口或类级别定义)或使用 AspectJ 语法将一组切入点定义为子元素来保护方法。spring-doc.cadn.net.cn

<global-method-security> 属性

  • 访问决策管理器引用方法安全性使用相同的AccessDecisionManager配置为 Web 安全性,但可以使用此属性覆盖此属性。 默认情况下,AffirmativeBased 实现用于 RoleVoter 和 AuthenticatedVoter。spring-doc.cadn.net.cn

  • jsr250 注释指定是否使用 JSR-250 样式属性(例如,“RolesAllowed”)。 这将需要 Classpath 上的 javax.annotation.security 类。 将此设置为 true 还会添加Jsr250VoterAccessDecisionManager,因此,如果您使用的是自定义实现并希望使用这些注解,则需要确保执行此作。spring-doc.cadn.net.cn

  • 元数据源引用外部MethodSecurityMetadataSource实例将优先于其他源(例如默认注释)。spring-doc.cadn.net.cn

  • 模式这个属性可以设置为“aspectj”,以指定应该使用 AspectJ 而不是默认的 Spring AOP。 安全方法必须与AnnotationSecurityAspectspring-security-aspects模块。spring-doc.cadn.net.cn

需要注意的是,AspectJ 遵循 Java 的规则,即接口上的注解不被继承。 这意味着在接口上定义 Security 注释的方法将不安全。 相反,在使用 AspectJ 时,您必须将 Security 注释放在类上。spring-doc.cadn.net.cn

  • pre-post-annotations 注解指定是否应为此应用程序上下文启用 Spring Security 的调用前和调用后注释(@PreFilter、@PreAuthorize、@PostFilter @PostAuthorize)。 默认为 “disabled”。spring-doc.cadn.net.cn

  • 代理目标类如果为 true,则将使用基于类的代理,而不是基于接口的代理。spring-doc.cadn.net.cn

  • 作为管理器引用运行对可选RunAsManager实现,该 API 将由配置的MethodSecurityInterceptorspring-doc.cadn.net.cn

  • 安全注释指定是否应为此应用程序上下文启用 Spring Security 的 @Secured 注释。 默认为 “disabled”。spring-doc.cadn.net.cn

<after-invocation-provider>

此元素可用于装饰AfterInvocationProvider供由<global-method-security>Namespace。 您可以在global-method-security元素,每个元素都有一个ref属性指向AfterInvocationProviderbean 实例。spring-doc.cadn.net.cn

<after-invocation-provider> 的父元素

<after-invocation-provider> 属性

<pre-post-annotation-handling>

允许完全替换用于处理 Spring Security 的调用前和调用后注释(@PreFilter、@PreAuthorize、@PostFilter @PostAuthorize)的默认基于表达式的机制。 仅当启用这些注释时适用。spring-doc.cadn.net.cn

<pre-post-annotation-handling的父元素>

<invocation-attribute-factory>

定义 PrePostInvocationAttributeFactory 实例,该实例用于从带注释的方法生成调用前和调用后元数据。spring-doc.cadn.net.cn

<invocation-attribute-factory 的父元素>

<invocation-attribute-factory> 属性

<后调用建议>

自定义PostInvocationAdviceProvider以 ref 作为PostInvocationAuthorizationAdvice对于 <pre-post-annotation-handling> 元素。spring-doc.cadn.net.cn

<post-invocation-advice 的父元素>

<post-invocation-advice> 属性

<pre-invocation-advice>

自定义PreInvocationAuthorizationAdviceVoter以 ref 作为PreInvocationAuthorizationAdviceVoter对于 <pre-post-annotation-handling> 元素。spring-doc.cadn.net.cn

<pre-invocation-advice 的父元素>

<pre-invocation-advice> 属性

使用 保护方法

<protect-pointcut>而不是在单个方法或类的基础上定义安全属性,而是使用@Secured注解中,您可以使用<protect-pointcut>元素。 您可以在 命名空间简介.spring-doc.cadn.net.cn

<protect-pointcut> 属性

  • 访问访问配置属性列表,该列表适用于与切入点匹配的所有方法,例如 “ROLE_A,ROLE_B”spring-doc.cadn.net.cn

  • 表达一个 AspectJ 表达式,包括execution关键词。 例如execution(int com.foo.TargetObject.countLength(String)).spring-doc.cadn.net.cn

<拦截方法>

可以在 Bean 定义中使用,以向 Bean 添加安全拦截器,并为 Bean 的方法设置访问配置属性spring-doc.cadn.net.cn

<intercept-methods> 属性

  • 使用授权管理器使用 AuthorizationManager API 而不是 AccessDecisionManager(默认为 true)spring-doc.cadn.net.cn

  • 授权管理器引用可选的AuthorizationManager bean ID代替默认的 (取代use-authorization-manager)spring-doc.cadn.net.cn

  • 访问决策管理器引用可选的 AccessDecisionManager bean ID,供创建的方法安全拦截器使用。spring-doc.cadn.net.cn

<intercept-methods> 的子元素

<方法安全元数据源>

创建 MethodSecurityMetadataSource 实例spring-doc.cadn.net.cn

<method-security-metadata-source> 属性

  • 使用表达式允许在 <intercept-url> 元素的 'access' 属性中使用表达式,而不是传统的配置属性列表。 默认为 'false'。 如果启用,则每个属性都应包含一个布尔表达式。 如果表达式的计算结果为 'true',则将授予访问权限。spring-doc.cadn.net.cn

<method-security-metadata-source> 的子元素

<保护>

定义受保护的方法和应用于它的访问控制配置属性。 我们强烈建议您不要将 “protect” 声明与提供的任何 “global-method-security” 服务混合使用。spring-doc.cadn.net.cn

<protect> 属性