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 允许方法调用,但必须在进入方法之前过滤输入值
  • 相关阅读:
    数据库02
    MySQL1
    GIL 死锁 递归锁 event 信号量 线程Queue
    小脚本 暴力删除文件 刷屏
    常见web攻击 及基础 回顾(杂记)
    接口中的简单异步 async
    python协程 示例
    python 利用jinja2模板生成html
    python 调用webservices 接口
    python 进程 进程池 进程间通信
  • 原文地址:https://www.cnblogs.com/520playboy/p/7286085.html
Copyright © 2011-2022 走看看