欢迎来到代码驿站!

当前位置:首页 >

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

时间:2020-08-17 10:00:09|栏目:|点击:

主要是重写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";
  }
}

上一篇:python使用建议与技巧分享(一)

栏    目:

下一篇:R语言ggplot2边框背景去除的实现

本文标题:Spring Security基于json登录实现过程详解

本文地址:http://www.codeinn.net/misctech/3637.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有