SpringSecurity 自定义表单登录

SpringSecurity 自定义表单登录

本篇主要讲解 在SpringSecurity中 如何 自定义表单登录 , SpringSecurity默认提供了一个表单登录,但是实际项目里肯定无法使用的,本篇就主要讲解如何自定义表单登录

1.创建SpringSecurity项目

1.1 使用IDEA

先通过IDEA 创建一个SpringBoot项目 并且依赖SpringSecurity,Web依赖

SpringSecurity 自定义表单登录

此时pom.xml会自动添加

<dependency>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-security</artifactid>
</dependency>

2.扩展 WebSecurityConfigurerAdapter

WebSecurityConfigurerAdapter 是SpringSecurity 提供的用于我们扩展自己的配置

实现WebSecurityConfigurerAdapter经常需要重写的:

1&#x3001;configure(AuthenticationManagerBuilder auth);

2&#x3001;configure(WebSecurity web);

3&#x3001;configure(HttpSecurity http);

2.1 默认 WebSecurityConfigurerAdapter 为我们提供了一些基础配置如下

protected void configure(HttpSecurity http) throws Exception {
    logger.debug("Using default configure(HttpSecurity). If subclassed this will potentially override subclass configure(HttpSecurity).");

    http
        .authorizeRequests()
            .anyRequest().authenticated()
            .and()
        .formLogin().and()
        .httpBasic();
}

2.2 创建自定义的 WebSecurityConfigurer

1.formLogin() 开启表单登录,该方法会应用 FormLoginConfigurer 到HttpSecurity上,后续会被转换为对应的Filter
2.loginPage() 配置自定义的表单页面
3.authorizeRequests().anyRequest().authenticated(); 表示任何请求接口都要认证

@Configuration
@Slf4j
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable()
            .formLogin()
            .loginPage("/mylogin.html")
            .and()
            .authorizeRequests().anyRequest().authenticated();

    }
}

2.3 mylogin.html

<!DOCTYPE html>
<html lang="en">
  <head>
     <meta charset="UTF-8">
     <title>Title</title>
  </head>
  <body>
    <h1>&#x6807;&#x51C6;&#x767B;&#x5F55;&#x9875;&#x9762;</h1>
    <h3>&#x8868;&#x5355;&#x767B;&#x5F55;</h3>

 <form action="/login" method="post">
   <table>
    <tr>
        <td>&#x7528;&#x6237;&#x540D;:</td>
        <td><input type="text" name="username"></td>
    </tr>

    <tr>
        <td>&#x5BC6;&#x7801;:</td>
        <td><input type="password" name="password"></td>
    </tr>

    <tr>
        <td colspan="2">
            <button type="submit">&#x767B;&#x5F55;</button>
        </td>
    </tr>
   </table>
  </form>
</body>
</html>

3.访问自定义登录页面(注意有重定向过多问题)

启动项目 并且直接访问

http://localhost:8080

会发现浏览器报 重定向次数过多,这是什么原因呢?

这是因为 我们上面配置了 loginPage(“/mylogin.html”) ,但是这个路径它没有被允许访问,也就是当重定向到/mylogin.html路径后,还是会因为需要认证 被重定向道 /mylogin.html 导致该错误

SpringSecurity 自定义表单登录

4.允许登录页面路径访问 antMatchers(“/mylogin.html”).permitAll()

只需要在配置的地方 添加 .antMatchers(“/mylogin.html”).permitAll() 允许这个路径

    http.csrf().disable()
            .formLogin()
            .loginPage("/mylogin.html")
            .and()
            .authorizeRequests()
            .antMatchers("/mylogin.html").permitAll()
            .anyRequest().authenticated();

再次访问,我们自定义的表单就显示出来了(忽略样式。。。)

SpringSecurity 自定义表单登录

此时我们输入用户名 user 密码 : 控制台打印

Using generated security password: 6bf253eb-c785-42b6-b147-b0fe2971586e

发现又跳转到 /mylogin.html页面,这是因为 当我们配置了 loginPage(“/mylogin.html”)之后 处理表单登录的过滤器它所拦截的请求就不再是 /login (默认是 /login) ,拦截的登录请求地址变成了 和 loginPage一样的 mylogin.html

此时如果将 action地址改成 /mylogin.html ,那么再登录 就能成功

<form action="/mylogin.html" method="post">
</form>

5.配置自定义登录接口路径 loginProcessingUrl

由于我们上面配置了 loginPage ,则对应登录接口路径也变成了 loginPage所配置的 mylogin.html,但是当我们不想使用这个作为接口路径的时候,可以通过以下配置来修改

通过 loginProcessingUrl 类配置处理登录请求的路径

 http.csrf().disable()
            .formLogin()
            .loginPage("/mylogin.html")
            .loginProcessingUrl("/auth/login")
            .and()
            .authorizeRequests()
            .antMatchers("/mylogin.html").permitAll()
            .anyRequest().authenticated();

记得和action 对应

<form action="/auth/login" method="post">
</form>

至此SpringSecurity自定义登录页面的配置 以及注意事项全部说完

6. 总结

本篇主要讲解 在SpringSecurity中 如何 自定义表单登, 是不是非常简单 ,但是也有一些要注意的点
1.扩展WebSecurityConfigurerAdapter
2.配置loginPage 页面路径
3.允许loginPage 页面路径 访问
4.配置登录请求的路径 loginProcessingUrl

个人博客地址: https://www.askajohnny.com 欢迎访问!
本文由博客一文多发平台 OpenWrite 发布!

Original: https://www.cnblogs.com/askajohnny/p/12229190.html
Author: AskaJohnny
Title: SpringSecurity 自定义表单登录

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/684937/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球