新版本Spring Security 2.7 + 用法,直接旧正版粘贴
一、以前的用法:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Beanpublic PasswordEncoder passwordEncoder(){return new BCryptPasswordEncoder();}@Overrideprotected void configure(HttpSecurity http) throws Exception {http//关闭csrf.csrf().disable()//不通过Session获取SecurityContext.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests()// 对于登录接口 允许匿名访问.antMatchers("/user/login").anonymous()// 除上面外的所有请求全部需要鉴权认证.anyRequest().authenticated();}@Bean@Overridepublic AuthenticationManager authenticationManagerBean() throws Exception {return super.authenticationManagerBean();}
}
WebSecurityConfigurerAdapter 已经过时了,新版本已经不用这个了。
二、现在的用法
使用@EnableWebSecurity注解
@Configuration
@EnableWebSecurity
public class SecurityConfig{//配置密码加密器@Beanpublic PasswordEncoder passwordEncoder(){return new BCryptPasswordEncoder();}/*** 安全配置*/@BeanSecurityFilterChain filterChain(HttpSecurity http) throws Exception {http.csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests().antMatchers("/user/login").anonymous().anyRequest().authenticated();return http.build();}/*** 认证管理器,登录的时候参数会传给 authenticationManager*/@Bean(name = BeanIds.AUTHENTICATION_MANAGER)public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {return authenticationConfiguration.getAuthenticationManager();}
}
三、其他的一些配置
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SpringSecurityConfig {@Resourceprivate CustomAuthenticationSuccessHandler customAuthenticationSuccessHandler;@Resourceprivate CustomAuthenticationFailureHandler customAuthenticationFailureHandler;@Resourceprivate CustomAuthenticationEntryPoint customAuthenticationEntryPoint;@Resourceprivate CustomLogoutHandler customLogoutHandler;@Resourceprivate CustomLogoutSuccessHandler customLogoutSuccessHandler;@Resourceprivate CustomAccessDeniedHandler customAccessDeniedHandler;@Resourceprivate SecurityProperties securityProperties;@Resourceprivate JwtStoreService jwtStoreService;@Resourceprivate UserDetailsServiceImpl userDetailsService;@Resourceprivate AuthenticationConfiguration authenticationConfiguration;/*** 静态文件放行*/@Beanpublic WebSecurityCustomizer webSecurityCustomizer() {return (web) -> web.ignoring().antMatchers(securityProperties.getStaticPaths());}/*** 取消ROLE_前缀*/@Beanpublic GrantedAuthorityDefaults grantedAuthorityDefaults() {// Remove the ROLE_ prefixreturn new GrantedAuthorityDefaults("");}/*** 设置密码编码器*/@Beanpublic PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}/*** 设置中文配置*/@Beanpublic ReloadableResourceBundleMessageSource messageSource() {ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();messageSource.setBasename("classpath:org/springframework/security/messages_zh_CN");return messageSource;}/*** 认证管理器,登录的时候参数会传给 authenticationManager*/@Beanpublic AuthenticationManager authenticationManager() throws Exception {return authenticationConfiguration.getAuthenticationManager();}/*** 设置默认认证提供*/@Beanpublic DaoAuthenticationProvider daoAuthenticationProvider() {final DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();authenticationProvider.setUserDetailsService(userDetailsService);authenticationProvider.setPasswordEncoder(passwordEncoder());return authenticationProvider;}/*** 安全配置*/@Beanpublic SecurityFilterChain securityFilterChain(HttpSecurity http, AuthenticationConfiguration authenticationConfiguration) throws Exception {// 表单http.formLogin()// 登录成功处理器.successHandler(customAuthenticationSuccessHandler)// 登录错误处理器.failureHandler(customAuthenticationFailureHandler).and()//添加登录逻辑拦截器,不使用默认的UsernamePasswordAuthenticationFilter.addFilterBefore(new CustomUsernamePasswordAuthenticationFilter(authenticationManager(),customAuthenticationSuccessHandler,customAuthenticationFailureHandler), UsernamePasswordAuthenticationFilter.class)//添加token验证过滤器.addFilterBefore(new JwtAuthenticationFilter(jwtStoreService), LogoutFilter.class);//退出http.logout()// URL.logoutUrl("/user/logout")// 登出处理.addLogoutHandler(customLogoutHandler)// 登出成功处理.logoutSuccessHandler(customLogoutSuccessHandler);//拦截设置http.authorizeRequests()//公开以下urls.antMatchers(securityProperties.getPublicPaths()).permitAll()//其他路径必须验证.anyRequest().authenticated();//异常处理http.exceptionHandling()// 未登录处理.authenticationEntryPoint(customAuthenticationEntryPoint)// 无权限处理.accessDeniedHandler(customAccessDeniedHandler);//关闭sessionhttp.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);// 关闭corshttp.cors().disable();// 关闭csrfhttp.csrf().disable();// 关闭headershttp.headers().frameOptions().disable();return http.build();}
}
————————
如觉不错,随手点赞,关注,收藏(* ̄︶ ̄),谢谢~~
相关文章:
新版本Spring Security 2.7 + 用法,直接旧正版粘贴
一、以前的用法: Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter {Beanpublic PasswordEncoder passwordEncoder(){return new BCryptPasswordEncoder();}Overrideprotected void configure(HttpSecurity http) throws Exceptio…...
JVM——类加载器(JDK8及之前,双亲委派机制)
目录 1.类加载器的分类1.实现方式分类1.虚拟机底层实现2.JDK中默认提供或者自定义 2.类加载器的分类-启动类加载器3.类加载器的分类-Java中的默认类加载器4.类加载器的分类-扩展类加载器5.类加载器的分类-类加载器的继承 2.类加载器的双亲委派机制 类加载器(ClassLo…...
(七)什么是Vite——vite优劣势、命令
vite分享ppt,感兴趣的可以下载: Vite分享、原理介绍ppt 什么是vite系列目录: (一)什么是Vite——vite介绍与使用-CSDN博客 (二)什么是Vite——Vite 和 Webpack 区别࿰…...
vue之Error: Unknown option: .devServer.
背景 在使用内网穿透工具时,加入对应的配置,启动出现报错。 一、遇到的问题 报错: Error: Unknown option: .devServer. Check out https://babeljs.io/docs/en/babel-core/#options for more information about options. Error: Unknown …...
基于ssm的房屋租售网站(有报告)。Javaee项目,ssm项目。
演示视频: 基于ssm的房屋租售网站(有报告)。Javaee项目,ssm项目。 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。 项目介绍: 采用M(mode…...
LeeCode AutoX-4 计算几何
题意 传送门 LeeCode AutoX-4 蚂蚁爬行 题解 枚举每一对几何图形,判断相交性,用并查集维护连通性即可。总时间复杂度 O ( n 2 m ) O(n^2 m) O(n2m),其中 n n n 为几何图形数量, m m m 为查询数量。 根据几何图形性质分类讨…...
Vue3 动态设置 ref
介绍 在一些场景,ref设置是未知的需要根据动态数据来决定,如表格中的input框需要我们主动聚焦,就需要给每一个input设置一个ref,进而进行聚焦操作。 Demo 点击下面截图中的编辑按钮,自动聚焦到相应的输入框中。 &…...
fast lio 2 保存每一帧的点云PCD和里程计矩阵 Odom 在txt文件
修改了源代码的 laserMapping.cpp 文件,替换为下面的代码就可以保存了,注意里面有一个路径,需要修改为你的电脑的路径 // This is an advanced implementation of the algorithm described in the // following paper: // J. Zhang and S. Singh. LOAM: Lidar Odometry an…...
当前主流DDos方式有哪几类
随着互联网的普及和技术的进步,网络安全问题日益凸显。DDoS攻击作为其中一种常见且具破坏性的攻击方式,受到了广泛关注。小德将带领大家一起来了解当前流行的三种DDoS攻击方式。 1. 容量耗尽攻击 容量耗尽攻击是最常见也是最直接的DDoS攻击方式。攻击者通…...
神经网络常见评价指标AUROC(AUC-ROC)、AUPR(AUC-PR)
神经网络的性能可以通过多个评价指标进行衡量,具体选择哪些指标取决于任务的性质。以下是神经网络中常见的评价指标: 准确性(Accuracy): 准确性是最常见的分类任务评价指标,表示模型正确预测的样本数占总样…...
Apache Doris安装部署
Apache Doris安装部署 版本: CentOS 7.6 Apache Doris 0.14.0 编译 选择合适的版本进行下载,此次选择0.14.0版本 下载 | Apache Doris 一、CentOS编译 1 安装依赖 sudo yum groupinstall Development Tools && sudo yum install maven c…...
Excel查询时用vlookup或者xlookup时,虽然用的参数选择的是精确匹配,但是发现不能区分大小写,应该如何解决?
Excel查询时用vlookup或者xlookup时,虽然用的参数选择的是精确匹配,但是发现不能区分大小写,应该如何解决? Index函数解决 INDEX([excel1.xlsx]Sheet1!$E:$E,MATCH(1,EXACT(G5,[excel1.xlsx]Sheet1!$E:$E)*1,0))重点说明&#x…...
4种经典的限流算法
0、基础知识 1000毫秒内,允许2个请求,其他请求全部拒绝。 不拒绝就可能往db打请求,把db干爆~ interval 1000 rate 2; 一、固定窗口限流 固定窗口限流算法(Fixed Window Rate Limiting Algorithm)是…...
<MySQL> 什么是数据库事务?事务该如何使用?
目录 一、事务的概念 二、事务的核心特性 三、事务操作中的常见BUG 3.1 脏读 3.2 不可重复读 3.3 幻读 四、隔离级别 五、使用事务 一、事务的概念 “事务”是指一组操作,在逻辑上是不可分割的,组成这组操作的各个语句,或者全部执行成…...
Linux 网络:PMTUD 简介
文章目录 1. 前言2. Path MTU Discovery(PMTUD) 协议2.1 PMTUD 发现最小 MTU 的过程 3. Linux 的 PMTUD 简析3.1 创建 socket 时初始化 PMTUD 模式3.2 数据发送时 PMTUD 相关处理3.2.1 源头主机发送过程中 PMTU 处理3.2.2 转发过程中 PMTUD 处理 4. PMTUD 观察5. 参考链接 1. 前…...
BatchNormalization:解决神经网络中的内部协变量偏移问题
ICML2015 截至目前51172引 论文链接 代码连接(planing) 文章提出的问题 减少神经网络隐藏层中的”内部协变量偏移”问题。 在机器学习领域存在“协变量偏移”问题,问题的前提是我们划分数据集的时候,训练集和测试集往往假设是独立同分布(i.i.d)的,这种独立同分布更有利于…...
DAC实验(DAC 输出三角波实验)(DAC 输出正弦波实验)
DAC 输出三角波实验 本实验我们来学习使用如何让 DAC 输出三角波,DAC 初始化部分还是用 DAC 输出实验 的,所以做本实验的前提是先学习 DAC 输出实验。 使用 DAC 输出三角波,通过 KEY0/KEY1 两个按键,控制 DAC1 的通道 1 输出两种…...
许多网友可能还不知道,升级到Windows 11其实没那么复杂,只要符合几个条件可以了
如果你的Windows 10电脑可以升级Windows 11,现在怎么办?有几种方法可以免费安装新的操作系统。以下是你的选择。 如果你想升级到Windows 11,你可以随时购买一台已经安装了操作系统的新电脑。然而,如果你目前的Windows 10 PC满足所有必要的升级要求,那么在Windows 11免费的…...
ubuntu下载conda
系统:Ubuntu18.04 (1)下载安装包 wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2021.11-Linux-x86_64.sh 报错错误 403:Forbidden 解决方法 wget -U NoSuchBrowser/1.0 https://mirrors.tuna.tsingh…...
重磅 | 进一步夯实生态建设,朗思科技与阿里龙蜥完成兼容性认证
近日,北京朗思智能科技有限公司(以下简称“朗思科技”)自主研发的数字员工产品与OpenAnolis龙蜥社区龙蜥操作系统(Anolis OS)8完成兼容性认证。测试结果显示,双方产品相互兼容,功能正常…...
Python|GIF 解析与构建(5):手搓截屏和帧率控制
目录 Python|GIF 解析与构建(5):手搓截屏和帧率控制 一、引言 二、技术实现:手搓截屏模块 2.1 核心原理 2.2 代码解析:ScreenshotData类 2.2.1 截图函数:capture_screen 三、技术实现&…...
网络六边形受到攻击
大家读完觉得有帮助记得关注和点赞!!! 抽象 现代智能交通系统 (ITS) 的一个关键要求是能够以安全、可靠和匿名的方式从互联车辆和移动设备收集地理参考数据。Nexagon 协议建立在 IETF 定位器/ID 分离协议 (…...
web vue 项目 Docker化部署
Web 项目 Docker 化部署详细教程 目录 Web 项目 Docker 化部署概述Dockerfile 详解 构建阶段生产阶段 构建和运行 Docker 镜像 1. Web 项目 Docker 化部署概述 Docker 化部署的主要步骤分为以下几个阶段: 构建阶段(Build Stage):…...
【OSG学习笔记】Day 18: 碰撞检测与物理交互
物理引擎(Physics Engine) 物理引擎 是一种通过计算机模拟物理规律(如力学、碰撞、重力、流体动力学等)的软件工具或库。 它的核心目标是在虚拟环境中逼真地模拟物体的运动和交互,广泛应用于 游戏开发、动画制作、虚…...
【Linux】C语言执行shell指令
在C语言中执行Shell指令 在C语言中,有几种方法可以执行Shell指令: 1. 使用system()函数 这是最简单的方法,包含在stdlib.h头文件中: #include <stdlib.h>int main() {system("ls -l"); // 执行ls -l命令retu…...
解锁数据库简洁之道:FastAPI与SQLModel实战指南
在构建现代Web应用程序时,与数据库的交互无疑是核心环节。虽然传统的数据库操作方式(如直接编写SQL语句与psycopg2交互)赋予了我们精细的控制权,但在面对日益复杂的业务逻辑和快速迭代的需求时,这种方式的开发效率和可…...
【大模型RAG】Docker 一键部署 Milvus 完整攻略
本文概要 Milvus 2.5 Stand-alone 版可通过 Docker 在几分钟内完成安装;只需暴露 19530(gRPC)与 9091(HTTP/WebUI)两个端口,即可让本地电脑通过 PyMilvus 或浏览器访问远程 Linux 服务器上的 Milvus。下面…...
《通信之道——从微积分到 5G》读书总结
第1章 绪 论 1.1 这是一本什么样的书 通信技术,说到底就是数学。 那些最基础、最本质的部分。 1.2 什么是通信 通信 发送方 接收方 承载信息的信号 解调出其中承载的信息 信息在发送方那里被加工成信号(调制) 把信息从信号中抽取出来&am…...
python如何将word的doc另存为docx
将 DOCX 文件另存为 DOCX 格式(Python 实现) 在 Python 中,你可以使用 python-docx 库来操作 Word 文档。不过需要注意的是,.doc 是旧的 Word 格式,而 .docx 是新的基于 XML 的格式。python-docx 只能处理 .docx 格式…...
SiFli 52把Imagie图片,Font字体资源放在指定位置,编译成指定img.bin和font.bin的问题
分区配置 (ptab.json) img 属性介绍: img 属性指定分区存放的 image 名称,指定的 image 名称必须是当前工程生成的 binary 。 如果 binary 有多个文件,则以 proj_name:binary_name 格式指定文件名, proj_name 为工程 名&…...
