SpringSecurity框架学习与使用

news2024/9/20 9:28:29

SpringSecurity框架学习与使用

  • SpringSecurity学习
    • SpringSecurity入门
    • SpringSecurity深入
      • 认证
      • 授权
      • 自定义授权失败页面
      • 权限注解
        • @Secured
        • @PreAuthorize
        • @PostAuthorize
        • @PostFilter
        • @PreFilter
    • 参考

SpringSecurity学习

SpringSecurity入门

引入相关的依赖,SpringBoot的版本是2.7.10;

        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity6</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

前端页面编写,home.html、hello.html、login.html

hello.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
      xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity6">
<head>
    <title>Hello World!</title>
</head>
<body>
<h1 th:inline="text">Hello <span th:remove="tag" sec:authentication="name">thymeleaf</span>!</h1>
<form th:action="@{/logout}" method="post">
    <input type="submit" value="Sign Out"/>
</form>
</body>
</html>

home.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
<head>
    <title>Spring Security Example</title>
</head>
<body>
<h1>Welcome!</h1>

<p>Click <a th:href="@{/templates/hello.html}">here</a> to see a greeting.</p>
</body>
</html>

login.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
<head>
    <title>Spring Security Example </title>
</head>
<body>
<div th:if="${param.error}">
    Invalid username and password.
</div>
<div th:if="${param.logout}">
    You have been logged out.
</div>
<form th:action="@{/login}" method="post">
    <div><label> User Name : <input type="text" name="username"/> </label></div>
    <div><label> Password: <input type="password" name="password"/> </label></div>
    <div><input type="submit" value="Sign In"/></div>
</form>
</body>
</html>

视图控制,访问对应的url跳转到不同的页面

/**
 * 视图配置
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        WebMvcConfigurer.super.addViewControllers(registry);
        //请求/home时显示home.html页面
        registry.addViewController("/home").setViewName("home");
        registry.addViewController("/").setViewName("home");
        registry.addViewController("/hello").setViewName("hello");
        registry.addViewController("/login").setViewName("login");
    }
}

SpringSecurity配置

@Configuration
@EnableWebSecurity
public class WebSecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests(request -> {
                    request.antMatchers("/").permitAll()
		                    // /home、/  的请求可以访问
                            .antMatchers("/home").permitAll() 
                            //除了上面的请求,其它的请求必须认证通过
                            .anyRequest().authenticated();  
                })
                //设置登录页面以及允许访问登录页面,springSecurity是有自带的默认登录页面的,如果不		      	设置会跳转到默认的登录页面
                .formLogin((form) -> form.loginPage("/login").permitAll())  
                //允许访问登出页面
                .logout(LogoutConfigurer::permitAll);
        return http.build();
    }

	 /**
     * 设置默认的登录密码,这里是直接使用存放在内存中的密码;
     * 实际开发从数据库中查询读取
     * @return
     */
    @Bean
    public UserDetailsService userDetailsService() {
        UserDetails user = User.withDefaultPasswordEncoder()
                .username("user")
                .password("password")
                .roles("USER")
                .build();
        return new InMemoryUserDetailsManager(user);
    }
}

结果
在这里插入图片描述
登录失败时
在这里插入图片描述
登录成功
在这里插入图片描述

SpringSecurity深入

认证

上面的demo中,我们是把登录密码放在内存中记录着的,除了这种方式外我们还可以在配置文件中设置登录用户名和密码;
在这里插入图片描述
实际开发中一般都从数据库中进行读取;因此我们需要实现UserDetailsService接口,这个接口中有一个loadUserByUsername方法,我们在这个方法中根据username查询用户的信息,如果查询到了,就把用户的信息封装成UserDetails返回。
在这里插入图片描述

用户输入的密码会被我们注入的PasswordEncoder加密,所以在后面模拟的从数据库中查询用户密码的时候,对输入的密码使用PasswordEncoder加密了。

    /**
     * 密码加密
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

用户认证接口实现

@Service("userDetailService")
public class MyUserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //模拟从数据库中查询出密码
        if ("zhangsan".equals(username)) {
            username = "zhangsan";
            String password = passwordEncoder.encode("root");
            return new User(username, password, true, true, true, true,
                    AuthorityUtils.commaSeparatedStringToAuthorityList("admin, ROLE_SALES"));
        }
        throw new UsernameNotFoundException("用户没有找到");
    }
    
}

授权

前面我们对用户认证进行了讲解,接下来讲如何授权。
在SpringSecurity中用户认证和授权的过程是很紧密的,在loadUserByUsername方法返回的UserDetails的构造函数中最后一个参数就是用户具有的权限。而在Shiro中,授权和认证是分为两个方法的。

在SpringSecurity中我们通过AuthorizedUrl类的方法来确定访问指定的url需要的请求和角色。

方法作用
hasAuthority如果当前的主体具有指定的权限,则返回 true,否则返回 false
hasAnyAuthority如果当前的主体有任何提供的角色(给定的作为一个逗号分隔的字符串列表)的话,返回true
hasRole如果用户具备给定角色就允许访问,否则出现 403。如果当前主体具有指定的角色,则返回 true
hasAnyRole表示用户具备任何一个条件都可以访问

在这里插入图片描述
url:/test/test1,只有SALES角色才能访问;
url:/test/test2,只有admin权限才能访问;

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests(request -> {
                    request.antMatchers("/").permitAll()
                            .antMatchers("/home").permitAll() // /home、/  的请求可以访问
                            //需要SALES角色才可以访问
                            .antMatchers("/test/test1").hasRole("SALES")
                            //需要admin权限才能访问
                            .antMatchers("/test/test2").hasAuthority("admin")
                            .anyRequest().authenticated();  //除了上面的,其它的请求必须认证通过
                })
                .formLogin((form) -> form.loginPage("/login").permitAll())  //设置登录页面以及允许访问登录页面
                .logout(LogoutConfigurer::permitAll);
        return http.build();
    }

自定义授权失败页面

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests(request -> {
            request.antMatchers("/").permitAll()
                    .antMatchers("/home").permitAll() // /home、/  的请求可以访问
                    .antMatchers("/test/test1").hasRole("SALES")
                    .antMatchers("/test/test2").hasAuthority("admin")
                    .anyRequest().authenticated();  //除了上面的,其它的请求必须认证通过
        });
        //设置没有权限访问跳转自定义页面
        http.exceptionHandling().accessDeniedPage("/error.html");
        http.formLogin((form) -> {
                    //设置登录页面以及允许访问登录页面
                    form.loginPage("/login").permitAll()
                            //登录访问路径
                            .loginProcessingUrl("/login")
                            //登录成功之后的跳转路径
                            .defaultSuccessUrl("/hello").permitAll(); 
                })
                .logout(LogoutConfigurer::permitAll);
        return http.build();
    }

权限注解

注解作用
@Secured判断是否具有角色,另外需要注意的是这里匹配的字符串需要添加前缀“ROLE_
@PreAuthorize注解适合进入方法前的权限验证, @PreAuthorize 可以将登录用户的 roles/permissions 参数传到方法中
@PostAuthorize在方法执行后再进行权限验证,适合验证带有返回值的权限
@PostFilter权限验证之后对数据进行过滤 留下用户名是 admin1 的数据

@Secured

@Secured:判断是否具有角色,另外需要注意的是这里匹配的字符串需要添加前缀“ROLE_“
使用@Secured注解之前需要先使用注解@EnableGlobalMethodSecurity(securedEnabled=true)开启此功能;

@RequestMapping("testSecured")
@ResponseBody
@Secured({"ROLE_normal","ROLE_admin"}) //判断是否有normal、admin角色
public String helloUser() {
return "hello,user";
}

@PreAuthorize

@PreAuthorize:注解适合进入方法前的权限验证, @PreAuthorize 可以将登录用户的 roles/permissions 参数传到方法中;同样的使用之前也需要使用@EnableGlobalMethodSecurity(prePostEnabled = true)开启此功能;

@RequestMapping("/preAuthorize")
@ResponseBody
@PreAuthorize("hasAnyAuthority('menu:system')")
public String preAuthorize(){
 System.out.println("preAuthorize");
return "preAuthorize";
}

@PostAuthorize

@PostAuthorize:在方法执行后再进行权限验证,适合验证带有返回值的权限;

@RequestMapping("/testPostAuthorize")
@ResponseBody
@PostAuthorize("hasAnyAuthority('menu:system')")
public String preAuthorize(){
System.out.println("test--PostAuthorize");
return "PostAuthorize";
}

@PostFilter

@PostFilter :权限验证之后对数据进行过滤 留下用户名是 admin1 的数据;
表达式中的 filterObject 引用的是方法返回值 List 中的某一个元素;

@RequestMapping("getAll")
@PreAuthorize("hasRole('ROLE_管理员')")
@PostFilter("filterObject.username == 'admin1'")
@ResponseBody
public List<UserInfo> getAllUser(){
 ArrayList<UserInfo> list = new ArrayList<>();
 list.add(new UserInfo(1l,"admin1","6666"));
 list.add(new UserInfo(2l,"admin2","888"));
return list;
}

@PreFilter

@PreFilter: 进入控制器之前对数据进行过滤

@RequestMapping("getTestPreFilter")
@PreAuthorize("hasRole('ROLE_管理员')")
@PreFilter(value = "filterObject.id%2==0")
@ResponseBody
public List<UserInfo> getTestPreFilter(@RequestBody List<UserInfo>
list){
 list.forEach(t-> {
 System.out.println(t.getId()+"\t"+t.getUsername());
 });
return list;
}

除了上面提到的注解外,还有权限表达式,权限表达式

参考

  1. SpringSecurity视频
  2. SpringSecurity教程
  3. SpringSecurity文档

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/481247.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

IPsec中IKE与ISAKMP过程分析(快速模式-消息3)

IPsec中IKE与ISAKMP过程分析&#xff08;主模式-消息1&#xff09;_搞搞搞高傲的博客-CSDN博客 IPsec中IKE与ISAKMP过程分析&#xff08;主模式-消息2&#xff09;_搞搞搞高傲的博客-CSDN博客 IPsec中IKE与ISAKMP过程分析&#xff08;主模式-消息3&#xff09;_搞搞搞高傲的博客…

Spring - IoC

Spring - IoC Spring- IoC1)Spring简介1.1)什么是框架1.2)框架的作用1.3)Spring是什么1.4)Spring的体系结构1.5)Spring的发展历史1.6)Spring优势 2)IoC简介2.1)优质程序代码的制作原则2.2)耦合与内聚2.3)工厂模式发展史2.4)Spring发展历程2.5)IoC 3)入门案例3.1)案例环境说明3.…

synchronized用法加锁原理

目录 使用场景不同场景加锁对象结论验证实验实验1&#xff1a; synchronized 修饰方法&#xff0c;加锁对象是类实例&#xff0c;不同实例之间的锁互不影响实验2&#xff1a; synchronized 加在静态方法上&#xff0c;加锁对象是方法所在类&#xff0c;不同类实例之间相互影响实…

docker镜像导入导出

项目背景 1. docker pull 的时候比较慢的情况下&#xff0c; 之前已经下载过的话&#xff0c;可以直接导入使用 2. 有些服务器不允许上外网的情况下&#xff0c;可以先在自己电脑上下载好&#xff0c;再上传到服务器上进行安装 导出镜像 查看镜像 docker images 导出镜像 …

【文件系统】

目录 1 inode 2 软链接 3 硬链接 1 inode 当我们创建一个文件时&#xff0c;用带上 -i 选项可以查看文件的inode: 其中第一个选项就是文件的inode,除此之外另外几列表示的是&#xff1a; 模式 硬链接数 文件所有者 所属组 大小 最后修改时间文件名 ls -l读取存储在磁盘上的文…

windows编译安卓源码记录

环境 Windows10 vmware17 ubuntu22 ubuntu环境设置 装完ubuntu系统后拖拽复制文件进去验证vmtools安装情况&#xff0c;如果vmtools异常很麻烦&#xff0c;试了n多方法&#xff0c;最后还是重新安装系统解决&#xff0c; 如果ok的话&#xff0c;再继续下步骤&#xff0c;否…

【C++入门第四期】类和对象 ( 上 )

前言类的使用类的定义类的两种定义方式&#xff1a;成员变量名的定义建议 类的访问限定符类的作用域类的实列化如何计算类的大小结构体内存对齐规则 this指针this指针的特性 前言 C语言是面向过程的&#xff0c;关注的是过程&#xff0c;分析出求解问题的步骤&#xff0c;通过…

Linux的目录结构

在Linux世界里&#xff0c;一切皆文件硬件如显卡、cpu等都会映射成一个文件具体的目录结构/bin 是Binary的缩写&#xff0c;这个目录存放着最经常使用的命令 /sbin(/usr/sbin、/usr/local/sbin) s就是Super User的意思&#xff0c;这里存放的是系统管理员使用的系统管理程序 /h…

Elasticsearch扫盲篇

1. 什么是搜索&#xff1f; 在日常的工作和学习中&#xff0c;当我们说想找查询任何的信息的时候&#xff0c;可能第一时间会想到上百度或者谷歌去搜索一下。比如说找一部自己喜欢的电影&#xff0c;或者说找一本喜欢的书&#xff0c;或者找一条感兴趣的新闻。但是百度和谷歌不…

QML基础模型(Basic Model)

最基本的分离数据与显示的方法是使用Repeater元素。它被用于实例化一组元素项&#xff0c;并且很容易与一个用于填充用户界面的定位器相结合。 最基本的实现举例&#xff0c;repeater元素用于实现子元素的标号。每个子元素都拥有一个可以访问的属性index&#xff0c;用于区分不…

第11章 项目人力资源管理

文章目录 项目人力资源管理 过程11.2.1 编制项目人力资源计划的工具与技术 375&#xff08;1&#xff09;层次结构图&#xff08;工作、组织、资源 分解结构&#xff09;&#xff08;2&#xff09;矩阵图&#xff08;责任分配矩阵&#xff0c;RAM&#xff09;&#xff08;3&…

KinectFusion中的ICP算法

投影数据关联-求匹配点 利用算法projective data association对前一帧和当前帧的&#xff08;Vertex、Normal&#xff09;进行匹配&#xff0c;算法如下&#xff1a; 在当前帧 i 的深度图像上的每一个像素 U并行计算&#xff1b;对于深度值大于0的像素&#xff0c;求该像素点…

从破解虫脑到攻克人脑:一条“永生之路”的新赛道?

从破解虫脑到攻克人脑&#xff1a;一条“永生之路”的新赛道&#xff1f; 首张果蝇大脑连接组&#xff1a;耗费十余年&#xff0c;重建三千神经元&#xff0c;超50万突触&#xff01; 论文地址 果蝇幼虫大脑的连接组。 所有脑神经元的形态学都经过了突触分辨率的电子显微镜成像…

聊天机器人开发实战--(微信小程序+SpringCloud+Pytorch+Flask)【后端部分】

文章目录 前言架构SpringCloud服务构建后台搭建Python服务调用 Python算法服务app 总结 前言 趁着五一有时间&#xff0c;先把大三下个学期的期末作业做了&#xff0c;把微信小程序和Java开发的一起做了。顺便把机器学习的也一起做了。所以的话&#xff0c;我们完整项目的技术…

如何用ChatGPT做书籍、报告、文件的读取与互动式问答?故事人物活起来

该场景对应的关键词库&#xff08;15个&#xff09;&#xff1a; 书籍、报告、文件、详细信息、查询、主题、作者、出版日期、出版社、问题、方面、原则、方法、概括、主要观点、解释。 注意&#xff1a; ChatGPT的知识库截止于2021年9月&#xff0c;对于更新的数据或最新出版…

系统化思维:大数中心原理与限制性选择原理。

系统化思维&#xff1a;大数中心原理与限制性选择原理TOC 许多人的思考特点都是混乱而复杂的&#xff0c;只有受过严格训练的人才能做到系统化思维。这里将讨论系统化思维的基础考量。 大数中心原理&#xff1a;大数中心原理是客观而真实的普遍存在&#xff0c;应用在思维上就…

ImageJ实践——拟合矩形选区探究(bounding rectangle),左侧优先法则

在上一篇ImageJ实践中ImageJ实践——测量大小/长短&#xff08;以细胞为例&#xff09;&#xff0c;我勾选了Set Measurements中的Bounding rectangle以测量细胞的长和宽&#xff08;实际上是拟合矩形的长短边&#xff09;&#xff0c;文末我也提出了自己的疑惑&#xff1a;拟合…

【GORM框架】模型定义超详解,确定不来看看?

博主简介&#xff1a;努力学习的大一在校计算机专业学生&#xff0c;热爱学习和创作。目前在学习和分享&#xff1a;数据结构、Go&#xff0c;Java等相关知识。博主主页&#xff1a; 是瑶瑶子啦所属专栏: GORM框架学习 近期目标&#xff1a;写好专栏的每一篇文章 目录 一、GORM…

Ansible自动化运维工具---Playbook

Ansible自动化运维工具--playbook 一、playbook1、playbook简介2、playbook应用场景3、yaml基本语法规则4、yaml支持数据结构 二、Inventory中的变量1、inventor变量参数 三、playbook实例1、编写httpd的playbook2、tasks列表和action3、条件测试4、迭代5、with_items模块6、te…