spring security 注解@EnableGlobalMethodSecurity详解

1、Spring Security默认是禁用注解的,要想开启注解, 需要在继承WebSecurityConfigurerAdapter的类上加@EnableGlobalMethodSecurity注解, 来判断用户对某个控制层的方法是否具有访问权限 

@Configuration

@EnableWebSecurity

@EnableAutoConfiguration

@EnableGlobalMethodSecurity(prePostEnabled =true)

public class WebSecurityConfigextends WebSecurityConfigurerAdapter {……}

2、例如下面代码就表示如果用户具有admin角色,就能访问listAllUsers方法,但是如果方法前不加@preAuthorize注解,意味着所有用户都能访问listAllUsers

    @PreAuthorize("hasRole(‘admin‘)")

    @RequestMapping(value = "/user/", method = RequestMethod.GET)

    @ResponseBody

    publicList listAllUsers() {

        ……

    }

3、@EnableGlobalMethodSecurity详解

3.1、@EnableGlobalMethodSecurity(securedEnabled=true) 开启@Secured 注解过滤权限

3.2、@EnableGlobalMethodSecurity(jsr250Enabled=true)开启@RolesAllowed 注解过滤权限 

3.3、@EnableGlobalMethodSecurity(prePostEnabled=true) 使用表达式时间方法级别的安全性         4个注解可用

@PreAuthorize 在方法调用之前,基于表达式的计算结果来限制对方法的访问

@PostAuthorize 允许方法调用,但是如果表达式计算结果为false,将抛出一个安全性异常

@PostFilter 允许方法调用,但必须按照表达式来过滤方法的结果

@PreFilter 允许方法调用,但必须在进入方法之前过滤输入值



作者:谁在烽烟彼岸
链接:https://www.jianshu.com/p/41b7c3fb00e0
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值