spring boot security 自定义AuthenticationProvider
spring boot security 自定义AuthenticationProvider
基于 spring boot 3.x
场景实现 手机验证码登陆
实现
CaptureCodeAuthenticationFilter
public class CaptureCodeAuthenticationFilter extends AbstractAuthenticationProcessingFilter {private static final String DEFAULT_LOGIN_URL = "/capture/login";private static final String DEFAULT_PHONE_NAME = "phone";private static final String DEFAULT_CODE_NAME = "code";private String codeParamName = DEFAULT_CODE_NAME;private String phoneParamName = DEFAULT_PHONE_NAME;public CaptureCodeAuthenticationFilter(AuthenticationManager authenticationManager) {super(DEFAULT_LOGIN_URL, authenticationManager);}public CaptureCodeAuthenticationFilter(String defaultFilterProcessesUrl, AuthenticationManager authenticationManager) {super(defaultFilterProcessesUrl, authenticationManager);}@Overridepublic Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {if (!request.getMethod().equals("POST")) {throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());}String phone = obtainPhone(request);phone = (phone != null) ? phone.trim() : "";String code = obtainCaptureCode(request);code = (code != null) ? code : "";CaptureCodeAuthenticationToken token = new CaptureCodeAuthenticationToken(phone, code);return this.getAuthenticationManager().authenticate(token);}protected String obtainCaptureCode(HttpServletRequest request) {return request.getParameter(this.codeParamName);}protected String obtainPhone(HttpServletRequest request) {return request.getParameter(this.phoneParamName);}
}
CaptureCodeAuthenticationToken
public class CaptureCodeAuthenticationToken extends UsernamePasswordAuthenticationToken {public CaptureCodeAuthenticationToken(Object principal, Object credentials) {super(principal, credentials);}
}
CaptureCodeAuthenticationProvider
public class CaptureCodeAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {@Overridepublic boolean supports(Class<?> authentication) {return (CaptureCodeAuthenticationToken.class.isAssignableFrom(authentication));}@Overrideprotected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {if (authentication.getPrincipal()==null){throw new BadCredentialsException("Bad credentials "+ authentication.getPrincipal().toString());}if (authentication.getCredentials()==null){throw new BadCredentialsException("Bad credentials "+ authentication.getPrincipal().toString());}}@Overrideprotected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {CaptureCodeAuthenticationToken token = (CaptureCodeAuthenticationToken) authentication;if (!token.getPrincipal().equals("tom")){throw new UsernameNotFoundException("username not fund!");}UserDetails user = User.withUsername("tom").password("tom").build();return user;}}
配置 DefaultSecurityConfig
@Configuration
@EnableWebSecurity
public class DefaultSecurityConfig {@Autowiredprivate ObjectMapper objectMapper;@Beanpublic SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {http.authorizeHttpRequests(request -> request.anyRequest().authenticated());http.formLogin(Customizer.withDefaults());http.csrf(AbstractHttpConfigurer::disable);http.addFilterBefore(captureCodeAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);return http.build();}public CaptureCodeAuthenticationFilter captureCodeAuthenticationFilter() {ProviderManager providerManager = new ProviderManager(new CaptureCodeAuthenticationProvider());CaptureCodeAuthenticationFilter filter =new CaptureCodeAuthenticationFilter(providerManager);filter.setAuthenticationSuccessHandler((request, response, authentication) -> {response.setStatus(HttpServletResponse.SC_OK);response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);response.getWriter().write(objectMapper.writeValueAsString(Result.ok("认证成功")));response.getWriter().flush();});filter.setAuthenticationFailureHandler((request, response, exception) -> {response.setStatus(HttpServletResponse.SC_OK);response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);response.getWriter().write(objectMapper.writeValueAsString(Result.ok("认证失败")));response.getWriter().flush();});return filter;}@Beanpublic UserDetailsService users(PasswordEncoder passwordEncoder) {UserDetails user = User.withUsername("admin").password(passwordEncoder.encode("admin")).build();return new InMemoryUserDetailsManager(user);}@Beanpublic PasswordEncoder encoder() {return new BCryptPasswordEncoder();}}相关文章:
spring boot security 自定义AuthenticationProvider
spring boot security 自定义AuthenticationProvider 基于 spring boot 3.x 场景实现 手机验证码登陆 实现 CaptureCodeAuthenticationFilter public class CaptureCodeAuthenticationFilter extends AbstractAuthenticationProcessingFilter {private static final Strin…...
某电力设计公司绩效考核优化项目成功案例纪实
——引入角色定位考核法,建立多维度评价体系,支持业务转型后的客观评价 【客户行业】电力行业 【问题类型】绩效考核 【客户背景及现状分析】 某电力设计公司成立于2000年左右,是一家从事输变电工程勘察、设计、咨询的专业公司,…...
力扣371周赛
力扣第371场周赛 找出强数对的最大异或值 I 枚举 class Solution { public:int maximumStrongPairXor(vector<int>& a) {int n a.size() , res 0;for(int i 0 ; i < n ; i ){for(int j 0 ; j < n ; j ){if(abs(a[i]-a[j])<min(a[i],a[j])){int c (a…...
Python之字符串、正则表达式练习
目录 1、输出随机字符串2、货币的转换(字符串 crr107)3、凯撒加密(book 实验 19)4、字符替换5、检测字母或数字6、纠正字母7、输出英文中所有长度为3个字母的单词 1、输出随机字符串 编写程序,输出由英文字母大小写或…...
Transmit :macOS 好用的 Ftp/SFtp 工具
Transmit 是一种功能强大的 FTP/SFTP/WebDAV 客户端软件,是一个 Mac OS X 平台上设计的文件传输软件。它由 Panic(一家以软件工具为主的公司)开发和维护,是一款非常受欢迎且易于使用的软件,而且被广泛认为是 Mac OS X …...
【Github】git clone命令下载文件中途停止
方法一: 使用git clone命令下载github上的源代码时,有时文件下载到一定百分比时就停止不动, 这是因为我们所下载的文件很大,超过了git预先分配的Postbuffer容量,所以一直卡在那里。可以使用以下命令查看当前Postbuffe…...
Clickhouse学习笔记(10)—— 查询优化
单表查询 Prewhere 替代 where prewhere与where相比,在过滤数据的时候会首先读取指定的列数据,来判断数据过滤,等待数据过滤之后再读取 select 声明的列字段来补全其余属性 简单来说就是先过滤再查询,而where过滤是先查询出对应…...
[量化投资-学习笔记012]Python+TDengine从零开始搭建量化分析平台-策略回测
上一章节《MACD金死叉策略回测》中,对平安银行这只股票,按照金死叉策略进行了回测。 但通常我们的股票池中有许多股票,每完成一个交易策略都需要对整个股票池进行回测。 下面使用简单的轮询,对整个股票池进行回测。 # 计算单只…...
MySQL 查看 event 执行记录
文章目录 1. 查看 EVENT 执行记录2. 示例3. 结论 MySQL 是一款流行的关系型数据库管理系统,它提供了许多功能来帮助用户管理和操作数据库。其中之一就是 EVENT事件,它允许用户在特定的时间间隔内自动执行指定的操作,类似于计划任务。 在使用 …...
开发知识点-Vue-Electron
Electron ElectronVue打包.exe桌面程序 ElectronVue打包.exe桌面程序 为了不报错 卸载以前的脚手架 npm uninstall -g vue-cli安装最新版脚手架 cnpm install -g vue/cli创建一个 vue 随便起个名 vue create electron-vue-example (随便起个名字electron-vue-example)进入 创建…...
【线性代数】反求矩阵A
...
MyBatis 中的 foreach 的用法
本文将介绍 MyBatis 中的 <foreach> 标签的灵活应用,并结合财经领域的数据处理场景,阐述其在财经系统开发中的重要性和应用价值。 MyBatis中的<foreach>标签简介 MyBatis 是一个优秀的持久层框架,它简化了数据库操作的流程&…...
交叉编译 mysql-connector-c
下载 mysql-connector-c $ wget https://downloads.mysql.com/archives/get/p/19/file/mysql-connector-c-6.1.5-src.tar.gz 注意:mysql-connector 的页面有很多版本,在测试过程中发现很多默认编译有问题,其中上面的 6.1.5 的版本呢是经过测…...
企业如何选择正确的存储服务器租用?
数据时代的发展,让越来越多的企业选择使用存储服务器来存储数据,今天小编就带大家了解一下企业应该怎么正确的选择存储服务器吧,要关注哪些方面的问题呢? 第一点肯定是看自己的需求,不论是选择什么服务器最重要的一点就…...
45.跳跃游戏II
45.跳跃游戏II 题目描述:给定一个长度为 n 的 0 索引整数数组 nums。初始位置为 nums[0]。 每个元素 nums[i] 表示从索引 i 向前跳转的最大长度。换句话说,如果你在 nums[i] 处,你可以跳转到任意 nums[i j] 处: 0 < j < nums[i]i …...
css style、css color 转 UIColor
你能看过来,就说明这个问题很好玩!IT开发是一个兴趣,更是一个挑战!兴趣使你工作有热情。挑战使让你工作充满刺激拉满的状态!我们日复一日年复一年的去撸代码,那些普普通通的功能代码,已经厌倦了…...
C++(20):typename声明类的子类型的简化
C++:typename声明类的子类型_风静如云的博客-CSDN博客 介绍了某些时候需要使用typename来告诉编译器,这是一个类的类型。 C++20简化了对typename的需求,对于明显是类型的地方,可以不再使用typename进行说明: #include <iostream> #include <string>using na…...
一个java文件的JVM之旅
准备 我是小C同学编写得一个java文件,如何实现我的功能呢?需要去JVM(Java Virtual Machine)这个地方旅行。 变身 我高高兴兴的来到JVM,想要开始JVM之旅,它确说:“现在的我还不能进去,需要做一次转换&#x…...
C# wpf 实现任意控件(包括窗口)更多拖动功能
系列文章目录 第一章 Grid内控件拖动 第二章 Canvas内控件拖动 第三章 任意控件拖动 第四章 窗口拖动 第五章 附加属性实现任意拖动 第六章 拓展更多拖动功能(本章) 文章目录 系列文章目录前言一、添加的功能1、任意控件MoveTo2、任意控件DragMove3、边…...
一种ADC采样算法,中位值平均滤波+递推平均滤波
前言 在实际AD采集场景中,会出现周期性变化和偶然脉冲波动干扰对AD采集的影响 这里使用中位值平均滤波递推平均滤波的结合 参考前人写好的代码框架,也参考博主GuYH_下面这篇博客,在此基础上稍作修改,写出这篇博客,能…...
显示器“刷新率”的实战选择指南
1. 刷新率的基础认知:从翻书动画到电竞屏 第一次接触"刷新率"这个概念时,我正对着两台显示器纠结不已。左边是标注着60Hz的普通办公屏,右边是144Hz的电竞显示器,价格相差三倍。销售员反复强调"高刷屏更流畅"&…...
Windows冷注入实战:如何绕过内存检测并加密混淆DLL?【附完整代码】
1. 冷注入技术基础与内存检测原理 冷注入(Cold Injection)是Windows平台下一种特殊的DLL注入技术,与热注入不同,它不需要目标进程处于运行状态。这种技术最早被用于软件插件开发,后来在安全领域有了更广泛的应用。我刚…...
终极指南:如何用Sunshine搭建免费游戏串流服务器,让任何设备畅玩PC大作
终极指南:如何用Sunshine搭建免费游戏串流服务器,让任何设备畅玩PC大作 【免费下载链接】Sunshine Self-hosted game stream host for Moonlight. 项目地址: https://gitcode.com/GitHub_Trending/su/Sunshine 你是否曾梦想过在任何设备上都能玩到…...
用TensorFlow和BERT实战:从海量安全报告中自动提取攻击技战术(TTPs)
基于TensorFlow与BERT的自动化TTPs提取系统实战指南 当安全团队每天需要处理数百份威胁报告时,人工提取攻击技战术(TTPs)的效率瓶颈就会暴露无遗。本文将展示如何构建一个能自动解析安全报告、识别关键攻击模式的智能系统,这套方案…...
FPGA实战:从真值表到硬件实现的译码器与优先编码器
1. 数字电路设计的核心基石:真值表与布尔代数 第一次接触FPGA开发时,我被Verilog代码和硬件描述弄得晕头转向,直到导师指着实验板上的LED灯说:"所有复杂的数字电路,本质上都是开关的组合"。这句话让我突然明…...
5步终极方案:用MediaCreationTool.bat轻松绕过Windows 11硬件限制
5步终极方案:用MediaCreationTool.bat轻松绕过Windows 11硬件限制 【免费下载链接】MediaCreationTool.bat Universal MCT wrapper script for all Windows 10/11 versions from 1507 to 21H2! 项目地址: https://gitcode.com/gh_mirrors/me/MediaCreationTool.ba…...
梦幻动漫魔法工坊应用案例:为游戏角色设计动漫立绘
梦幻动漫魔法工坊应用案例:为游戏角色设计动漫立绘 1. 游戏角色设计的新选择 在游戏开发领域,角色立绘设计一直是既关键又耗时的环节。传统方式需要雇佣专业画师,从草图到上色往往需要数天时间,成本高昂且迭代困难。现在&#x…...
Phi-4-mini-reasoning实战教程:用HuggingFace TGI替代Gradio部署
Phi-4-mini-reasoning实战教程:用HuggingFace TGI替代Gradio部署 1. 项目介绍 Phi-4-mini-reasoning是微软推出的3.8B参数轻量级开源模型,专为数学推理、逻辑推导和多步解题等强逻辑任务设计。这个模型主打"小参数、强推理、长上下文、低延迟&quo…...
Vue + G 实战:打造高校学生打卡数据可视化大屏米
1、普通的insert into 如果(主键/唯一建)存在,则会报错 新需求:就算冲突也不报错,用其他处理逻辑 回到顶部 2、基本语法(INSERT INTO ... ON CONFLICT (...) DO (UPDATE SET ...)/(NOTHING)) 语…...
自题库-智能题库管理系统V1.0
自题库V1.0,是我个人根据自己的需求编程的一个款软件。原本打算是用别人既有的软件,总觉得存在问题不符合自己的需求,主要情况如下:1、做题界面不友善;2、不能自己新建题库;3、题目不能加载图片,…...
