SpringSecurity 自定義表單登錄

SpringSecurity 自定義表單登錄

本篇主要講解 在SpringSecurity中 如何 自定義表單登錄 , SpringSecurity默認提供了一個表單登錄,但是實際項目裏肯定無法使用的,本篇就主要講解如何自定義表單登錄

 1.創建SpringSecurity項目

  1.1 使用IDEA

  先通過IDEA 創建一個SpringBoot項目 並且依賴SpringSecurity,Web依賴

Xnip20200121_194420.png

  此時pom.xml會自動添加

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

 2.擴展 WebSecurityConfigurerAdapter

 WebSecurityConfigurerAdapter 是SpringSecurity 提供的用於我們擴展自己的配置

 實現WebSecurityConfigurerAdapter經常需要重寫的:

1、configure(AuthenticationManagerBuilder auth);

2、configure(WebSecurity web);

3、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>標準登錄頁面</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.訪問自定義登錄頁面(注意有重定向過多問題)

 啓動項目 並且直接訪問

http://localhost:8080

 會發現瀏覽器報 重定向次數過多,這是什麼原因呢?

 這是因爲 我們上面配置了 loginPage("/mylogin.html") ,但是這個路徑它沒有被允許訪問,也就是當重定向到/mylogin.html路徑後,還是會因爲需要認證 被重定向道 /mylogin.html 導致該錯誤

Xnip20200122_172625.png

 4.允許登錄頁面路徑訪問 antMatchers("/mylogin.html").permitAll()

 只需要在配置的地方 添加 .antMatchers("/mylogin.html").permitAll() 允許這個路徑

    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 發佈!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章