1 导入Spring Security的相关依赖

        <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>

2 Spring Security认证方式之Http基本认证

2.1 实现流程

  1. Http基本认证是最简单的认证方式,不需要任何配置,导入依赖启动应用后就会弹出默认的httpBasic认证框。也相当于在实现了BrowserSecurityConfig类进行了如下配置:

    @Configuration
    public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    //HttpBasic的alert框登录
    http.httpBasic()
    .and()
    .authorizeRequests()//对请求进行授权
    .anyRequest()//任何请求
    .authenticated();
    }
    }

    在Spring Security的默认配置中,使用用户名user,密码为控制台打印的随机码。

  2. 可以在resources配置文件中修改默认的用户名和密码,指定密码后控制台不再生成动态的随机码。

  • properties配置
  • security.user.name=user
    security.user.password=123456
  • yml配置
  • spring:
    security:
    user:
    name: user
    password: 123456

2.2 Http基本认证原理

  • 客户端向服务端请求被保护的资源,服务端向客户端请求用户名和密码,浏览器弹出登录输入框,HttpBasic模式要求传输的用户名密码使用Base64模式进行加密。客户端向服务端发起login的请求。

  • 客户端发送的Http请求中会使用Authorization作为Header,即“Basic + 空格 + Base64密码”。而Base64很容易被逆向解码,所以HttpBstic的认证方式是不安全的。

    服务器接收到请求后,到达BasicAuthenticationFilter过滤器,将提取Header值,并使用用于验证用户身份的相同算法Base64进行解码。解码结果与登录验证的用户名密码匹配,匹配成功则可以继续过滤器后续的访问。

2.3 小结

Http认证虽然配置简单,但是安全性极差,灵活性不高,无法携带cookie,所以一般应用会用安全性和灵活性更强的表单认证方式,可以通过自定义登录和验证逻辑,提高安全性。

3 表单认证

3.1 实现流程

  1. 配置使用自定义的表单登录

    @Configuration
    public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    // 表单登录,而不是httpBasic
    http.formLogin()
    // 自定义登录页面
    .loginPage("login.html")
    // 指定处理登录请求的路径
    .loginProcessingUrl("/login")
    // 不限制登录页面的访问
    .permitAll()
    .and()
    .authorizeRequests()//对请求进行授权
    .anyRequest()//任何请求
    .authenticated()
    .and()
    .csrf()
    .disable();
    }
    }
  1. 自定义认证成功/失败的处理逻辑

    Spring Security中默认的成功处理逻辑定义在AbstractAuthenticationTargetUrlRequestHandler类中,在发送登录请求并认证成功后页面会跳转回到原来的访问页面,页面跳转不适用于前后端分离的系统中,因此可以自定义成功后的处理逻辑,登录后返回JSON数据。

    实现AuthenticationSuccessHandler和 AuthenticationFailureHandle类中的处理方法。

    @Component("customAuthenticationSuccessHandler")
    public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
    httpServletResponse.setContentType("application/json;charset=UTF-8");
    httpServletResponse.getWriter().write(objectMapper.writeValueAsString(authentication));
    }
    }
    @Component("customAuthenticationFailureHandler")
    public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
    log.info("登录失败");
    httpServletResponse.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
    httpServletResponse.setContentType("application/json;charset=UTF-8");
    httpServletResponse.getWriter().write(e.getMessage());
    }
    }

    在WebSecurity配置文件中配置自定义的处理器

@Configuration
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
// 表单登录,而不是httpBasic
http.formLogin()
// 自定义登录页面
.loginPage("login.html")
// 不限制登录页面的访问
.permitAll()
// 自定义认证后的处理器
.successHandler(customAuthenticationSuccessHandler)
.failureHandler(customAuthenticationFailureHandler)
.and()
.authorizeRequests()//对请求进行授权
.anyRequest()//任何请求
.authenticated()
.and()
.csrf()
.disable();

}
}

3.2 关于HttpSecurity

在配置文件中使用HttpSecurity进行HTTP请求安全策略的配置,HttpSecurity被设计为链式调用,执行每个方法会返回一个预期的上下文,用于连续调用,其中:

  • authorizeRequests()相当于XML中的<intercept-url>标签,它返回一个URL拦截注册器,可以调用它提供的anyRequest()、antMatchers()、regexMatchers()等方法来匹配系统的URL,并为它指定安全策略。

  • formLogin()相当于XML中的<form-login>标签,声明了需要Spring Security提供的表单认证方式,返回对应的配置器,loginPage()用于指定自定义的登录页面。Spring Security会为该登录页注册一个POST路由用于接收登录请求。

  • httpBasic()相当于XML中的<http-basic>标签

  • csrf()相当于XML中的<csrf>标签,提供了跨站请求伪造防护功能,继承WebSecurityConfigurerAdapter会默认开启csrf()方法。

使用and()方法可以结束当前标签,上下文会回到HttpSecurity,否则链式调用的上下文会自动进入对应的标签域。

最新文章

  1. JSP前三章测试改错
  2. MVC代码中如何调用api接口
  3. Winform Textbox MultiLine和WordWrap属性的使用
  4. 多年前写的一个ASP.NET网站管理系统,到现在有些公司在用
  5. WP8_访问ListBox中的Item项中的某个元素
  6. android开发 获取父控件的高宽
  7. 【LeetCode】107 - Binary Tree Level Order Traversal II
  8. 二叉树单色路径最长&amp;&amp;穿珠子
  9. fzu Problem 2140 Forever 0.5(推理构造)
  10. 【清澄A1333】【整体二分+二维树状数组】矩阵乘法(梁盾)
  11. javaWeb学习总结(4)- HTML 关于head中的&lt;meta&gt;标签
  12. 1.Servlet介绍 和 HTTP协议简述
  13. Oracle11g静默安装
  14. Vue/Egg大型项目开发(一)搭建项目
  15. Markdown简记
  16. H5 五子棋源码
  17. bootstrap学习一
  18. http协议-https协议-相对协议
  19. 清理 Xcode 10
  20. service citrix xcenserver health check service (xenserver healthcheck) failed to start verfy that you have sufficient privileges to srart system services

热门文章

  1. 帮助你更好的理解Spring循环依赖
  2. wordpress学习笔记
  3. python golang中grpc 使用示例代码详解
  4. 超简单的jq图片上传
  5. 更改docker默认存储路径操作(centos6版本)
  6. python新手70个练手项目
  7. pdfmake.js使用及其源码分析
  8. Java中的锁机制
  9. Spring发布WebService并调用已有的WebService
  10. php提取xml配置参数