SpringSecurity 自定义表单登陆

SpringSecurity 自定义表单登陆

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

 1.建立SpringSecurity项目

  1.1 使用IDEA

  先经过IDEA 建立一个SpringBoot项目 而且依赖SpringSecurity,Web依赖web

Xnip20200121_194420.png

  此时pom.xml会自动添加spring

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
</dependency>

 2.扩展 WebSecurityConfigurerAdapter

 WebSecurityConfigurerAdapter 是SpringSecurity 提供的用于咱们扩展本身的配置segmentfault

 实现WebSecurityConfigurerAdapter常常须要重写的:浏览器

一、configure(AuthenticationManagerBuilder auth);

二、configure(WebSecurity web);

三、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(); 表示任何请求接口都要认证**ide

@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>标准登陆页面</h1>
    <h3>表单登陆</h3>

 <form action="/login" method="post">
   <table>
    <tr>
        <td>用户名:</td>
        <td><input type="text" name="username"/></td>
    </tr>

    <tr>
        <td>密码:</td>
        <td><input type="password" name="password"/></td>
    </tr>

    <tr>
        <td colspan="2">
            <button type="submit">登陆</button>
        </td>
    </tr>
   </table>
  </form>
</body>
</html>

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

 启动项目 而且直接访问spring-boot

http://localhost:8080

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

 这是由于 咱们上面配置了 loginPage("/mylogin.html") ,可是这个路径它没有被容许访问,也就是当重定向到/mylogin.html路径后,仍是会由于须要认证 被重定向道 /mylogin.html 致使该错误网站

Xnip20200122_172625.png

 4.容许登陆页面路径访问 antMatchers("/mylogin.html").permitAll()

 只须要在配置的地方 添加 .antMatchers("/mylogin.html").permitAll() 容许这个路径ui

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

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

Xnip20200122_173428.png

 此时咱们输入用户名 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">

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

 至此SpringSecurity自定义登陆页面的配置 以及注意事项所有说完

6. 总结

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

我的博客网站 https://www.askajohnny.com 欢迎来访问!
本文由博客一文多发平台 OpenWrite 发布!
相关文章
相关标签/搜索