学习Spring Security前我们需要了解RBAC角色权限控制
看看别人总结的BAC资源只给有对应权限的人访问

RBAC的数据库表设计:

springsecurity如何查看当前角色的权限 spring security 角色 权限_java

1.先给用户添加角色

第一种(前期整合授权的时候可以用来测试一下)
直接再配置文件中定义角色

spring.security.user.roles=manager

第二种*(new 一个User示例)
User中填三个参数,账号密码跟角色

List<GrantedAuthority> roles = new ArrayList<>();
//GrantedAuthority视为一种角色或者权限,角色开头必须添加ROLE
//new SimpleGrantedAuthority把字符串转成GrantedAuthority
roles.add(new SimpleGrantedAuthority("ROLE_"+ADMIN));
//(这里是简单的展示,密码要加密器加密)
User user = new User("lisi","123456",roles);

2. 基于角色的授权

用户拥有一个特定或者多个角色才可以访问对应的资源。

前两种方式用得比较多

第一种

  • 使用@PreAuthorize,@PostAuthorize注解的方式:
  • 需要在Security配置类添加启动注解@EnableGlobalMethodSecurity(prePostEnabled = true)
    @PreAuthorize,这个是在用户访问之前拦截
    解析如果你没有与资源匹配的角色,访问不了,后台对应业务不执行
拥有ADMIN角色的用户才可以访问
    //NO.1_1
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/admin")
    public Object admin(){
        System.out.println("运行到这里了");
        return "hello SpringBoot admin";
    }
    没有对应角色的用户后台不打印输出

@PostAuthorize,这个是在用户访问之后拦截
解析:如果你没有与资源匹配的角色,访问不了,后台对应业执行

拥有ADMIN角色的用户才可以访问
    //NO.1_2
    @PostAuthorize("hasRole('ADMIN')")
    @GetMapping("/admin")
    public Object admin(){
        System.out.println("运行到这里了");
        return "hello SpringBoot admin";
    }
    没有对应角色的用户后台打印输出
多个角色可以这样写
@PreAuthorize("hasRole('manager')  AND  hasRole('worker')")

第二种
在Security配置文件中完成

http.authorizeRequests()
具有角色ADMIN的用户可以访问  /admin
.antMatchers("/admin").hasRole("ADMIN")
具有角色ADMIN或MANAGER者的用户可以访问  /admin
.antMatchers("/admin").hasAnyRole("ADMIN","MANAGER")
具有角色ADMIN的用户可以访问  /admin及 /admin/之后的任何url
只有一个*只允许多访问下一级
.antMatchers("/admin/**").hasRole("ADMIN")

第三种(这个及下面的方法都是用得比较少的)
编码的方式,主要是获取上下文。判断改用户角色列表中有没有包含目标角色,包含再放行关键业务操作。

if (!SecurityContextHolder.getContext().getAuthentication().getAuthorities().contains("ROLE_ADMIN")) {
            throw new AccessDeniedException("你没有权限访问");
        }

第四种
@Secured
需要在Security配置类添加启动注解配置
@EnableGlobalMethodSecurity(securedEnabled = true)

@Secured("ROLE_worker")
@Secured("ROLE_worker", "ROLE_manager")
@GetMapping("/admin")
    public Object admin(){
        System.out.println("运行到这里了");
        return "hello SpringBoot admin";
    }

第五种
@RolesAllowed
需要在Security配置类添加启动注解配置
@EnableGlobalMethodSecurity(jsr250Enabled = true)

@RolesAllowed("manager")
@RolesAllowed("manager", “worker")
@GetMapping("/admin")
    public Object admin(){
        System.out.println("运行到这里了");
        return "hello SpringBoot admin";
    }

@EnableGlobalMethodSecurity启动注解可以一次性开启多种角色授权方式
@EnableGlobalMethodSecurity(securedEnabled = true, jsr250Enabled = true, prePostEnabled = true)

3.基于权限的授权

首先我们要给用户初始化权限

跟设置角色一样,用户的权限我们加在 List< GrantedAuthority > roles 中,跟角色的区别在于:角色要以**ROLE_**开头

roles.add(new SimpleGrantedAuthority("admin:in"));

权限授权有两种用法:
第一种
在控制类中绑定注解,@PreAuthorize ,@PostAuthorize

@PreAuthorize("hasAuthority('admin:in')")
    @GetMapping("/admin/in")
    public Object in() {
        return "入库";
    }

第二种
在Security配置文件中去设置权限

http.authorizeRequests()
.antMatchers("/admin/in").hasAuthority("admin:in");

4.基于用户的授权

这种授权方式,面向于高级用户的授权,比如公司老总
给指定的用户授权,只需要根据用户账号放权就可以了。
第一种(用编程的方式)
编码去获取登录用户上下文

方式1
    @GetMapping("/onlyAdmin")
    public Object onlyAdmin() {
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        if (!name.equals("admin")){
            throw new RuntimeException("你不是管理员");
        }

        return "只有管理可以访问";
    }
方式2 自动传参的方式
    @GetMapping("/onlyAdmin")
    public Object onlyAdmin(Principal principal) {
        String name = principal.getName();
        if (!name.equals("admin")){
            throw new RuntimeException("你不是管理员");
        }

        return "只有管理可以访问";
    }

第二种(使用注解@PreAuthorize,@PostAuthorize)

@PreAuthorize("authentication.name=='admin'")
    @GetMapping("/onlyAdmin")
    public Object onlyAdmin(Principal principal) {
        return "只有管理可以访问";
    }

Spring Security授权用法就是这么简单,大家设计数据好数据库测试一下吧。