zoukankan      html  css  js  c++  java
  • spring security 注解@EnableGlobalMethodSecurity详解

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

    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class OAuth2SecurityConfiguration extends WebSecurityConfigurerAdapter {
    ...........................
    }

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

    方法

        @PreAuthorize("hasRole('admin')")
        @RequestMapping(value = "/user/", method = RequestMethod.GET)
        @ResponseBody
        public List<User> listAllUsers() {
            List<User> users = userService.findAll();
            if(users.isEmpty()){
                return null;
            }
            return users;
        }
      

    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 允许方法调用,但必须在进入方法之前过滤输入值
  • 相关阅读:
    第5章 css与背景相关的样式background
    第4章 css文字text与字体font-face
    第3章 css属性color的RGBA值
    第2章 css边框属性
    第1章 初识CSS3
    CSS单行、多行文本溢出显示省略号(……)解决方案
    Access-Control-Allow-Origin实现跨域访问 跨域
    Mysql主数据库+备份数据库部署教程
    PHP 数据安全问题总结
    PHP redis 批量操作
  • 原文地址:https://www.cnblogs.com/520playboy/p/7286085.html
Copyright © 2011-2022 走看看