前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >【Spring Security】004-Spring Security web权限方案(2):用户授权

【Spring Security】004-Spring Security web权限方案(2):用户授权

作者头像
訾博ZiBo
发布2025-01-06 17:11:42
发布2025-01-06 17:11:42
5500
代码可运行
举报
运行总次数:0
代码可运行

一、用户授权

1、基于权限访问控制

指定单个权限可访问:

hasAuthority 方法概述:

如果当前的主体具有指定的权限,则返回 true,否则返回 false;

只能指定一个权限,无法指定多个权限都可访问;

第一步:在配置类MySecurityConfig中设置当前访问地址有哪个权限才可以访问

第二步:在MyUserDetailsService中给返回的对象设置权限

第三步:访问测试

指定多个权限可访问:

hasAnyAuthority 方法概述:

如果当前的主体有任何提供的角色(给定的作为一个逗号分隔的字符串列表)的话,返回true;

简单地说就是:指定多个权限都可访问;

第一步:在配置类MySecurityConfig中设置当前访问地址有哪些权限才可以访问

第二步:访问测试

2、基于角色访问控制

指定单个角色可访问:

hasRole 方法概述:

如果用户具备给定角色就允许访问,否则出现 403;

如果当前主体具有指定的角色,则返回 true;

看一下源码:

基于角色与基于权限的访问使用起来基本一致,有下面的些许差异;

第一步:在配置类MySecurityConfig中设置当前访问地址有哪个角色才可以访问

第二步:在MyUserDetailsService中给返回的对象添加权限

注意在sale前面加上ROLE_;

第三步:访问测试

指定多个角色可访问:

hasAnyRole方法概述:

表示用户具备任何一个条件都可以访问;

第一步:在配置类MySecurityConfig中设置当前访问地址有哪些角色才可以访问

第二步:访问测试

3、自定义403页面

概述:

前面基于权限访问和基于角色访问,如果没有权限会挑战到403页面,我们这里对403页面进行自定义!

第一步:在配置类MySecurityConfig中设置跳转到自定义的403页面
第二步:在MyUserDetailsService中给返回的对象设置权限
第三步:编写自定义的403页面unauth.html
代码语言:javascript
代码运行次数:0
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>您没有相关访问权限!</h1>
</body>
</html>
第四步:访问测试

4、注解的使用

@Secured:

概述:

判断是否具有角色,另外需要注意的是这里匹配的字符串需要添加前缀“ROLE_“;

使用注解先要开启注解功能;

第一步:在启动类添加注解

@EnableGlobalMethodSecurity(securedEnabled=true)

第二步:在控制器TestController内方法上添加@Secured注解

代码语言:javascript
代码运行次数:0
复制
package com.zibo.studyspringsecurity.controller;

import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping("/hello")
    public String hello(){
        return "Hello Spring Security!";
    }

    @GetMapping("/index")
    public String index(){
        return "登录成功!";
    }

    // 这里我们新增一个方法,在这个方法上进行注解配置
    @GetMapping("/update")
    // @Secured("ROLE_sale") // 可以写一个
    @Secured({"ROLE_sale","ROLE_manage"}) // 可以写多个
    public String update(){
        return "您成功进行了更新!";
    }
}

第三步:在MyUserDetailsService中设置角色

第四步:启动项目,访问测试

访问http://localhost:8111/test/update

@PreAuthorize:

概述:

@PreAuthorize:注解适合进入方法前的权限验证, @PreAuthorize 可以将登录用户的 roles/permissions 参数传到方法中;

第一步:在启动类开启注解

@EnableGlobalMethodSecurity(prePostEnabled = true)

代码语言:javascript
代码运行次数:0
复制
package com.zibo.studyspringsecurity;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;

@SpringBootApplication
@EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled = true) // 开启security注解功能
public class StudyspringsecurityApplication {

    public static void main(String[] args) {
        SpringApplication.run(StudyspringsecurityApplication.class, args);
    }

}

第二步:在控制器TestController内方法上添加@PreAuthorize注解

第三步:在MyUserDetailsService中设置权限

第四步:重启项目,访问测试

访问http://localhost:8111/test/update

@PostAuthorize:

概述:

@PostAuthorize 注解使用并不多,在方法执行后再进行权限验证,适合验证带有返回值的权限

第一步:在启动类开启注解

@EnableGlobalMethodSecurity(prePostEnabled = true)

代码语言:javascript
代码运行次数:0
复制
package com.zibo.studyspringsecurity;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;

@SpringBootApplication
@EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled = true) // 开启security注解功能
public class StudyspringsecurityApplication {

    public static void main(String[] args) {
        SpringApplication.run(StudyspringsecurityApplication.class, args);
    }

}

第二步:在控制器TestController内方法上添加@PostAuthorize注解

第三步:在MyUserDetailsService中设置权限

第四步:重启项目,访问测试

访问http://localhost:8111/test/update

@PostFilter:

概述:

权限验证之后对数据进行过滤;

这个注解很不常用,也很简单,仅作用法说明,不再演示;

@PreFilter:

概述:

进入控制器之前对数据进行过滤;

这个注解很不常用,也很简单,仅作用法说明,不再演示;

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-01-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、用户授权
    • 1、基于权限访问控制
      • 指定单个权限可访问:
      • 指定多个权限可访问:
    • 2、基于角色访问控制
      • 指定单个角色可访问:
      • 指定多个角色可访问:
    • 3、自定义403页面
      • 概述:
      • 第一步:在配置类MySecurityConfig中设置跳转到自定义的403页面
      • 第二步:在MyUserDetailsService中给返回的对象设置权限
      • 第三步:编写自定义的403页面unauth.html
      • 第四步:访问测试
    • 4、注解的使用
      • @Secured:
      • @PreAuthorize:
      • @PostAuthorize:
      • @PostFilter:
      • @PreFilter:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档