Spring Security权限控制机制
Spring Security中可以通过表达式控制方法权限,其中有四个支持使用表达式的注解,分别是@PreAuthorize、@PostAuthorize、@PreFilter和@PostFilter。其中前两者可以用来在方法调用前或者调用后进行权限检查,后两者可以用来对集合类型的参数或者返回值进行过滤。
启动Security机制的配置
它们的定义能够对我们的方法的调用产生影响我们需要设置global-method-security元素的pre-post-annotations=”enabled”,默认为disabled。
xml配置方式
<security:global-method-security pre-post-annotations="disabled"/>
JavaConfig配置方式
类上增加@EnableGlobalMethodSecurity(securedEnabled = true)注解,secureEnabled默认为false,使其赋值为true才能使用相关注解。
@SpringBootApplication
@EnableGlobalMethodSecurity(securedEnabled = true)
public class XXXApplication {
public static void main(String[] args) {
SpringApplication.run(XXXApplication.class, args);
}
}
@PreAuthorize和@PostAuthorize进行访问控制
@PreAuthorize 注解,顾名思义是进入方法前的权