springsecurity如何使用application/json接收数据
发表于:2025-02-21 作者:千家信息网编辑
千家信息网最后更新 2025年02月21日,这篇文章将为大家详细讲解有关springsecurity如何使用application/json接收数据,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。spring
千家信息网最后更新 2025年02月21日springsecurity如何使用application/json接收数据
这篇文章将为大家详细讲解有关springsecurity如何使用application/json接收数据,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
spring security 使用 application/json 接收数据
不了解 security 的请看 security 的简单使用
https://blog.51cto.com/5013162/2404946
在使用 spring security 登录用户的时候 发现使用 application/josn 后台不能获取到数据
看 UsernamePasswordAuthenticationFilter 源码发现
//获取密码 protected String obtainPassword(HttpServletRequest request) { return request.getParameter(passwordParameter); } //获取用户名 protected String obtainUsername(HttpServletRequest request) { return request.getParameter(usernameParameter); }
是直接从request 获取的 不是从 requestBody 中获取的
那我们就只需要重写这两个方法从 requestBody 中获取参数
重写 UsernamePasswordAuthenticationFilter 类
public class UserAuthenticationFilter extends UsernamePasswordAuthenticationFilter { private ThreadLocal
登录成功处理类
LoginSuccessHandler.class
@Componentpublic class LoginSuccessHandler implements AuthenticationSuccessHandler { @Override public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException { httpServletResponse.setContentType("application/json;charset=UTF-8"); httpServletResponse.getWriter().write(authentication.getName()); }}
用户校验处理类
@Componentpublic class UserDetailServiceImpl implements UserDetailsService { /** * 用户校验 * @param s * @return * @throws UsernameNotFoundException */ @Override public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException { Collectioncollection = new ArrayList<>();//权限集合 String password = new BCryptPasswordEncoder().encode("123456"); User user = new User(s,password,collection); return user; }}
改造完成 支持 post application/json 同时也支持 post form-data/x-www-form-urlencoded
都可以获取到传入的参数
关于"springsecurity如何使用application/json接收数据"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
登录
用户
数据
参数
篇文章
保护
支持
时候
更多
页面
处理
不错
实用
成功
两个
内容
同时
后台
地址
密码
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
sql数据库表的内容怎么删
最难的软件开发
网络安全辩论赛主持稿
软件开发人员在企业中的地位
网络技术与应用作业3答案
软件开发哪家做的好
淮安服务器精密空调
把txt文档导入到数据库
税务数据网络安全问题
关于什么是数据库
崇明区工商软件开发咨询热线
服务器硬盘通用吗
软件开发三种测试方法和原理
网络技术及教育应用书籍
流媒体服务器主要参数
北京乐游畅想软件开发公司
嵌入式软件开发方法学
发展信息网络技术
建立检查性约束数据库
sap 数据库下载
网络技术三级考什么
青海省市场监督管理局网络安全
互联网科技创新集团有限公司
我的世界服务器金币指令大全
固态硬盘怎么查看写入数据库
两个数据库数据相加
从事网络技术工作怎么样
群晖文件服务器
乾安软件开发商
网络安全制度规定