Spring Security基于json登录实现过程详解
作者:柒丶月 发布时间:2023-12-07 07:15:18
标签:Spring,Security,json,登录
主要是重写attemptAuthentication方法
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
相关配置和代码
application.properties配置密码
spring.security.user.name=admin
spring.security.user.password=123
创建自定义身份过滤类
写json登录之前先看一下源码,了解一下它是如何表单登录的
在idea连按下shift键,搜索UsernamePasswordAuthenticationFilter类
进入后再按Ctrl+F12可以查看该类的所有方法
进入方法
我们只需要在request.getParameter()那里重写一下不就可以实现json登陆
重写attemptAuthentication(HttpServletRequestrequest,HttpServletResponseresponse)方法
只需要复制父类的方法,多加一个判断json的方法。就能同时支持key-value形式可json形式的参数了
public class MyAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
if(!request.getMethod().equals("POST")){
throw new AuthenticationServiceException("Authentication method not supported" + request.getMethod());
}
//说明是以json的形式传递参数
if (request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE)) {
String username = null;
String password = null;
//将传入的json数据转换成map再通过get("key")获得
try {
Map<String,String> map =new ObjectMapper().readValue(request.getInputStream(),
Map.class);
username = map.get("username");
password = map.get("password");
} catch (IOException e) {
e.printStackTrace();
}
if (username == null) {
}
if (password == null) {
}
username = username.trim();
UsernamePasswordAuthenticationToken authRequest =
new UsernamePasswordAuthenticationToken(username, password);
setDetails(request, authRequest);
return this.getAuthenticationManager().authenticate(authRequest);
}
return super.attemptAuthentication(request, response);
}
}
创建SecurityConfig配置类
注:自定义的过滤类和security原来那个表单登陆过滤设置是分开的
体现在filter.setFilterProcessesUrl()和loginProcessingUrl
因此表单登陆和json登陆的,successHandler判断也要分开写,
一会下面有效果图也可以印证这一点
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginProcessingUrl("/doLogin")
.permitAll()
.and()
.csrf().disable();
//将自定义的过滤器加进来,第二参数表示加到usernamePasswordAuthenticationFilter所在的位置
http.addFilterAt(myAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
}
@Bean
MyAuthenticationFilter myAuthenticationFilter() throws Exception{
MyAuthenticationFilter filter = new MyAuthenticationFilter();
filter.setAuthenticationManager(authenticationManagerBean());
return filter;
}
}
创建Controller
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello security";
}
}
来源:https://www.cnblogs.com/qiuwenli/p/13447061.html


猜你喜欢
- 1 在图片上用鼠标进行操作,opencv主要用到setMouseCallback()函数。winname 窗口名称onMouse 鼠标事件的
- 这篇文章主要介绍了Springboot打包部署代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- 最近比较无聊,随便翻着博客,无意中看到了有的人用VBS读文本内容,也就是读几句中文,emmm,挺有趣的,实现也很简单,都不需要安装什么环境,
- 序本文主要研究下迁移到java9的一些注意事项。迁移种类1、代码不模块化,先迁移到jdk9上,好利用jdk9的api2、代码同时也模块化迁移
- 第一次写技术博客,写一下以前写的一个双色球抽奖随机算法。原理如下:1首先初始化一个待抽奖的数组nums,数组的长度k2. 随机一个1-k之间
- 题目要求思路一:枚举 + 二分逐一枚举值域内的所有值,然后二分判断是否合法。Javaclass Solution { &nbs
- 为什么需要在应用程序中增加渠道信息?Android应用的发布需要面对各种各样的市场,我们称之为渠道。有的时候,我们需要知道应用是从哪个渠道下
- 1、Spring Boot跨域配置有两种方法在后端使用Spring Boot。Spring Boot跨域非常简单,只需书写以下代码即可。@C
- 今天下了个新浪微博的API研究研究,目前实现了发布微博功能,包括带图片的微博。为了安全,新浪微博的API中并没有提供用微博帐号密码登录的功能
- springboot整合vue实现上传下载文件,供大家参考,具体内容如下环境springboot 1.5.x完整代码下载:springboo
- 本文实例讲述了Android图片压缩工具类。分享给大家供大家参考,具体如下:这里共享一个图片压缩工具类:package com.sanwei
- 本文实例讲述了Android编程之ListView和EditText发布帖子隐藏软键盘功能。分享给大家供大家参考,具体如下:在Android
- Result可以设定全局结果集,如:<struts> <constant name="struts
- 本文实例讲述了C#实现XSL转换的方法。分享给大家供大家参考,具体如下:xsl 可方便的将一种格式的xml,转换成另一种格式的xml,参考下
- mybatis-spring:@MapperScan注解在demo: springboot+mybatis的示例中,dao层接口使用了注解@
- 1.流布局FlowLayout所有组件像流一样,一个一个排放,排满了一行之后排下一行,默认情况下,每个组件是居中排列的,但是也可以设置。流布
- 一、算法效率算法效率分析分为两种:第一种是时间效率,第二种是空间效率。时间效率被称为时间复杂度,而空间效率被称作空间复杂度。 时间复杂度主要
- 消息发送过程消息的发送可能会经过 * 、序列化、分区器等过程。消息发送的主要涉及两个线程,分别为main线程和sender线程。如图所示,主
- 存储访问框架,简称:SAF, 就是系统文件选择器+文件操作API。先选择文件,在用文件操作API处理文件。系统文件选择器,就和Windows
- 开发程序的时候经常会引用一些第三方的DLL,然后编译生成的exe文件就不能脱离这些DLL独立运行了。但是,很多时候我们本想开发一款只需要一个