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 允许方法调用,但必须在进入方法之前过滤输入值
  • 相关阅读:
    SQL Server 索引结构及其使用(四)
    正确配置和使用SQL mail
    2进制、8进制、10进制、16进制...各种进制间的轻松转换(c#)
    配置远程服务器
    SQLServer基本函数
    将人民币的数字表示转化成大写表示(C#版)
    SQL Serer 索引全攻略
    亿众国际点对点文件传输程序
    表的相关操作
    windows文件副檔名說明
  • 原文地址:https://www.cnblogs.com/520playboy/p/7286085.html
Copyright © 2011-2022 走看看