spring security 注解@EnableGlobalM

2018-03-29  本文已影响0人  huoyl0410

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

來源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

上一篇下一篇

猜你喜欢

热点阅读