1. 前言
基于配置的接口角色访问控制[2] 我们讲解了如何通过 javaConfig 的方式配置接口的角色访问控制。其实还有一种更加灵活的配置方式 基于注解 。今天我们就来探讨一下。DEMO 获取方式在文末。
2. Spring Security 方法安全
Spring Security 基于注解的安全认证是通过在相关的方法上进行安全注解标记来实现的。
2.1 开启全局方法安全
我们可以在任何 @Configuration实例上使用 @EnableGlobalMethodSecurity 注解来启用全局方法安全注解功能。该注解提供了三种不同的机制来实现同一种功能,所以我们单独开一章进行探讨。
3. @EnableGlobalMethodSecurity 注解
@Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
@Target(value = {
java.lang.annotation.ElementType.TYPE })
@Documented
@Import({
GlobalMethodSecuritySelector.class })
@EnableGlobalAuthentication
@Configuration
public @interface EnableGlobalMethodSecurity {
/**
* 基于表达式进行方法访问控制
*/
boolean prePostEnabled() default