温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring Security基于json登录的方法

发布时间:2020-08-19 11:01:21 来源:亿速云 阅读:306 作者:小新 栏目:开发技术

小编给大家分享一下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类

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登录的方法

上是Spring Security基于json登录的方法的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI