Geeks_Z の Blog Geeks_Z の Blog
首页
  • 学习笔记

    • 《HTML》
    • 《CSS》
    • 《JavaWeb》
    • 《Vue》
  • 后端文章

    • Linux
    • Maven
    • 汇编语言
    • 软件工程
    • 计算机网络概述
    • Conda
    • Pip
    • Shell
    • SSH
    • Mac快捷键
    • Zotero
  • 学习笔记

    • 《数据结构与算法》
    • 《算法设计与分析》
    • 《Spring》
    • 《SpringMVC》
    • 《SpringBoot》
    • 《SpringCloud》
    • 《Nginx》
  • 深度学习文章
  • 学习笔记

    • 《PyTorch》
    • 《ReinforementLearning》
    • 《MetaLearning》
  • 学习笔记

    • 《高等数学》
    • 《线性代数》
    • 《概率论与数理统计》
  • 增量学习
  • 哈希学习
GitHub (opens new window)

Geeks_Z

AI小学生
首页
  • 学习笔记

    • 《HTML》
    • 《CSS》
    • 《JavaWeb》
    • 《Vue》
  • 后端文章

    • Linux
    • Maven
    • 汇编语言
    • 软件工程
    • 计算机网络概述
    • Conda
    • Pip
    • Shell
    • SSH
    • Mac快捷键
    • Zotero
  • 学习笔记

    • 《数据结构与算法》
    • 《算法设计与分析》
    • 《Spring》
    • 《SpringMVC》
    • 《SpringBoot》
    • 《SpringCloud》
    • 《Nginx》
  • 深度学习文章
  • 学习笔记

    • 《PyTorch》
    • 《ReinforementLearning》
    • 《MetaLearning》
  • 学习笔记

    • 《高等数学》
    • 《线性代数》
    • 《概率论与数理统计》
  • 增量学习
  • 哈希学习
GitHub (opens new window)
  • Linux

  • 数据结构与算法

  • 算法设计与分析

  • Java

  • Python

  • 设计模式

  • 计算机网络

  • Spring笔记

  • SpringMVC笔记

  • SpringBoot笔记

  • SpringSecurity

    • SpringSecurity
    • SpringSecurity简介
    • SpringSecurity Web 权限方案
    • 新建 Markdown
    • 新建 Markdown
    • Handler
    • 注解使用
      • CSRF
      • 访问控制
    • Elasticsearch笔记

    • RabbitMQ笔记

    • Docker笔记

    • MySQL

    • Redis

    • Mybatis

    • MybatisPlus

    • Nginx

    • Kubernetes笔记

    • Git

    • Software

    • 微服务笔记

    • bug

    • BackEndNotes
    • SpringSecurity
    Geeks_Z
    2023-09-04
    目录

    注解使用

    @Secured

    判断是否具有角色,另外需要注意的是这里匹配的字符串需要添加前缀 ROLE_。 类似hasAnyRole方法

    开启注解功能

    使用 @Secured 注解先要开启注解功能!@EnableGlobalMethodSecurity(securedEnabled = true)

    @SpringBootApplication
    @EnableGlobalMethodSecurity(securedEnabled = true)
    public class SpringSecurityApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringSecurityApplication.class, args);
        }
    
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9

    Controller

    @RestController
    public class AnnotationController {
        // 告诉SpringSecurity访问这个方法需要用户具有ROLE_admin或ROLE_manager角色才能访问成功
        @Secured({"ROLE_admin", "ROLE_manager"})
        @GetMapping("/testSecured")
        public String testSecured(){
            return "hello secured";
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9

    @PreAuthorize

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

    开启注解功能

    使用 @PreAuthorize 注解先要开启注解功能!@EnableGlobalMethodSecurity(prePostEnabled = true)

    Controller

    @MapperScan("org.hong.springsecurity01.mapper")
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    @SpringBootApplication
    public class Springsecurity01Application {
        public static void main(String[] args) {
            SpringApplication.run(Springsecurity01Application.class, args);
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8

    @PostAuthorize

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

    开启注解功能

    使用 @PostAuthorize 注解先要开启注解功能!@EnableGlobalMethodSecurity(prePostEnabled = true)

    Controller

    // 使用与@PerAuthorize注解类似
    @PostAuthorize("hasAuthority('admin')")
    @GetMapping("/testPostAuthorize")
    public String testPostAuthorize(){
        System.out.println("update");
        return "hello";
    }
    
    1
    2
    3
    4
    5
    6
    7

    @PreFilte

    @PreFilter: 进入控制器之前对数据进行过滤,Spring Security将移除使对应表达式的结果为false的元素。

    @PreFilter("filterObject.length() > 2")
    @GetMapping("/testPerFilter")
    public String testPerFilter(@RequestParam ArrayList<String> list){
        System.out.println(list.toString() + list.size());
        return "hello testPerFilter";
    }
    
    1
    2
    3
    4
    5
    6

    只能对集合进行过滤,filterObject 代表集合中的项。

    @PostFilter

    @PostFilter :对方法返回的数据进行过滤,Spring Security将移除使对应表达式的结果为false的元素。

    @PostFilter("filterObject.length() > 2")
    @GetMapping("/testPostFilter")
    public List<String> testPostFilter(){
        List<String> list = new ArrayList<>();
        list.add("abc");
        list.add("ab");
        return list;
    }
    
    1
    2
    3
    4
    5
    6
    7
    8

    只能对集合进行过滤,filterObject 代表集合中的项。

    用户注销

    logout.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <h5>登录成功</h5>
        <a href="/logout">登出</a>
    </body>
    </html>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    修改配置类

    修改 configure(HttpSecurity http) 方法

    // 退出配置
    http.logout()
        .logoutUrl("/logout") // 设置登出访问的url, 这个controller不需要我们定义
        .logoutSuccessUrl("/login.html"); // 登出成功后的跳转页面
    
    1
    2
    3
    4
    上次更新: 2024/03/29, 08:56:31
    Handler
    CSRF

    ← Handler CSRF→

    最近更新
    01
    并行训练
    03-29
    02
    tensor维度转换
    03-26
    03
    ResNet源码解读
    03-23
    更多文章>
    Theme by Vdoing | Copyright © 2022-2024 Geeks_Z | MIT License
    京公网安备 11010802040735号 | 京ICP备2022029989号-1
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式