当前位置: 首页 > news >正文

进阶SpringBoot之 SpringSecurity(2)用户认证和授权

Spring Security 官网

Spring Security 是针对 Spring 项目的安全框架

也是 Spring Boot 底层安全模块默认的技术选型

它可以实现强大的 Web 安全控制

只需引入 spring-boot-starter-security 模块,进行少量配置,即可实现强大的安全管理

几个重要的类:

WebSecurityConfigurerAdapter:自定义 Security 策略(从 5.7.0-M2 起已被弃用)

Spring Security:升级已弃用的 WebSecurityConfigurerAdapter - spring 中文网 (springdoc.cn)

AuthenticationManagerBuilder:自定义认证策略

@EnableWebSecurity:开启 WebSecurity 模式

Spring Security 的主要目标是认证授权(访问控制)

认证:Authentication

授权:Authorization

概念通用,只要涉及到安全都有

pom.xml 文件导入 security 依赖

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

Maven 导入成功

新版 Spring Security 中的路径匹配机制 - spring 中文网 (springdoc.cn)

编写过程中,antMatchers 发现没有,搜索后发现新版本已被移除(看老版本教学视频好痛苦)

使用 requestMatchers 来进行替代

config 包下创建 SecurityConfig 类:

package com.demo.security.config;import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;//AOP横切:拦截器
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
public class SecurityConfig {//授权@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {//首页所有人都可以访问,功能页只有对应有权限的人才能访问http.authorizeRequests().requestMatchers("/").permitAll().requestMatchers("/level1/**").hasRole("vip1").requestMatchers("/level2/**").hasRole("vip2").requestMatchers("/level3/**").hasRole("vip3");//没有权限默认会到登录页面,需要开启登录页面http.formLogin();return http.build();}//认证@Beanpublic AuthenticationManager authenticate(AuthenticationManagerBuilder auth) throws Exception{//这些数据整除应该从数据库中读取,密码需要加密处理auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("demo").password(new BCryptPasswordEncoder().encode("demo")).roles("vip2","vip3").and().withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3").and().withUser("root").password(new BCryptPasswordEncoder().encode("root")).roles("vip1");return auth.build();}}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • TIM输出比较之PWM驱动直流电机应用案例
  • UEFI启动流程
  • 《黑神话:悟空》到底是用什么语言开发的
  • 从0到1构建视频汇聚生态:EasyCVR视频汇聚平台流媒体协议支持的前瞻性布局
  • 依靠 VPN 生存——探索 VPN 后利用技术
  • 【Python】列表和元组
  • 两个独立的SpringBoot项目如何互相引用?
  • 20240824给飞凌OK3588-C的核心板刷Ubuntu22.04后适配SONY索尼的HDMI OUT的机芯8530
  • 【通俗易懂】限流、降级、熔断有什么区别?
  • C语言刷题日记(附详解)(2)
  • 防患未然:构建AIGC时代下开发团队应对突发技术故障与危机的全面策略
  • 代码随想录算法训练营day30 | 贪心算法 | 452.用最少数量的箭引爆气球、435.无重叠区间、763.划分字母区间
  • PostgreSQL SELECT 语句:深入解析与实例应用
  • vscode修改选中文字颜色及当前tab颜色
  • web前端之选项卡集合、动态添加类名、动态移除类名、动态添加样式、激活、间距、节流、tabBar
  • 「前端」从UglifyJSPlugin强制开启css压缩探究webpack插件运行机制
  • 【跃迁之路】【699天】程序员高效学习方法论探索系列(实验阶段456-2019.1.19)...
  • go语言学习初探(一)
  • java取消线程实例
  • Java新版本的开发已正式进入轨道,版本号18.3
  • laravel5.5 视图共享数据
  • Linux下的乱码问题
  • macOS 中 shell 创建文件夹及文件并 VS Code 打开
  • PhantomJS 安装
  • uva 10370 Above Average
  • yii2中session跨域名的问题
  • 编写符合Python风格的对象
  • 翻译--Thinking in React
  • 关于字符编码你应该知道的事情
  • 欢迎参加第二届中国游戏开发者大会
  • 手机端车牌号码键盘的vue组件
  • 突破自己的技术思维
  • 想写好前端,先练好内功
  • 延迟脚本的方式
  • 白色的风信子
  • 如何在 Intellij IDEA 更高效地将应用部署到容器服务 Kubernetes ...
  • 新海诚画集[秒速5センチメートル:樱花抄·春]
  • ​力扣解法汇总946-验证栈序列
  • ​软考-高级-系统架构设计师教程(清华第2版)【第9章 软件可靠性基础知识(P320~344)-思维导图】​
  • # AI产品经理的自我修养:既懂用户,更懂技术!
  • #nginx配置案例
  • #VERDI# 关于如何查看FSM状态机的方法
  • $分析了六十多年间100万字的政府工作报告,我看到了这样的变迁
  • (07)Hive——窗口函数详解
  • (1)虚拟机的安装与使用,linux系统安装
  • (7)STL算法之交换赋值
  • (ctrl.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MDd_DynamicDebug”不匹配值“
  • (C语言)strcpy与strcpy详解,与模拟实现
  • (Python) SOAP Web Service (HTTP POST)
  • (Redis使用系列) Springboot 使用redis的List数据结构实现简单的排队功能场景 九
  • (vue)页面文件上传获取:action地址
  • (二)Pytorch快速搭建神经网络模型实现气温预测回归(代码+详细注解)
  • (二)测试工具
  • (附源码)计算机毕业设计SSM疫情社区管理系统
  • (六)Hibernate的二级缓存