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

方法安全性

除了在请求级别对授权进行建模外, Spring Security 还支持在方法级别进行建模。spring-doc.cadn.net.cn

您可以在应用程序中通过注释任何@Configurationclass 替换为@EnableMethodSecurity或添加<method-security>添加到任何 XML 配置文件中,如下所示:spring-doc.cadn.net.cn

@EnableMethodSecurity
@EnableMethodSecurity
<sec:method-security/>

然后,您可以立即使用@PreAuthorize,@PostAuthorize,@PreFilter@PostFilter授权方法调用,包括输入参数和返回值。spring-doc.cadn.net.cn

Method Security 还支持许多其他用例,包括 AspectJ 支持自定义注释和几个配置点。 考虑了解以下使用案例:spring-doc.cadn.net.cn

方法安全性的工作原理

Spring Security 的方法授权支持对于以下方面非常方便:spring-doc.cadn.net.cn

由于 Method Security 是使用 Spring AOP 构建的,因此您可以访问其所有表达能力,以根据需要覆盖 Spring Security 的默认值。spring-doc.cadn.net.cn

如前所述,您首先要添加@EnableMethodSecurity更改为@Configurationclass 或<sec:method-security/>在 Spring XML 配置文件中。spring-doc.cadn.net.cn

此 annotation 和 XML 元素取代@EnableGlobalMethodSecurity<sec:global-method-security/>分别。 它们提供了以下改进:spring-doc.cadn.net.cn

  1. 使用简化的AuthorizationManagerAPI 而不是元数据源、配置属性、决策管理器和投票者。 这简化了重用和定制。spring-doc.cadn.net.cn

  2. 支持直接基于 Bean 的配置,而不是要求扩展GlobalMethodSecurityConfiguration自定义 beanspring-doc.cadn.net.cn

  3. 使用原生 Spring AOP 构建,去除抽象并允许您使用 Spring AOP 构建块进行自定义spring-doc.cadn.net.cn

  4. 检查冲突的注释以确保安全配置明确spring-doc.cadn.net.cn

  5. 符合 JSR-250spring-doc.cadn.net.cn

  6. 使@PreAuthorize,@PostAuthorize,@PreFilter@PostFilter默认情况下spring-doc.cadn.net.cn

如果您正在使用@EnableGlobalMethodSecurity<global-method-security/>,这些选项现已弃用,建议您迁移。spring-doc.cadn.net.cn

方法授权是方法授权之前和方法之后授权的组合。 考虑以下列方式注释的服务 Bean:spring-doc.cadn.net.cn

@Service
public class MyCustomerService {
    @PreAuthorize("hasAuthority('permission:read')")
    @PostAuthorize("returnObject.owner == authentication.name")
    public Customer readCustomer(String id) { ... }
}
@Service
open class MyCustomerService {
    @PreAuthorize("hasAuthority('permission:read')")
    @PostAuthorize("returnObject.owner == authentication.name")
    fun readCustomer(id: String): Customer { ... }
}

MyCustomerService#readCustomer激活方法安全性时,可能如下所示:spring-doc.cadn.net.cn

方法安全性
  1. Spring AOP 调用其代理方法readCustomer.在代理的其他顾问程序中,它会调用AuthorizationManagerBeforeMethodInterceptor匹配@PreAuthorize切入点spring-doc.cadn.net.cn

  2. 拦截器调用PreAuthorizeAuthorizationManager#checkspring-doc.cadn.net.cn

  3. 授权管理器使用MethodSecurityExpressionHandler解析注释的 SPEL 表达式并构造相应的EvaluationContextMethodSecurityExpressionRoot一个Supplier<Authentication>MethodInvocation.spring-doc.cadn.net.cn

  4. 侦听器使用此上下文来评估表达式;具体来说,它写道AuthenticationSupplier并检查它是否具有permission:read在其权威集合中spring-doc.cadn.net.cn

  5. 如果评估通过,则 Spring AOP 将继续调用该方法。spring-doc.cadn.net.cn

  6. 否则,拦截器会发布一个AuthorizationDeniedEvent并抛出一个AccessDeniedExceptionExceptionTranslationFilter捕获并向响应返回 403 状态代码spring-doc.cadn.net.cn

  7. 在方法返回后, Spring AOP 会调用AuthorizationManagerAfterMethodInterceptor匹配@PostAuthorize切入点,作与上述相同,但使用PostAuthorizeAuthorizationManagerspring-doc.cadn.net.cn

  8. 如果评估通过(在本例中,返回值属于已登录用户),则处理将继续正常进行spring-doc.cadn.net.cn

  9. 否则,拦截器会发布一个AuthorizationDeniedEvent并抛出一个AccessDeniedExceptionExceptionTranslationFilter捕获并向响应返回 403 状态代码spring-doc.cadn.net.cn

如果未在 HTTP 请求的上下文中调用该方法,则可能需要处理AccessDeniedException你自己

多个注释是按顺序计算的

如上所述,如果方法调用涉及多个 Method Security 注释,则一次处理一个 Method Security 注释。 这意味着它们可以统称为“和”在一起。 换句话说,要使调用获得授权,所有 Comments 检查都需要通过授权。spring-doc.cadn.net.cn

不支持重复的注释

也就是说,不支持在同一方法上重复相同的注释。 例如,您不能将@PreAuthorize两次。spring-doc.cadn.net.cn

相反,请使用 SPEL 的布尔支持或其对委托给单独 bean 的支持。spring-doc.cadn.net.cn

每个注释都有自己的切入点

每个 Annotation 都有自己的 pointcut 实例,该实例在整个对象层次结构中查找该 Annotation 或其元 Annotation 对应项,从方法及其封闭类开始。spring-doc.cadn.net.cn

每个 Annotation 都有自己的 Method Interceptor

每个 Comments 都有其自己的专用方法拦截器。 这样做的原因是为了让事情更具可组合性。 例如,如果需要,您可以禁用 Spring Security 默认值和仅发布@PostAuthorizemethod intercept器.spring-doc.cadn.net.cn

方法拦截器如下:spring-doc.cadn.net.cn

一般来说,你可以认为下面的清单代表了 Spring Security 在你添加@EnableMethodSecurity:spring-doc.cadn.net.cn

@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
static Advisor preAuthorizeMethodInterceptor() {
    return AuthorizationManagerBeforeMethodInterceptor.preAuthorize();
}

@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
static Advisor postAuthorizeMethodInterceptor() {
    return AuthorizationManagerAfterMethodInterceptor.postAuthorize();
}

@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
static Advisor preFilterMethodInterceptor() {
    return AuthorizationManagerBeforeMethodInterceptor.preFilter();
}

@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
static Advisor postFilterMethodInterceptor() {
    return AuthorizationManagerAfterMethodInterceptor.postFilter();
}

优先授予对复杂 SPEL 表达式的权限

很多时候,引入复杂的 SPEL 表达式可能很诱人,如下所示:spring-doc.cadn.net.cn

@PreAuthorize("hasAuthority('permission:read') || hasRole('ADMIN')")
@PreAuthorize("hasAuthority('permission:read') || hasRole('ADMIN')")

但是,您可以改为授予permission:read对于那些ROLE_ADMIN. 一种方法是使用RoleHierarchy这样:spring-doc.cadn.net.cn

@Bean
static RoleHierarchy roleHierarchy() {
    return RoleHierarchyImpl.fromHierarchy("ROLE_ADMIN > permission:read");
}
companion object {
    @Bean
    fun roleHierarchy(): RoleHierarchy {
        return RoleHierarchyImpl.fromHierarchy("ROLE_ADMIN > permission:read")
    }
}
<bean id="roleHierarchy"
        class="org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl" factory-method="fromHierarchy">
    <constructor-arg value="ROLE_ADMIN > permission:read"/>
</bean>

然后MethodSecurityExpressionHandler实例. 这样,您就可以拥有更简单的@PreAuthorize表达式,如下所示:spring-doc.cadn.net.cn

@PreAuthorize("hasAuthority('permission:read')")
@PreAuthorize("hasAuthority('permission:read')")

或者,在可能的情况下,在登录时将特定于应用程序的授权逻辑调整为授予的权限。spring-doc.cadn.net.cn

比较请求级和方法级授权

何时应优先使用方法级授权而不是请求级授权? 其中一些归结为品味;但是,请考虑以下每个优势列表来帮助您做出决定。spring-doc.cadn.net.cn

请求级别spring-doc.cadn.net.cn

方法级别spring-doc.cadn.net.cn

授权类型spring-doc.cadn.net.cn

粗粒度spring-doc.cadn.net.cn

细粒度spring-doc.cadn.net.cn

配置位置spring-doc.cadn.net.cn

在 Config 类中声明spring-doc.cadn.net.cn

local to 方法声明spring-doc.cadn.net.cn

配置样式spring-doc.cadn.net.cn

DSL (英语)spring-doc.cadn.net.cn

附注spring-doc.cadn.net.cn

授权定义spring-doc.cadn.net.cn

编程spring-doc.cadn.net.cn

斯佩尔spring-doc.cadn.net.cn

主要的权衡似乎是您希望授权规则所在的位置。spring-doc.cadn.net.cn

请务必记住,当您使用基于注释的方法安全性时,未注释的方法将不安全。 为了防止这种情况,请在您的HttpSecurity实例。

使用注释进行授权

Spring Security 启用方法级授权支持的主要方式是通过可以添加到方法、类和接口的 Comments。spring-doc.cadn.net.cn

授权方法调用@PreAuthorize

Method Security 处于活动状态时,您可以使用@PreAuthorize注解,如下所示:spring-doc.cadn.net.cn

@Component
public class BankService {
	@PreAuthorize("hasRole('ADMIN')")
	public Account readAccount(Long id) {
        // ... is only invoked if the `Authentication` has the `ROLE_ADMIN` authority
	}
}
@Component
open class BankService {
	@PreAuthorize("hasRole('ADMIN')")
	fun readAccount(id: Long): Account {
        // ... is only invoked if the `Authentication` has the `ROLE_ADMIN` authority
	}
}

这是为了表明只有在提供的表达式hasRole('ADMIN')通过。spring-doc.cadn.net.cn

然后,您可以测试该类以确认它正在强制执行授权规则,如下所示:spring-doc.cadn.net.cn

@Autowired
BankService bankService;

@WithMockUser(roles="ADMIN")
@Test
void readAccountWithAdminRoleThenInvokes() {
    Account account = this.bankService.readAccount("12345678");
    // ... assertions
}

@WithMockUser(roles="WRONG")
@Test
void readAccountWithWrongRoleThenAccessDenied() {
    assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(
        () -> this.bankService.readAccount("12345678"));
}
@WithMockUser(roles="ADMIN")
@Test
fun readAccountWithAdminRoleThenInvokes() {
    val account: Account = this.bankService.readAccount("12345678")
    // ... assertions
}

@WithMockUser(roles="WRONG")
@Test
fun readAccountWithWrongRoleThenAccessDenied() {
    assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy {
        this.bankService.readAccount("12345678")
    }
}
@PreAuthorize也可以是元注释在类或接口级别定义,并使用 SPEL 授权表达式

@PreAuthorize对于声明所需的权限非常有帮助,它也可以用于评估涉及方法参数的更复杂的表达式spring-doc.cadn.net.cn

授权方法结果@PostAuthorize

当 Method Security 处于活动状态时,您可以使用@PostAuthorize注解,如下所示:spring-doc.cadn.net.cn

@Component
public class BankService {
	@PostAuthorize("returnObject.owner == authentication.name")
	public Account readAccount(Long id) {
        // ... is only returned if the `Account` belongs to the logged in user
	}
}
@Component
open class BankService {
	@PostAuthorize("returnObject.owner == authentication.name")
	fun readAccount(id: Long): Account {
        // ... is only returned if the `Account` belongs to the logged in user
	}
}

这是为了表明该方法只有在提供的表达式returnObject.owner == authentication.name通过。returnObject表示Account对象。spring-doc.cadn.net.cn

然后,您可以测试该类以确认它正在强制执行授权规则:spring-doc.cadn.net.cn

@Autowired
BankService bankService;

@WithMockUser(username="owner")
@Test
void readAccountWhenOwnedThenReturns() {
    Account account = this.bankService.readAccount("12345678");
    // ... assertions
}

@WithMockUser(username="wrong")
@Test
void readAccountWhenNotOwnedThenAccessDenied() {
    assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(
        () -> this.bankService.readAccount("12345678"));
}
@WithMockUser(username="owner")
@Test
fun readAccountWhenOwnedThenReturns() {
    val account: Account = this.bankService.readAccount("12345678")
    // ... assertions
}

@WithMockUser(username="wrong")
@Test
fun readAccountWhenNotOwnedThenAccessDenied() {
    assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy {
        this.bankService.readAccount("12345678")
    }
}
@PostAuthorize也可以是元注释在类或接口级别定义,并使用 SPEL 授权表达式

@PostAuthorize在防御不安全的 Direct Object Reference 时特别有用。 事实上,它可以被定义为元注释,如下所示:spring-doc.cadn.net.cn

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@PostAuthorize("returnObject.owner == authentication.name")
public @interface RequireOwnership {}
@Target(ElementType.METHOD, ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@PostAuthorize("returnObject.owner == authentication.name")
annotation class RequireOwnership

允许您改为按以下方式对服务进行注释:spring-doc.cadn.net.cn

@Component
public class BankService {
	@RequireOwnership
	public Account readAccount(Long id) {
        // ... is only returned if the `Account` belongs to the logged in user
	}
}
@Component
open class BankService {
	@RequireOwnership
	fun readAccount(id: Long): Account {
        // ... is only returned if the `Account` belongs to the logged in user
	}
}

结果是上述方法只会返回Account如果其owner属性与已登录用户的name. 否则,Spring Security 将抛出一个AccessDeniedException并返回 403 状态代码。spring-doc.cadn.net.cn

过滤方法参数@PreFilter

当 Method Security 处于活动状态时,您可以使用@PreFilter注解,如下所示:spring-doc.cadn.net.cn

@Component
public class BankService {
	@PreFilter("filterObject.owner == authentication.name")
	public Collection<Account> updateAccounts(Account... accounts) {
        // ... `accounts` will only contain the accounts owned by the logged-in user
        return updated;
	}
}
@Component
open class BankService {
	@PreFilter("filterObject.owner == authentication.name")
	fun updateAccounts(vararg accounts: Account): Collection<Account> {
        // ... `accounts` will only contain the accounts owned by the logged-in user
        return updated
	}
}

这是为了从accounts其中表达式filterObject.owner == authentication.name失败。filterObject表示每个accountaccounts,用于测试每个account.spring-doc.cadn.net.cn

然后,您可以通过以下方式测试该类,以确认它正在强制执行授权规则:spring-doc.cadn.net.cn

@Autowired
BankService bankService;

@WithMockUser(username="owner")
@Test
void updateAccountsWhenOwnedThenReturns() {
    Account ownedBy = ...
    Account notOwnedBy = ...
    Collection<Account> updated = this.bankService.updateAccounts(ownedBy, notOwnedBy);
    assertThat(updated).containsOnly(ownedBy);
}
@Autowired
lateinit var bankService: BankService

@WithMockUser(username="owner")
@Test
fun updateAccountsWhenOwnedThenReturns() {
    val ownedBy: Account = ...
    val notOwnedBy: Account = ...
    val updated: Collection<Account> = bankService.updateAccounts(ownedBy, notOwnedBy)
    assertThat(updated).containsOnly(ownedBy)
}
@PreFilter也可以是元注释在类或接口级别定义,并使用 SPEL 授权表达式

@PreFilter支持数组、集合、映射和流(只要流仍处于打开状态)。spring-doc.cadn.net.cn

例如,上面的updateAccountsdeclaration 的功能与其他四个相同:spring-doc.cadn.net.cn

@PreFilter("filterObject.owner == authentication.name")
public Collection<Account> updateAccounts(Account[] accounts)

@PreFilter("filterObject.owner == authentication.name")
public Collection<Account> updateAccounts(Collection<Account> accounts)

@PreFilter("filterObject.value.owner == authentication.name")
public Collection<Account> updateAccounts(Map<String, Account> accounts)

@PreFilter("filterObject.owner == authentication.name")
public Collection<Account> updateAccounts(Stream<Account> accounts)
@PreFilter("filterObject.owner == authentication.name")
fun updateAccounts(accounts: Array<Account>): Collection<Account>

@PreFilter("filterObject.owner == authentication.name")
fun updateAccounts(accounts: Collection<Account>): Collection<Account>

@PreFilter("filterObject.value.owner == authentication.name")
fun updateAccounts(accounts: Map<String, Account>): Collection<Account>

@PreFilter("filterObject.owner == authentication.name")
fun updateAccounts(accounts: Stream<Account>): Collection<Account>

结果是上述方法将只有Account实例中,其owner属性与已登录用户的name.spring-doc.cadn.net.cn

过滤方法结果与@PostFilter

当 Method Security 处于活动状态时,您可以使用@PostFilter注解,如下所示:spring-doc.cadn.net.cn

@Component
public class BankService {
	@PostFilter("filterObject.owner == authentication.name")
	public Collection<Account> readAccounts(String... ids) {
        // ... the return value will be filtered to only contain the accounts owned by the logged-in user
        return accounts;
	}
}
@Component
open class BankService {
	@PostFilter("filterObject.owner == authentication.name")
	fun readAccounts(vararg ids: String): Collection<Account> {
        // ... the return value will be filtered to only contain the accounts owned by the logged-in user
        return accounts
	}
}

这是为了从返回值中过滤掉表达式filterObject.owner == authentication.name失败。filterObject表示每个accountaccounts,用于测试每个account.spring-doc.cadn.net.cn

然后,您可以像这样测试该类,以确认它正在强制执行授权规则:spring-doc.cadn.net.cn

@Autowired
BankService bankService;

@WithMockUser(username="owner")
@Test
void readAccountsWhenOwnedThenReturns() {
    Collection<Account> accounts = this.bankService.updateAccounts("owner", "not-owner");
    assertThat(accounts).hasSize(1);
    assertThat(accounts.get(0).getOwner()).isEqualTo("owner");
}
@Autowired
lateinit var bankService: BankService

@WithMockUser(username="owner")
@Test
fun readAccountsWhenOwnedThenReturns() {
    val accounts: Collection<Account> = bankService.updateAccounts("owner", "not-owner")
    assertThat(accounts).hasSize(1)
    assertThat(accounts[0].owner).isEqualTo("owner")
}
@PostFilter也可以是元注释在类或接口级别定义,并使用 SPEL 授权表达式

@PostFilter支持数组、集合、映射和流(只要流仍处于打开状态)。spring-doc.cadn.net.cn

例如,上面的readAccounts声明的功能与其他三种方法相同:spring-doc.cadn.net.cn

@PostFilter("filterObject.owner == authentication.name")
public Collection<Account> readAccounts(String... ids)

@PostFilter("filterObject.owner == authentication.name")
public Account[] readAccounts(String... ids)

@PostFilter("filterObject.value.owner == authentication.name")
public Map<String, Account> readAccounts(String... ids)

@PostFilter("filterObject.owner == authentication.name")
public Stream<Account> readAccounts(String... ids)
@PostFilter("filterObject.owner == authentication.name")
fun readAccounts(vararg ids: String): Collection<Account>

@PostFilter("filterObject.owner == authentication.name")
fun readAccounts(vararg ids: String): Array<Account>

@PostFilter("filterObject.owner == authentication.name")
fun readAccounts(vararg ids: String): Map<String, Account>

@PostFilter("filterObject.owner == authentication.name")
fun readAccounts(vararg ids: String): Stream<Account>

结果是上述方法将返回Account实例中,其owner属性与已登录用户的name.spring-doc.cadn.net.cn

内存筛选显然可能很昂贵,因此要考虑是否更适合筛选数据层中的数据

授权方法调用@Secured

@Secured是用于授权调用的旧选项。@PreAuthorize取代它,而是推荐使用。spring-doc.cadn.net.cn

要使用@Secured注解,您应该首先更改 Method Security 声明以启用它,如下所示:spring-doc.cadn.net.cn

@EnableMethodSecurity(securedEnabled = true)
@EnableMethodSecurity(securedEnabled = true)
<sec:method-security secured-enabled="true"/>

这将导致 Spring Security 发布相应的方法拦截器,该拦截器授权带有 Comments 的方法、类和接口@Secured.spring-doc.cadn.net.cn

使用 JSR-250 注释授权方法调用

如果您想使用 JSR-250 注释,Spring Security 也支持它。@PreAuthorize具有更强的表现力,因此推荐使用。spring-doc.cadn.net.cn

要使用 JSR-250 注释,您应该首先更改 Method Security 声明以启用它们,如下所示:spring-doc.cadn.net.cn

@EnableMethodSecurity(jsr250Enabled = true)
@EnableMethodSecurity(jsr250Enabled = true)
<sec:method-security jsr250-enabled="true"/>

这将导致 Spring Security 发布相应的方法拦截器,该拦截器授权带有 Comments 的方法、类和接口@RolesAllowed,@PermitAll@DenyAll.spring-doc.cadn.net.cn

在类或接口级别声明注释

还支持在类和接口级别具有 Method Security 注释。spring-doc.cadn.net.cn

如果它是在类级别,如下所示:spring-doc.cadn.net.cn

@Controller
@PreAuthorize("hasAuthority('ROLE_USER')")
public class MyController {
    @GetMapping("/endpoint")
    public String endpoint() { ... }
}
@Controller
@PreAuthorize("hasAuthority('ROLE_USER')")
open class MyController {
    @GetMapping("/endpoint")
    fun endpoint(): String { ... }
}

然后,所有方法都继承类级行为。spring-doc.cadn.net.cn

或者,如果它在类和方法级别都声明如下:spring-doc.cadn.net.cn

@Controller
@PreAuthorize("hasAuthority('ROLE_USER')")
public class MyController {
    @GetMapping("/endpoint")
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    public String endpoint() { ... }
}
@Controller
@PreAuthorize("hasAuthority('ROLE_USER')")
open class MyController {
    @GetMapping("/endpoint")
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    fun endpoint(): String { ... }
}

然后,声明 Comments 的方法将覆盖类级 Comments。spring-doc.cadn.net.cn

接口也是如此,不同之处在于,如果一个类从两个不同的接口继承 Comments,则启动将失败。 这是因为 Spring Security 无法判断你想使用哪一个。spring-doc.cadn.net.cn

在这种情况下,您可以通过将 Annotation 添加到具体方法来解决歧义。spring-doc.cadn.net.cn

使用 Meta 注释

Method Security 支持元注释。 这意味着您可以采用任何注释,并根据特定于应用程序的用例提高可读性。spring-doc.cadn.net.cn

例如,您可以简化@PreAuthorize("hasRole('ADMIN')")@IsAdmin这样:spring-doc.cadn.net.cn

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@PreAuthorize("hasRole('ADMIN')")
public @interface IsAdmin {}
@Target(ElementType.METHOD, ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@PreAuthorize("hasRole('ADMIN')")
annotation class IsAdmin

结果是,您现在可以在安全方法上执行以下作:spring-doc.cadn.net.cn

@Component
public class BankService {
	@IsAdmin
	public Account readAccount(Long id) {
        // ... is only returned if the `Account` belongs to the logged in user
	}
}
@Component
open class BankService {
	@IsAdmin
	fun readAccount(id: Long): Account {
        // ... is only returned if the `Account` belongs to the logged in user
	}
}

这将产生更具可读性的方法定义。spring-doc.cadn.net.cn

模板化元注释表达式

您还可以选择使用元注释模板,它允许使用更强大的注释定义。spring-doc.cadn.net.cn

首先,发布以下 Bean:spring-doc.cadn.net.cn

@Bean
static AnnotationTemplateExpressionDefaults templateExpressionDefaults() {
	return new AnnotationTemplateExpressionDefaults();
}
companion object {
    @Bean
    fun templateExpressionDefaults(): AnnotationTemplateExpressionDefaults {
        return AnnotationTemplateExpressionDefaults()
    }
}

Now 而不是@IsAdmin,您可以创建更强大的内容,例如@HasRole这样:spring-doc.cadn.net.cn

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@PreAuthorize("hasRole('{value}')")
public @interface HasRole {
	String value();
}
@Target(ElementType.METHOD, ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@PreAuthorize("hasRole('{value}')")
annotation class HasRole(val value: String)

结果是,您现在可以在安全方法上执行以下作:spring-doc.cadn.net.cn

@Component
public class BankService {
	@HasRole("ADMIN")
	public Account readAccount(Long id) {
        // ... is only returned if the `Account` belongs to the logged in user
	}
}
@Component
open class BankService {
	@HasRole("ADMIN")
	fun readAccount(id: Long): Account {
        // ... is only returned if the `Account` belongs to the logged in user
	}
}

请注意,这也适用于方法变量和所有 Comments 类型,尽管您需要小心地正确使用引号,以便生成的 SPEL 表达式是正确的。spring-doc.cadn.net.cn

例如,请考虑以下@HasAnyRole注解:spring-doc.cadn.net.cn

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@PreAuthorize("hasAnyRole({roles})")
public @interface HasAnyRole {
	String[] roles();
}
@Target(ElementType.METHOD, ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@PreAuthorize("hasAnyRole({roles})")
annotation class HasAnyRole(val roles: Array<String>)

在这种情况下,您会注意到您不应该在表达式中使用引号,而应该在参数值中使用引号,如下所示:spring-doc.cadn.net.cn

@Component
public class BankService {
	@HasAnyRole(roles = { "'USER'", "'ADMIN'" })
	public Account readAccount(Long id) {
        // ... is only returned if the `Account` belongs to the logged in user
	}
}
@Component
open class BankService {
	@HasAnyRole(roles = arrayOf("'USER'", "'ADMIN'"))
	fun readAccount(id: Long): Account {
        // ... is only returned if the `Account` belongs to the logged in user
	}
}

因此,一旦被替换,表达式就会变成@PreAuthorize("hasAnyRole('USER', 'ADMIN')").spring-doc.cadn.net.cn

启用某些注释

您可以关闭@EnableMethodSecurity的预配置,并将其替换为您自己的配置。 如果您愿意,您可以选择这样做自定义AuthorizationManagerPointcut. 或者您可能只想启用特定的注释,例如@PostAuthorize.spring-doc.cadn.net.cn

您可以通过以下方式执行此作:spring-doc.cadn.net.cn

仅 @PostAuthorize 配置
@Configuration
@EnableMethodSecurity(prePostEnabled = false)
class MethodSecurityConfig {
	@Bean
	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
	Advisor postAuthorize() {
		return AuthorizationManagerAfterMethodInterceptor.postAuthorize();
	}
}
@Configuration
@EnableMethodSecurity(prePostEnabled = false)
class MethodSecurityConfig {
	@Bean
	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
	fun postAuthorize() : Advisor {
		return AuthorizationManagerAfterMethodInterceptor.postAuthorize()
	}
}
<sec:method-security pre-post-enabled="false"/>

<aop:config/>

<bean id="postAuthorize"
	class="org.springframework.security.authorization.method.AuthorizationManagerBeforeMethodInterceptor"
	factory-method="postAuthorize"/>

上面的代码片段通过首先禁用 Method Security 的预配置,然后发布@PostAuthorize拦截 器本身。spring-doc.cadn.net.cn

授权方式<intercept-methods>

虽然使用 Spring Security 的基于 Comments 的支持是方法安全性的首选,但您也可以使用 XML 来声明 Bean 授权规则。spring-doc.cadn.net.cn

如果需要在 XML 配置中声明它,可以使用<intercept-methods>这样:spring-doc.cadn.net.cn

<bean class="org.mycompany.MyController">
    <intercept-methods>
        <protect method="get*" access="hasAuthority('read')"/>
        <protect method="*" access="hasAuthority('write')"/>
    </intercept-methods>
</bean>
这仅支持按前缀或按名称匹配方法。 如果您的需求比这更复杂,请改用 annotation support

以编程方式授权方法

如您所见,有几种方法可以使用方法安全性 SPEL 表达式指定重要的授权规则。spring-doc.cadn.net.cn

有多种方法可以让你的逻辑基于 Java 而不是基于 SPEL。 这为整个 Java 语言提供了 use 访问权限,以提高可测试性和流控制。spring-doc.cadn.net.cn

在 SPEL 中使用自定义 Bean

以编程方式授权方法的第一种方法是一个两步过程。spring-doc.cadn.net.cn

首先,声明一个 bean,该 bean 具有一个方法,该方法采用MethodSecurityExpressionOperations实例,如下所示:spring-doc.cadn.net.cn

@Component("authz")
public class AuthorizationLogic {
    public boolean decide(MethodSecurityExpressionOperations operations) {
        // ... authorization logic
    }
}
@Component("authz")
open class AuthorizationLogic {
    fun decide(operations: MethodSecurityExpressionOperations): boolean {
        // ... authorization logic
    }
}

然后,按以下方式在 Comments 中引用该 bean:spring-doc.cadn.net.cn

@Controller
public class MyController {
    @PreAuthorize("@authz.decide(#root)")
    @GetMapping("/endpoint")
    public String endpoint() {
        // ...
    }
}
@Controller
open class MyController {
    @PreAuthorize("@authz.decide(#root)")
    @GetMapping("/endpoint")
    fun String endpoint() {
        // ...
    }
}

Spring Security 将为每个方法调用在该 bean 上调用给定的方法。spring-doc.cadn.net.cn

这样做的好处是,您的所有授权逻辑都位于一个单独的类中,该类可以独立进行单元测试和正确性验证。 它还可以访问完整的 Java 语言。spring-doc.cadn.net.cn

除了返回Boolean,您还可以返回null以指示 Code 放弃做出决策。

如果要包含有关决策性质的更多信息,可以改为返回自定义AuthorizationDecision喜欢这个:spring-doc.cadn.net.cn

@Component("authz")
public class AuthorizationLogic {
    public AuthorizationDecision decide(MethodSecurityExpressionOperations operations) {
        // ... authorization logic
        return new MyAuthorizationDecision(false, details);
    }
}
@Component("authz")
open class AuthorizationLogic {
    fun decide(operations: MethodSecurityExpressionOperations): AuthorizationDecision {
        // ... authorization logic
        return MyAuthorizationDecision(false, details)
    }
}

或者抛出自定义AuthorizationDeniedException实例。 但请注意,返回对象是首选,因为这不会产生生成堆栈跟踪的费用。spring-doc.cadn.net.cn

然后,您可以在自定义授权结果的处理方式时访问自定义详细信息。spring-doc.cadn.net.cn

使用自定义授权管理器

以编程方式授权方法的第二种方法是创建自定义AuthorizationManager.spring-doc.cadn.net.cn

首先,声明一个授权 Management 器实例,可能像这样:spring-doc.cadn.net.cn

@Component
public class MyAuthorizationManager implements AuthorizationManager<MethodInvocation>, AuthorizationManager<MethodInvocationResult> {
    @Override
    public AuthorizationDecision check(Supplier<Authentication> authentication, MethodInvocation invocation) {
        // ... authorization logic
    }

    @Override
    public AuthorizationDecision check(Supplier<Authentication> authentication, MethodInvocationResult invocation) {
        // ... authorization logic
    }
}
@Component
class MyAuthorizationManager : AuthorizationManager<MethodInvocation>, AuthorizationManager<MethodInvocationResult> {
    override fun check(authentication: Supplier<Authentication>, invocation: MethodInvocation): AuthorizationDecision {
        // ... authorization logic
    }

    override fun check(authentication: Supplier<Authentication>, invocation: MethodInvocationResult): AuthorizationDecision {
        // ... authorization logic
    }
}

然后,使用与所需时间相对应的切入点发布方法 interceptorAuthorizationManager运行。 例如,您可以将@PreAuthorize@PostAuthorize像这样工作:spring-doc.cadn.net.cn

仅 @PreAuthorize 和 @PostAuthorize 配置
@Configuration
@EnableMethodSecurity(prePostEnabled = false)
class MethodSecurityConfig {
    @Bean
	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
	Advisor preAuthorize(MyAuthorizationManager manager) {
		return AuthorizationManagerBeforeMethodInterceptor.preAuthorize(manager);
	}

	@Bean
	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
	Advisor postAuthorize(MyAuthorizationManager manager) {
		return AuthorizationManagerAfterMethodInterceptor.postAuthorize(manager);
	}
}
@Configuration
@EnableMethodSecurity(prePostEnabled = false)
class MethodSecurityConfig {
   	@Bean
	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
	fun preAuthorize(manager: MyAuthorizationManager) : Advisor {
		return AuthorizationManagerBeforeMethodInterceptor.preAuthorize(manager)
	}

	@Bean
	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
	fun postAuthorize(manager: MyAuthorizationManager) : Advisor {
		return AuthorizationManagerAfterMethodInterceptor.postAuthorize(manager)
	}
}
<sec:method-security pre-post-enabled="false"/>

<aop:config/>

<bean id="preAuthorize"
	class="org.springframework.security.authorization.method.AuthorizationManagerBeforeMethodInterceptor"
	factory-method="preAuthorize">
    <constructor-arg ref="myAuthorizationManager"/>
</bean>

<bean id="postAuthorize"
	class="org.springframework.security.authorization.method.AuthorizationManagerAfterMethodInterceptor"
	factory-method="postAuthorize">
    <constructor-arg ref="myAuthorizationManager"/>
</bean>

你可以使用 Order 常量中指定的 order 常量将拦截器放置在 Spring Security 方法拦截器之间AuthorizationInterceptorsOrder.spring-doc.cadn.net.cn

自定义表达式处理

或者,第三种,您可以自定义每个 SPEL 表达式的处理方式。 为此,您可以公开自定义MethodSecurityExpressionHandler这样:spring-doc.cadn.net.cn

自定义 MethodSecurityExpressionHandler
@Bean
static MethodSecurityExpressionHandler methodSecurityExpressionHandler(RoleHierarchy roleHierarchy) {
	DefaultMethodSecurityExpressionHandler handler = new DefaultMethodSecurityExpressionHandler();
	handler.setRoleHierarchy(roleHierarchy);
	return handler;
}
companion object {
	@Bean
	fun methodSecurityExpressionHandler(roleHierarchy: RoleHierarchy) : MethodSecurityExpressionHandler {
		val handler = DefaultMethodSecurityExpressionHandler()
		handler.setRoleHierarchy(roleHierarchy)
		return handler
	}
}
<sec:method-security>
	<sec:expression-handler ref="myExpressionHandler"/>
</sec:method-security>

<bean id="myExpressionHandler"
		class="org.springframework.security.messaging.access.expression.DefaultMessageSecurityExpressionHandler">
	<property name="roleHierarchy" ref="roleHierarchy"/>
</bean>

我们揭露MethodSecurityExpressionHandler使用static方法,以确保 Spring 在初始化 Spring Security 的方法安全性之前发布它@Configurationspring-doc.cadn.net.cn

您还可以亚纲DefaultMessageSecurityExpressionHandler以添加您自己的自定义授权表达式,而不是默认值。spring-doc.cadn.net.cn

使用 AOT

Spring Security 将扫描应用程序上下文中的所有 bean 以查找使用@PreAuthorize@PostAuthorize. 找到一个 bean 后,它将解析安全表达式中使用的任何 bean,并为该 bean 注册适当的运行时提示。 如果它找到使用@AuthorizeReturnObject,它将在方法的返回类型中递归搜索@PreAuthorize@PostAuthorize注解并相应地注册它们。spring-doc.cadn.net.cn

例如,考虑以下 Spring Boot 应用程序:spring-doc.cadn.net.cn

@Service
public class AccountService { (1)

    @PreAuthorize("@authz.decide()") (2)
    @AuthorizeReturnObject (3)
    public Account getAccountById(String accountId) {
        // ...
    }

}

public class Account {

    private final String accountNumber;

    // ...

    @PreAuthorize("@accountAuthz.canViewAccountNumber()") (4)
    public String getAccountNumber() {
        return this.accountNumber;
    }

    @AuthorizeReturnObject (5)
    public User getUser() {
        return new User("John Doe");
    }

}

public class User {

    private final String fullName;

    // ...

    @PostAuthorize("@myOtherAuthz.decide()") (6)
    public String getFullName() {
        return this.fullName;
    }

}
@Service
class AccountService { (1)

    @PreAuthorize("@authz.decide()") (2)
    @AuthorizeReturnObject (3)
    fun getAccountById(accountId: String): Account {
        // ...
    }

}

class Account(private val accountNumber: String) {

    @PreAuthorize("@accountAuthz.canViewAccountNumber()") (4)
    fun getAccountNumber(): String {
        return this.accountNumber
    }

    @AuthorizeReturnObject (5)
    fun getUser(): User {
        return User("John Doe")
    }

}

class User(private val fullName: String) {

    @PostAuthorize("@myOtherAuthz.decide()") (6)
    fun getFullName(): String {
        return this.fullName
    }

}
1 Spring Security 会找到AccountService
2 查找使用@PreAuthorize,它将解析表达式authz在这种情况下,并为 Bean 类注册运行时提示
3 查找使用@AuthorizeReturnObject,它将查看方法的返回类型是否有任何@PreAuthorize@PostAuthorize
4 然后,它会找到一个@PreAuthorize替换为另一个 bean 名称:accountAuthz;运行时提示也为 Bean 类注册
5 寻找另一个@AuthorizeReturnObject它将再次查看方法的 return 类型
6 现在,一个@PostAuthorize与使用的另一个 bean 名称一起找到:myOtherAuthz;运行时提示也为 Bean 类注册

很多时候 Spring Security 无法提前确定方法的实际返回类型,因为它可能隐藏在擦除的泛型类型中。spring-doc.cadn.net.cn

请考虑以下服务:spring-doc.cadn.net.cn

@Service
public class AccountService {

    @AuthorizeReturnObject
    public List<Account> getAllAccounts() {
        // ...
    }

}
@Service
class AccountService {

    @AuthorizeReturnObject
    fun getAllAccounts(): List<Account> {
        // ...
    }

}

在这种情况下,泛型类型被擦除,因此 Spring Security 事先并不明显Account需要访问以检查@PreAuthorize@PostAuthorize.spring-doc.cadn.net.cn

要解决此问题,您可以发布PrePostAuthorizeExpressionBeanHintsRegistrar这样:spring-doc.cadn.net.cn

@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
static SecurityHintsRegistrar registerTheseToo() {
    return new PrePostAuthorizeExpressionBeanHintsRegistrar(Account.class);
}
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
fun registerTheseToo(): SecurityHintsRegistrar {
    return PrePostAuthorizeExpressionBeanHintsRegistrar(Account::class.java)
}

使用 AspectJ 进行授权

使用自定义切入点匹配方法

由于 Spring AOP 构建,您可以声明与 Comments 无关的模式,类似于请求级授权。 这具有集中方法级授权规则的潜在优势。spring-doc.cadn.net.cn

例如,您可以使用 publish your ownAdvisor或使用<protect-pointcut>将 AOP 表达式与服务层的授权规则匹配,如下所示:spring-doc.cadn.net.cn

import static org.springframework.security.authorization.AuthorityAuthorizationManager.hasRole

@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
static Advisor protectServicePointcut() {
    AspectJExpressionPointcut pattern = new AspectJExpressionPointcut()
    pattern.setExpression("execution(* com.mycompany.*Service.*(..))")
    return new AuthorizationManagerBeforeMethodInterceptor(pattern, hasRole("USER"))
}
import static org.springframework.security.authorization.AuthorityAuthorizationManager.hasRole

companion object {
    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    fun protectServicePointcut(): Advisor {
        val pattern = AspectJExpressionPointcut()
        pattern.setExpression("execution(* com.mycompany.*Service.*(..))")
        return new AuthorizationManagerBeforeMethodInterceptor(pattern, hasRole("USER"))
    }
}
<sec:method-security>
    <protect-pointcut expression="execution(* com.mycompany.*Service.*(..))" access="hasRole('USER')"/>
</sec:method-security>

与 AspectJ Byte-waving 集成

有时可以通过使用 AspectJ 将 Spring Security 建议编织到 bean 的字节码中来提高性能。spring-doc.cadn.net.cn

在设置了 AspectJ 之后,你可以非常简单地在@EnableMethodSecurityannotation 或<method-security>元素中:spring-doc.cadn.net.cn

@EnableMethodSecurity(mode=AdviceMode.ASPECTJ)
@EnableMethodSecurity(mode=AdviceMode.ASPECTJ)
<sec:method-security mode="aspectj"/>

结果将是 Spring Security 将其顾问作为 AspectJ 建议发布,以便它们可以相应地被编织进来。spring-doc.cadn.net.cn

指定顺序

如前所述,每个注解都有一个 Spring AOP 方法拦截器,每个注解在 Spring AOP advisor 链中都有一个位置。spring-doc.cadn.net.cn

即,@PreFiltermethod interceptor 的 order 为 100,@PreAuthorize的值为 200,依此类推。spring-doc.cadn.net.cn

需要注意的原因是还有其他基于 AOP 的注解,例如@EnableTransactionManagement的订单为Integer.MAX_VALUE. 换句话说,默认情况下,它们位于 advisor 链的末尾。spring-doc.cadn.net.cn

有时,在 Spring Security 之前执行其他建议可能很有价值。 例如,如果您有一个带有@Transactional@PostAuthorize,您可能希望在以下情况下事务仍处于打开状态@PostAuthorize运行,以便AccessDeniedException将导致回滚。spring-doc.cadn.net.cn

要获取@EnableTransactionManagement要在方法授权通知运行之前打开事务,您可以设置@EnableTransactionManagement的顺序如下:spring-doc.cadn.net.cn

@EnableTransactionManagement(order = 0)
@EnableTransactionManagement(order = 0)
<tx:annotation-driven ref="txManager" order="0"/>

由于最早的方法拦截器 (@PreFilter)设置为 100 的顺序,则设置为 0 表示事务通知将在所有 Spring Security 通知之前运行。spring-doc.cadn.net.cn

使用 SPEL 表示授权

您已经看到了几个使用 SpEL 的示例,现在让我们更深入地介绍 API。spring-doc.cadn.net.cn

Spring Security 将其所有授权字段和方法封装在一组根对象中。 最通用的根对象称为SecurityExpressionRoot它构成了MethodSecurityExpressionRoot. Spring Security 将此根对象提供给MethodSecurityEvaluationContext准备评估授权表达式时。spring-doc.cadn.net.cn

使用授权表达式字段和方法

它提供的第一件事是 SPEL 表达式的一组增强的授权字段和方法。 以下是最常见方法的快速概述:spring-doc.cadn.net.cn

以下是最常见的字段的简要介绍:spring-doc.cadn.net.cn

现在,您已经了解了模式、规则以及如何将它们配对在一起,您应该能够理解这个更复杂的示例中发生了什么:spring-doc.cadn.net.cn

授权请求
@Component
public class MyService {
    @PreAuthorize("denyAll") (1)
    MyResource myDeprecatedMethod(...);

    @PreAuthorize("hasRole('ADMIN')") (2)
    MyResource writeResource(...)

    @PreAuthorize("hasAuthority('db') and hasRole('ADMIN')") (3)
    MyResource deleteResource(...)

    @PreAuthorize("principal.claims['aud'] == 'my-audience'") (4)
    MyResource readResource(...);

	@PreAuthorize("@authz.check(authentication, #root)")
    MyResource shareResource(...);
}
@Component
open class MyService {
    @PreAuthorize("denyAll") (1)
    fun myDeprecatedMethod(...): MyResource

    @PreAuthorize("hasRole('ADMIN')") (2)
    fun writeResource(...): MyResource

    @PreAuthorize("hasAuthority('db') and hasRole('ADMIN')") (3)
    fun deleteResource(...): MyResource

    @PreAuthorize("principal.claims['aud'] == 'my-audience'") (4)
    fun readResource(...): MyResource

    @PreAuthorize("@authz.check(#root)")
    fun shareResource(...): MyResource
}
<sec:method-security>
    <protect-pointcut expression="execution(* com.mycompany.*Service.myDeprecatedMethod(..))" access="denyAll"/> (1)
    <protect-pointcut expression="execution(* com.mycompany.*Service.writeResource(..))" access="hasRole('ADMIN')"/> (2)
    <protect-pointcut expression="execution(* com.mycompany.*Service.deleteResource(..))" access="hasAuthority('db') and hasRole('ADMIN')"/> (3)
    <protect-pointcut expression="execution(* com.mycompany.*Service.readResource(..))" access="principal.claims['aud'] == 'my-audience'"/> (4)
    <protect-pointcut expression="execution(* com.mycompany.*Service.shareResource(..))" access="@authz.check(#root)"/> (5)
</sec:method-security>
1 任何人都不得出于任何原因调用此方法
2 此方法只能由Authentications 授予ROLE_ADMIN
3 此方法只能由Authentications 授予dbROLE_ADMIN当局
4 此方法只能由Princpals 替换为aud声明等于 “my-audience”
5 仅当 beanauthzcheckmethod 返回true

使用方法参数

此外,Spring Security 提供了一种发现方法参数的机制,因此也可以在 SPEL 表达式中访问它们。spring-doc.cadn.net.cn

作为完整的参考,Spring Security 使用DefaultSecurityParameterNameDiscoverer以发现参数名称。 默认情况下,对方法尝试以下选项。spring-doc.cadn.net.cn

  1. 如果 Spring Security 的@Pannotation 存在于方法的单个参数上,则使用该值。 以下示例使用@P注解:spring-doc.cadn.net.cn

    import org.springframework.security.access.method.P;
    
    ...
    
    @PreAuthorize("hasPermission(#c, 'write')")
    public void updateContact(@P("c") Contact contact);
    import org.springframework.security.access.method.P
    
    ...
    
    @PreAuthorize("hasPermission(#c, 'write')")
    fun doSomething(@P("c") contact: Contact?)

    此表达式的目的是要求当前的Authenticationwrite专门针对此的权限Contact实例。spring-doc.cadn.net.cn

    在幕后,这是通过使用AnnotationParameterNameDiscoverer,您可以自定义该属性以支持任何指定注释的 value 属性。spring-doc.cadn.net.cn

  2. 如果 Spring Data 的 @Paramannotation 的 Comments 存在于该方法的至少一个参数上,则使用该值。 以下示例使用@Param注解:spring-doc.cadn.net.cn

    import org.springframework.data.repository.query.Param;
    
    ...
    
    @PreAuthorize("#n == authentication.name")
    Contact findContactByName(@Param("n") String name);
    import org.springframework.data.repository.query.Param
    
    ...
    
    @PreAuthorize("#n == authentication.name")
    fun findContactByName(@Param("n") name: String?): Contact?

    此表达式的意图是要求name等于Authentication#getName以获取要授权的调用。spring-doc.cadn.net.cn

    在幕后,这是通过使用AnnotationParameterNameDiscoverer,您可以自定义该属性以支持任何指定注释的 value 属性。spring-doc.cadn.net.cn

  3. 如果您使用-parameters参数,则使用标准 JDK 反射 API 来发现参数名称。 这适用于类和接口。spring-doc.cadn.net.cn

  4. 最后,如果使用调试符号编译代码,则使用调试符号发现参数名称。 这不适用于接口,因为它们没有有关参数名称的调试信息。 对于接口,注解或-parameters必须使用方法。spring-doc.cadn.net.cn

授权任意对象

Spring Security 还支持包装任何带有 Comments 其方法安全注释的对象。spring-doc.cadn.net.cn

实现此目的的最简单方法是将返回您希望授权的对象的任何方法标记为@AuthorizeReturnObject注解。spring-doc.cadn.net.cn

例如,请考虑以下User类:spring-doc.cadn.net.cn

public class User {
	private String name;
	private String email;

	public User(String name, String email) {
		this.name = name;
		this.email = email;
	}

	public String getName() {
		return this.name;
	}

    @PreAuthorize("hasAuthority('user:read')")
    public String getEmail() {
		return this.email;
    }
}
class User (val name:String, @get:PreAuthorize("hasAuthority('user:read')") val email:String)

给定一个像这样的接口:spring-doc.cadn.net.cn

public class UserRepository {
	@AuthorizeReturnObject
    Optional<User> findByName(String name) {
		// ...
    }
}
class UserRepository {
    @AuthorizeReturnObject
    fun findByName(name:String?): Optional<User?>? {
        // ...
    }
}

然后任何User即从findById将像其他受 Spring Security 保护的组件一样受到保护:spring-doc.cadn.net.cn

@Autowired
UserRepository users;

@Test
void getEmailWhenProxiedThenAuthorizes() {
    Optional<User> securedUser = users.findByName("name");
    assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(() -> securedUser.get().getEmail());
}
import jdk.incubator.vector.VectorOperators.Test
import java.nio.file.AccessDeniedException
import java.util.*

@Autowired
var users:UserRepository? = null

@Test
fun getEmailWhenProxiedThenAuthorizes() {
    val securedUser: Optional<User> = users.findByName("name")
    assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy{securedUser.get().getEmail()}
}

@AuthorizeReturnObject在类级别

@AuthorizeReturnObject可以放置在类级别。但请注意,这意味着 Spring Security 将尝试代理任何返回对象,包括String,Integer和其他类型。 这通常不是您想要做的。spring-doc.cadn.net.cn

如果您想使用@AuthorizeReturnObject在其方法返回值类型的类或接口上,例如int,String,Double或这些类型的集合,那么您还应该发布相应的AuthorizationAdvisorProxyFactory.TargetVisitor如下:spring-doc.cadn.net.cn

import org.springframework.security.authorization.method.AuthorizationAdvisorProxyFactory.TargetVisitor;

// ...

@Bean
static TargetVisitor skipValueTypes() {
    return TargetVisitor.defaultsSkipValueTypes();
}
import org.springframework.security.authorization.method.AuthorizationAdvisorProxyFactory.TargetVisitor

// ...

@Bean
open fun skipValueTypes() = TargetVisitor.defaultsSkipValueTypes()

您可以设置自己的AuthorizationAdvisorProxyFactory.TargetVisitor自定义任意类型集的代理spring-doc.cadn.net.cn

以编程方式代理

您还可以以编程方式代理给定对象。spring-doc.cadn.net.cn

为此,您可以自动装配提供的AuthorizationProxyFactory实例,它基于您配置的方法 Security Interceptor。 如果您正在使用@EnableMethodSecurity,那么这意味着默认情况下它将具有@PreAuthorize,@PostAuthorize,@PreFilter@PostFilter.spring-doc.cadn.net.cn

您可以通过以下方式代理 user 的实例:spring-doc.cadn.net.cn

@Autowired
AuthorizationProxyFactory proxyFactory;

@Test
void getEmailWhenProxiedThenAuthorizes() {
    User user = new User("name", "email");
    assertThat(user.getEmail()).isNotNull();
    User securedUser = proxyFactory.proxy(user);
    assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(securedUser::getEmail);
}
@Autowired
var proxyFactory:AuthorizationProxyFactory? = null

@Test
fun getEmailWhenProxiedThenAuthorizes() {
    val user: User = User("name", "email")
    assertThat(user.getEmail()).isNotNull()
    val securedUser: User = proxyFactory.proxy(user)
    assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy(securedUser::getEmail)
}

手动施工

如果您需要与 Spring Security 默认值不同的实例,您也可以定义自己的实例。spring-doc.cadn.net.cn

例如,如果您定义AuthorizationProxyFactory实例,如下所示:spring-doc.cadn.net.cn

import org.springframework.security.authorization.method.AuthorizationAdvisorProxyFactory.TargetVisitor;
import static org.springframework.security.authorization.method.AuthorizationManagerBeforeMethodInterceptor.preAuthorize;
// ...

AuthorizationProxyFactory proxyFactory = AuthorizationAdvisorProxyFactory.withDefaults();
// and if needing to skip value types
proxyFactory.setTargetVisitor(TargetVisitor.defaultsSkipValueTypes());
import org.springframework.security.authorization.method.AuthorizationAdvisorProxyFactory.TargetVisitor;
import org.springframework.security.authorization.method.AuthorizationManagerBeforeMethodInterceptor.preAuthorize

// ...

val proxyFactory: AuthorizationProxyFactory = AuthorizationProxyFactory(preAuthorize())
// and if needing to skip value types
proxyFactory.setTargetVisitor(TargetVisitor.defaultsSkipValueTypes())

然后,您可以将User如下:spring-doc.cadn.net.cn

@Test
void getEmailWhenProxiedThenAuthorizes() {
	AuthorizationProxyFactory proxyFactory = AuthorizationAdvisorProxyFactory.withDefaults();
    User user = new User("name", "email");
    assertThat(user.getEmail()).isNotNull();
    User securedUser = proxyFactory.proxy(user);
    assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(securedUser::getEmail);
}
@Test
fun getEmailWhenProxiedThenAuthorizes() {
    val proxyFactory: AuthorizationProxyFactory = AuthorizationAdvisorProxyFactory.withDefaults()
    val user: User = User("name", "email")
    assertThat(user.getEmail()).isNotNull()
    val securedUser: User = proxyFactory.proxy(user)
    assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy(securedUser::getEmail)
}

代理集合

AuthorizationProxyFactory通过代理元素类型支持 Java 集合、流、数组、可选值和迭代器,并通过代理值类型来支持映射。spring-doc.cadn.net.cn

这意味着,当代理List的对象,以下也有效:spring-doc.cadn.net.cn

@Test
void getEmailWhenProxiedThenAuthorizes() {
	AuthorizationProxyFactory proxyFactory = AuthorizationAdvisorProxyFactory.withDefaults();
    List<User> users = List.of(ada, albert, marie);
    List<User> securedUsers = proxyFactory.proxy(users);
	securedUsers.forEach((securedUser) ->
        assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(securedUser::getEmail));
}

代理类

在有限的情况下,代理 a 可能很有价值Class本身,以及AuthorizationProxyFactory也支持此功能。 这大致相当于调用ProxyFactory#getProxyClass在 Spring Framework 对创建代理的支持中。spring-doc.cadn.net.cn

这很方便的一个地方是当你需要提前构造代理类时,就像使用 Spring AOT 一样。spring-doc.cadn.net.cn

支持所有方法安全注释

AuthorizationProxyFactory支持在应用程序中启用安全注释的任何方法。 它基于任何AuthorizationAdvisor类将作为 Bean 发布。spring-doc.cadn.net.cn

因为@EnableMethodSecurity出版@PreAuthorize,@PostAuthorize,@PreFilter@PostFilterAdvisors 中,您通常不需要执行任何作即可激活该能力。spring-doc.cadn.net.cn

使用returnObjectfilterObject位于 proxy 后面,因此具有对对象的完全访问权限。spring-doc.cadn.net.cn

定制建议

如果您有也希望应用的安全建议,则可以发布自己的安全建议AuthorizationAdvisor这样:spring-doc.cadn.net.cn

@EnableMethodSecurity
class SecurityConfig {
    @Bean
    static AuthorizationAdvisor myAuthorizationAdvisor() {
        return new AuthorizationAdvisor();
    }
}
@EnableMethodSecurity
internal class SecurityConfig {
    @Bean
    fun myAuthorizationAdvisor(): AuthorizationAdvisor {
        return AuthorizationAdvisor()
    }
]

Spring Security 会将该顾问程序添加到建议集中AuthorizationProxyFactory在代理对象时添加。spring-doc.cadn.net.cn

与 Jackson 合作

此功能的一个强大用途是从控制器返回一个受保护的值,如下所示:spring-doc.cadn.net.cn

@RestController
public class UserController {
    @Autowired
    AuthorizationProxyFactory proxyFactory;

    @GetMapping
    User currentUser(@AuthenticationPrincipal User user) {
        return this.proxyFactory.proxy(user);
    }
}
@RestController
class UserController  {
    @Autowired
    var proxyFactory: AuthorizationProxyFactory? = null

    @GetMapping
    fun currentUser(@AuthenticationPrincipal user:User?): User {
        return proxyFactory.proxy(user)
    }
}
@Component
public class Null implements MethodAuthorizationDeniedHandler {
    @Override
    public Object handleDeniedInvocation(MethodInvocation methodInvocation, AuthorizationResult authorizationResult) {
        return null;
    }
}

// ...

@HandleAuthorizationDenied(handlerClass = Null.class)
public class User {
	...
}
@Component
class Null : MethodAuthorizationDeniedHandler {
    override fun handleDeniedInvocation(methodInvocation: MethodInvocation?, authorizationResult: AuthorizationResult?): Any? {
        return null
    }
}

// ...

@HandleAuthorizationDenied(handlerClass = Null.class)
open class User {
	...
}

然后,您将看到基于用户授权级别的不同 JSON 序列化。 如果他们没有user:readauthority,则他们会看到:spring-doc.cadn.net.cn

{
    "name" : "name",
    "email" : null
}

如果他们确实拥有该权限,他们将看到:spring-doc.cadn.net.cn

{
    "name" : "name",
    "email" : "email"
}

您还可以添加 Spring Boot 属性spring.jackson.default-property-inclusion=non_null从序列化中排除 null 值(如果您还不想向未经授权的用户透露 JSON 密钥)。spring-doc.cadn.net.cn

使用 AOT

Spring Security 将扫描应用程序上下文中的所有 bean 以查找使用@AuthorizeReturnObject. 找到一个代理类后,它将提前创建并注册适当的代理类。 它还将递归搜索其他同样使用@AuthorizeReturnObject并相应地注册它们。spring-doc.cadn.net.cn

例如,考虑以下 Spring Boot 应用程序:spring-doc.cadn.net.cn

@SpringBootApplication
public class MyApplication {
	@RestController
    public static class MyController { (1)
		@GetMapping
        @AuthorizeReturnObject
        Message getMessage() { (2)
			return new Message(someUser, "hello!");
        }
    }

	public static class Message { (3)
		User to;
		String text;

		// ...

        @AuthorizeReturnObject
        public User getTo() { (4)
			return this.to;
        }

		// ...
	}

	public static class User { (5)
		// ...
	}

	public static void main(String[] args) {
		SpringApplication.run(MyApplication.class);
	}
}
@SpringBootApplication
open class MyApplication {
	@RestController
    open class MyController { (1)
		@GetMapping
        @AuthorizeReturnObject
        fun getMessage():Message { (2)
			return Message(someUser, "hello!")
        }
    }

	open class Message { (3)
		val to: User
		val test: String

		// ...

        @AuthorizeReturnObject
        fun getTo(): User { (4)
			return this.to
        }

		// ...
	}

	open class User { (5)
		// ...
	}

	fun main(args: Array<String>) {
		SpringApplication.run(MyApplication.class)
	}
}
1 - 首先,Spring Security 找到MyController
2 - 查找使用@AuthorizeReturnObject,它代理Message、返回值,并将该代理类注册到RuntimeHints
3 - 然后,它遍历Message以查看它是否使用@AuthorizeReturnObject
4 - 查找使用@AuthorizeReturnObject,它代理User、返回值,并将该代理类注册到RuntimeHints
5 - 最后,它遍历User以查看它是否使用@AuthorizeReturnObject;什么也没找到,算法完成

很多时候 Spring Security 无法提前确定代理类,因为它可能隐藏在擦除的泛型类型中。spring-doc.cadn.net.cn

请考虑对MyController:spring-doc.cadn.net.cn

@RestController
public static class MyController {
    @GetMapping
    @AuthorizeReturnObject
    List<Message> getMessages() {
        return List.of(new Message(someUser, "hello!"));
    }
}
@RestController
static class MyController {
    @AuthorizeReturnObject
    @GetMapping
    fun getMessages(): Array<Message> = arrayOf(Message(someUser, "hello!"))
}

在这种情况下,泛型类型被擦除,因此 Spring Security 事先并不明显Message需要在运行时代理。spring-doc.cadn.net.cn

要解决此问题,您可以发布AuthorizeProxyFactoryHintsRegistrar这样:spring-doc.cadn.net.cn

@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
static SecurityHintsRegsitrar registerTheseToo(AuthorizationProxyFactory proxyFactory) {
	return new AuthorizeReturnObjectHintsRegistrar(proxyFactory, Message.class);
}
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
fun registerTheseToo(proxyFactory: AuthorizationProxyFactory?): SecurityHintsRegistrar {
    return AuthorizeReturnObjectHintsRegistrar(proxyFactory, Message::class.java)
}

Spring Security 将注册该类,然后像以前一样遍历其类型。spring-doc.cadn.net.cn

在授权被拒绝时提供回退值

在某些情况下,您可能不希望抛出AuthorizationDeniedException在没有所需权限的情况下调用方法时。 相反,您可能希望返回后处理结果,如掩码结果,或者在调用该方法之前发生授权被拒绝的情况下返回默认值。spring-doc.cadn.net.cn

Spring Security 通过使用@HandleAuthorizationDenied. 处理程序适用于在@PreAuthorize@PostAuthorize附注以及AuthorizationDeniedException从方法调用本身引发。spring-doc.cadn.net.cn

让我们考虑一下上一节中的示例,但不是创建AccessDeniedExceptionInterceptor要将AccessDeniedException更改为null返回值,我们将使用handlerClass属性从@HandleAuthorizationDenied:spring-doc.cadn.net.cn

public class NullMethodAuthorizationDeniedHandler implements MethodAuthorizationDeniedHandler { (1)

    @Override
    public Object handleDeniedInvocation(MethodInvocation methodInvocation, AuthorizationResult authorizationResult) {
        return null;
    }

}

@Configuration
@EnableMethodSecurity
public class SecurityConfig {

    @Bean (2)
    public NullMethodAuthorizationDeniedHandler nullMethodAuthorizationDeniedHandler() {
        return new NullMethodAuthorizationDeniedHandler();
    }

}

public class User {
    // ...

    @PreAuthorize(value = "hasAuthority('user:read')")
    @HandleAuthorizationDenied(handlerClass = NullMethodAuthorizationDeniedHandler.class)
    public String getEmail() {
        return this.email;
    }
}
class NullMethodAuthorizationDeniedHandler : MethodAuthorizationDeniedHandler { (1)

    override fun handleDeniedInvocation(methodInvocation: MethodInvocation, authorizationResult: AuthorizationResult): Any {
        return null
    }

}

@Configuration
@EnableMethodSecurity
class SecurityConfig {

    @Bean (2)
    fun nullMethodAuthorizationDeniedHandler(): NullMethodAuthorizationDeniedHandler {
        return MaskMethodAuthorizationDeniedHandler()
    }

}

class User (val name:String, @PreAuthorize(value = "hasAuthority('user:read')") @HandleAuthorizationDenied(handlerClass = NullMethodAuthorizationDeniedHandler::class) val email:String) (3)
1 创建 的实现MethodAuthorizationDeniedHandler返回一个null
2 注册NullMethodAuthorizationDeniedHandler作为 Bean
3 @HandleAuthorizationDenied并传递NullMethodAuthorizationDeniedHandlerhandlerClass属性

然后,您可以验证null值,而不是AccessDeniedException:spring-doc.cadn.net.cn

你也可以用@Component而不是创建@Bean方法spring-doc.cadn.net.cn

@Autowired
UserRepository users;

@Test
void getEmailWhenProxiedThenNullEmail() {
    Optional<User> securedUser = users.findByName("name");
    assertThat(securedUser.get().getEmail()).isNull();
}
@Autowired
var users:UserRepository? = null

@Test
fun getEmailWhenProxiedThenNullEmail() {
    val securedUser: Optional<User> = users.findByName("name")
    assertThat(securedUser.get().getEmail()).isNull()
}

使用方法调用的 Denied 结果

在某些情况下,您可能希望返回从被拒绝的结果派生的安全结果。 例如,如果用户无权查看电子邮件地址,您可能希望对原始电子邮件地址应用一些掩码,即 [email protected] 将变为 use******@example.comspring-doc.cadn.net.cn

对于这些场景,您可以覆盖handleDeniedInvocationResultMethodAuthorizationDeniedHandler,它具有MethodInvocationResult作为参数。 让我们继续前面的示例,但不要返回null,我们将返回 email 的 masked 值:spring-doc.cadn.net.cn

public class EmailMaskingMethodAuthorizationDeniedHandler implements MethodAuthorizationDeniedHandler { (1)

    @Override
    public Object handleDeniedInvocation(MethodInvocation methodInvocation, AuthorizationResult authorizationResult) {
        return "***";
    }

    @Override
    public Object handleDeniedInvocationResult(MethodInvocationResult methodInvocationResult, AuthorizationResult authorizationResult) {
        String email = (String) methodInvocationResult.getResult();
        return email.replaceAll("(^[^@]{3}|(?!^)\\G)[^@]", "$1*");
    }

}

@Configuration
@EnableMethodSecurity
public class SecurityConfig {

    @Bean (2)
    public EmailMaskingMethodAuthorizationDeniedHandler emailMaskingMethodAuthorizationDeniedHandler() {
        return new EmailMaskingMethodAuthorizationDeniedHandler();
    }

}

public class User {
    // ...

    @PostAuthorize(value = "hasAuthority('user:read')")
    @HandleAuthorizationDenied(handlerClass = EmailMaskingMethodAuthorizationDeniedHandler.class)
    public String getEmail() {
        return this.email;
    }
}
class EmailMaskingMethodAuthorizationDeniedHandler : MethodAuthorizationDeniedHandler {

    override fun handleDeniedInvocation(methodInvocation: MethodInvocation, authorizationResult: AuthorizationResult): Any {
        return "***"
    }

    override fun handleDeniedInvocationResult(methodInvocationResult: MethodInvocationResult, authorizationResult: AuthorizationResult): Any {
        val email = methodInvocationResult.result as String
        return email.replace("(^[^@]{3}|(?!^)\\G)[^@]".toRegex(), "$1*")
    }

}

@Configuration
@EnableMethodSecurity
class SecurityConfig {

    @Bean
    fun emailMaskingMethodAuthorizationDeniedHandler(): EmailMaskingMethodAuthorizationDeniedHandler {
        return EmailMaskingMethodAuthorizationDeniedHandler()
    }

}

class User (val name:String, @PostAuthorize(value = "hasAuthority('user:read')") @HandleAuthorizationDenied(handlerClass = EmailMaskingMethodAuthorizationDeniedHandler::class) val email:String) (3)
1 创建 的实现MethodAuthorizationDeniedHandler返回 Unauthorized 结果值的掩码值
2 注册EmailMaskingMethodAuthorizationDeniedHandler作为 Bean
3 @HandleAuthorizationDenied并传递EmailMaskingMethodAuthorizationDeniedHandlerhandlerClass属性

然后,您可以验证返回的是屏蔽电子邮件,而不是AccessDeniedException:spring-doc.cadn.net.cn

由于您有权访问原始 denied 值,因此请确保正确处理它,并且不要将其返回给调用方。spring-doc.cadn.net.cn

@Autowired
UserRepository users;

@Test
void getEmailWhenProxiedThenMaskedEmail() {
    Optional<User> securedUser = users.findByName("name");
    // email is [email protected]
    assertThat(securedUser.get().getEmail()).isEqualTo("use******@example.com");
}
@Autowired
var users:UserRepository? = null

@Test
fun getEmailWhenProxiedThenMaskedEmail() {
    val securedUser: Optional<User> = users.findByName("name")
    // email is [email protected]
    assertThat(securedUser.get().getEmail()).isEqualTo("use******@example.com")
}

在实现MethodAuthorizationDeniedHandler对于可以返回的类型,您有几个选项:spring-doc.cadn.net.cn

请注意,由于处理程序必须在应用程序上下文中注册为 bean,因此如果需要更复杂的逻辑,可以将依赖项注入其中。 除此之外,您还可以使用MethodInvocationMethodInvocationResult以及AuthorizationResult有关授权决策的更多详细信息。spring-doc.cadn.net.cn

根据可用参数决定返回的内容

考虑这样一个场景:不同的方法可能有多个掩码值,如果我们必须为每个方法创建一个处理程序,那么效率不会那么高,尽管这样做是完全可以的。 在这种情况下,我们可以使用通过 parameters 传递的信息来决定要做什么。 例如,我们可以创建一个自定义的@Mask注解和一个检测该注解的处理程序来决定返回什么掩码值:spring-doc.cadn.net.cn

import org.springframework.core.annotation.AnnotationUtils;

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface Mask {

    String value();

}

public class MaskAnnotationDeniedHandler implements MethodAuthorizationDeniedHandler {

    @Override
    public Object handleDeniedInvocation(MethodInvocation methodInvocation, AuthorizationResult authorizationResult) {
        Mask mask = AnnotationUtils.getAnnotation(methodInvocation.getMethod(), Mask.class);
        return mask.value();
    }

}

@Configuration
@EnableMethodSecurity
public class SecurityConfig {

    @Bean
    public MaskAnnotationDeniedHandler maskAnnotationDeniedHandler() {
        return new MaskAnnotationDeniedHandler();
    }

}

@Component
public class MyService {

    @PreAuthorize(value = "hasAuthority('user:read')")
    @HandleAuthorizationDenied(handlerClass = MaskAnnotationDeniedHandler.class)
    @Mask("***")
    public String foo() {
        return "foo";
    }

    @PreAuthorize(value = "hasAuthority('user:read')")
    @HandleAuthorizationDenied(handlerClass = MaskAnnotationDeniedHandler.class)
    @Mask("???")
    public String bar() {
        return "bar";
    }

}
import org.springframework.core.annotation.AnnotationUtils

@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class Mask(val value: String)

class MaskAnnotationDeniedHandler : MethodAuthorizationDeniedHandler {

    override fun handleDeniedInvocation(methodInvocation: MethodInvocation, authorizationResult: AuthorizationResult): Any {
        val mask = AnnotationUtils.getAnnotation(methodInvocation.method, Mask::class.java)
        return mask.value
    }

}

@Configuration
@EnableMethodSecurity
class SecurityConfig {

    @Bean
    fun maskAnnotationDeniedHandler(): MaskAnnotationDeniedHandler {
        return MaskAnnotationDeniedHandler()
    }

}

@Component
class MyService {

    @PreAuthorize(value = "hasAuthority('user:read')")
    @HandleAuthorizationDenied(handlerClass = MaskAnnotationDeniedHandler::class)
    @Mask("***")
    fun foo(): String {
        return "foo"
    }

    @PreAuthorize(value = "hasAuthority('user:read')")
    @HandleAuthorizationDenied(handlerClass = MaskAnnotationDeniedHandler::class)
    @Mask("???")
    fun bar(): String {
        return "bar"
    }

}

现在,当访问被拒绝时,返回值将基于@Mask注解:spring-doc.cadn.net.cn

@Autowired
MyService myService;

@Test
void fooWhenDeniedThenReturnStars() {
    String value = this.myService.foo();
    assertThat(value).isEqualTo("***");
}

@Test
void barWhenDeniedThenReturnQuestionMarks() {
    String value = this.myService.foo();
    assertThat(value).isEqualTo("???");
}
@Autowired
var myService: MyService

@Test
fun fooWhenDeniedThenReturnStars() {
    val value: String = myService.foo()
    assertThat(value).isEqualTo("***")
}

@Test
fun barWhenDeniedThenReturnQuestionMarks() {
    val value: String = myService.foo()
    assertThat(value).isEqualTo("???")
}

与 Meta Annotation 支持相结合

您还可以将@HandleAuthorizationDenied替换为其他 Comments,以便减少和简化方法中的 Comments。 让我们考虑上一节中的示例并合并@HandleAuthorizationDenied@Mask:spring-doc.cadn.net.cn

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@HandleAuthorizationDenied(handlerClass = MaskAnnotationDeniedHandler.class)
public @interface Mask {

    String value();

}

@Mask("***")
public String myMethod() {
    // ...
}
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
@HandleAuthorizationDenied(handlerClass = MaskAnnotationDeniedHandler::class)
annotation class Mask(val value: String)

@Mask("***")
fun myMethod(): String {
    // ...
}

现在,当您的方法中需要 mask 行为时,您不必记住添加这两个 Comments。 请务必阅读 Meta Annotations Support 部分,了解有关用法的更多详细信息。spring-doc.cadn.net.cn

迁移自@EnableGlobalMethodSecurity

如果您正在使用@EnableGlobalMethodSecurity,您应该迁移到@EnableMethodSecurity.spring-doc.cadn.net.cn

全局方法安全性替换为方法安全性

@EnableGlobalMethodSecurity<global-method-security>已弃用,取而代之的是@EnableMethodSecurity<method-security>分别。 新的 annotation 和 XML 元素默认激活 Spring 的 pre-post 注释,并使用AuthorizationManager内部。spring-doc.cadn.net.cn

这意味着以下两个清单在功能上是等效的:spring-doc.cadn.net.cn

@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableGlobalMethodSecurity(prePostEnabled = true)
<global-method-security pre-post-enabled="true"/>
@EnableMethodSecurity
@EnableMethodSecurity
<method-security/>

对于不使用 pre-post 注释的应用程序,请确保将其关闭以避免激活不需要的行为。spring-doc.cadn.net.cn

例如,像这样的列表:spring-doc.cadn.net.cn

@EnableGlobalMethodSecurity(securedEnabled = true)
@EnableGlobalMethodSecurity(securedEnabled = true)
<global-method-security secured-enabled="true"/>

应更改为:spring-doc.cadn.net.cn

@EnableMethodSecurity(securedEnabled = true, prePostEnabled = false)
@EnableMethodSecurity(securedEnabled = true, prePostEnabled = false)
<method-security secured-enabled="true" pre-post-enabled="false"/>

使用自定义@Bean而不是子类化DefaultMethodSecurityExpressionHandler

作为性能优化,引入了一种新方法MethodSecurityExpressionHandler这需要Supplier<Authentication>而不是Authentication.spring-doc.cadn.net.cn

这允许 Spring Security 延迟对Authentication,并在您使用@EnableMethodSecurity而不是@EnableGlobalMethodSecurity.spring-doc.cadn.net.cn

但是,假设您的代码扩展了DefaultMethodSecurityExpressionHandler和覆盖createSecurityExpressionRoot(Authentication, MethodInvocation)返回自定义SecurityExpressionRoot实例。 这将不再有效,因为@EnableMethodSecurity设置呼叫createEvaluationContext(Supplier<Authentication>, MethodInvocation)相反。spring-doc.cadn.net.cn

令人高兴的是,这种级别的定制通常是不必要的。 相反,您可以使用所需的授权方法创建自定义 Bean。spring-doc.cadn.net.cn

例如,假设您希望对@PostAuthorize("hasAuthority('ADMIN')"). 您可以创建自定义@Bean像这个:spring-doc.cadn.net.cn

class MyAuthorizer {
	boolean isAdmin(MethodSecurityExpressionOperations root) {
		boolean decision = root.hasAuthority("ADMIN");
		// custom work ...
        return decision;
	}
}
class MyAuthorizer {
	fun isAdmin(root: MethodSecurityExpressionOperations): boolean {
		val decision = root.hasAuthority("ADMIN");
		// custom work ...
        return decision;
	}
}

然后在 Comments 中引用它,如下所示:spring-doc.cadn.net.cn

@PreAuthorize("@authz.isAdmin(#root)")
@PreAuthorize("@authz.isAdmin(#root)")

我还是更喜欢子类DefaultMethodSecurityExpressionHandler

如果必须继续子类化DefaultMethodSecurityExpressionHandler,您仍然可以这样做。 相反,请覆盖createEvaluationContext(Supplier<Authentication>, MethodInvocation)方法如下:spring-doc.cadn.net.cn

@Component
class MyExpressionHandler extends DefaultMethodSecurityExpressionHandler {
    @Override
    public EvaluationContext createEvaluationContext(Supplier<Authentication> authentication, MethodInvocation mi) {
		StandardEvaluationContext context = (StandardEvaluationContext) super.createEvaluationContext(authentication, mi);
        MethodSecurityExpressionOperations delegate = (MethodSecurityExpressionOperations) context.getRootObject().getValue();
        MySecurityExpressionRoot root = new MySecurityExpressionRoot(delegate);
        context.setRootObject(root);
        return context;
    }
}
@Component
class MyExpressionHandler: DefaultMethodSecurityExpressionHandler {
    override fun createEvaluationContext(authentication: Supplier<Authentication>,
        val mi: MethodInvocation): EvaluationContext {
		val context = super.createEvaluationContext(authentication, mi) as StandardEvaluationContext
        val delegate = context.getRootObject().getValue() as MethodSecurityExpressionOperations
        val root = MySecurityExpressionRoot(delegate)
        context.setRootObject(root)
        return context
    }
}

延伸阅读

现在您已经保护了应用程序的请求,如果您尚未保护其请求,请保护它。 您还可以进一步阅读有关测试应用程序或将 Spring Security 与应用程序的其他方面(如数据层跟踪和指标)集成的信息。spring-doc.cadn.net.cn