Spring Security基于json登录实现过程详解

作者:柒丶月 时间:2023-12-07 07:15:18 

主要是重写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类

Spring Security基于json登录实现过程详解

进入后再按Ctrl+F12可以查看该类的所有方法

Spring Security基于json登录实现过程详解

进入方法

Spring Security基于json登录实现过程详解

我们只需要在request.getParameter()那里重写一下不就可以实现json登陆

重写attemptAuthentication(HttpServletRequestrequest,HttpServletResponseresponse)方法

只需要复制父类的方法,多加一个判断json的方法。就能同时支持key-value形式可json形式的参数了

Spring Security基于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配置类

Spring Security基于json登录实现过程详解

注:自定义的过滤类和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";
 }
}

Spring Security基于json登录实现过程详解

Spring Security基于json登录实现过程详解

Spring Security基于json登录实现过程详解

来源:https://www.cnblogs.com/qiuwenli/p/13447061.html

标签:Spring,Security,json,登录
0
投稿

猜你喜欢

  • Java基本数据类型族谱与易错点梳理解析

    2021-08-18 10:20:27
  • Android 扫描附近的蓝牙设备并连接蓝牙音响的示例

    2022-05-14 05:36:48
  • C# 获取本机IP地址(IPv4和IPv6)

    2023-09-04 08:48:36
  • Java NIO Buffer实现原理详解

    2023-12-10 22:37:37
  • C#归并排序的实现方法(递归,非递归,自然归并)

    2023-10-03 01:00:36
  • SpringBoot全局异常处理与定制404页面的方法

    2021-08-07 18:54:56
  • java实现Xml与json之间的相互转换操作示例

    2023-06-21 13:30:39
  • Java进程cpu占用过高问题解决

    2021-08-09 00:16:59
  • C#中AS和IS关键字的用法

    2021-06-12 09:44:53
  • 解析C#中的装箱与拆箱的详解

    2023-03-15 23:52:36
  • Android seekbar(自定义)控制音量同步更新

    2022-09-05 08:46:47
  • Spring自动装配之方法、构造器位置的自动注入操作

    2021-11-30 23:28:40
  • Android自定义View实现垂直时间轴布局

    2022-12-12 15:21:16
  • SpringMVC入门实例

    2023-02-04 12:43:48
  • C# 参数按照ASCII码从小到大排序(字典序)

    2023-03-14 22:14:11
  • 基于Kubernetes实现前后端应用的金丝雀发布(两种方案)

    2023-01-07 02:32:27
  • Android仿优酷圆形菜单学习笔记分享

    2023-07-31 06:02:31
  • Spring boot工具类静态属性注入及多环境配置详解

    2022-10-02 16:12:37
  • 浅谈Java与C#的一些细微差别

    2022-09-25 06:33:36
  • android 照相功能的简单实例

    2023-08-08 01:11:55
  • asp之家 软件编程 m.aspxhome.com