SpringSecurity 自定義表單登錄

SpringSecurity 自定義表單登錄

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


 1.創建SpringSecurity項目


  1.1 使用IDEA

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

SpringSecurity 自定義表單登錄


  此時pom.xml會自動添加

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

 2.擴展 WebSecurityConfigurerAdapter

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

 實現WebSecurityConfigurerAdapter經常需要重寫的:

<code>    1、configure(AuthenticationManagerBuilder auth);/<code>
<code>    2、configure(WebSecurity web);/<code>
<code>    3、configure(HttpSecurity http);

/<code>

  2.1 默認 WebSecurityConfigurerAdapter 為我們提供了一些基礎配置如下


<code>    protected void configure(HttpSecurity http) throws Exception {
logger.debug("Using default configure(HttpSecurity). If subclassed this will potentially override subclass configure(HttpSecurity).");/<code>
<code>        http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin().and()
.httpBasic();
}/<code>

  2.2 創建自定義的 WebSecurityConfigurer

**  1.formLogin() 開啟表單登錄,該方法會應用 FormLoginConfigurer 到HttpSecurity上,後續會被轉換為對應的Filter  2.loginPage() 配置自定義的表單頁面  3.authorizeRequests().anyRequest().authenticated(); 表示任何請求接口都要認證**

<code>    @Configuration
@Slf4j
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {

/<code>
<code>    @Override
protected void configure(HttpSecurity http) throws Exception {/<code>
<code>            http.csrf().disable()
.formLogin()
.loginPage("/mylogin.html")
.and()
.authorizeRequests().anyRequest().authenticated();/<code>
<code>        }
} /<code>

  2.3 mylogin.html


<code>    



<title>Title/<title>


標準登錄頁面


表單登錄

/<code>
<code>     



/<code>

 3.訪問自定義登錄頁面(注意有重定向過多問題)

 啟動項目 並且直接訪問

<code>    http://localhost:8080/<code>

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

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

SpringSecurity 自定義表單登錄


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

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


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

 再次訪問,我們自定義的表單就顯示出來了(忽略樣式。。。)

SpringSecurity 自定義表單登錄

 此時我們輸入用戶名 user 密碼 : 控制檯打印

<code>    Using generated security password: 6bf253eb-c785-42b6-b147-b0fe2971586e/<code>

發現又跳轉到 /mylogin.html頁面,這是因為 當我們配置了 loginPage("/mylogin.html")之後 處理表單登錄的過濾器它所攔截的請求就不再是 /login (默認是 /login) ,攔截的登錄請求地址變成了 和 loginPage一樣的 mylogin.html

 此時如果將 action地址改成 /mylogin.html ,那麼再登錄 就能成功

<code>    /<code>

 5.配置自定義登錄接口路徑 loginProcessingUrl

 由於我們上面配置了 loginPage ,則對應登錄接口路徑也變成了 loginPage所配置的 mylogin.html,但是當我們不想使用這個作為接口路徑的時候,可以通過以下配置來修改

 通過 loginProcessingUrl 類配置處理登錄請求的路徑

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

 記得和action 對應

<code>    /<code>

 至此SpringSecurity自定義登錄頁面的配置 以及注意事項全部說完

6. 總結

**本篇主要講解 在SpringSecurity中 如何 自定義表單登, 是不是非常簡單 ,但是也有一些要注意的點1.擴展WebSecurityConfigurerAdapter2.配置loginPage 頁面路徑3.允許loginPage 頁面路徑 訪問4.配置登錄請求的路徑 loginProcessingUrl**


個人博客網站 https://www.askajohnny.com 歡迎訪問!

本文由博客一文多發平臺 https://openwrite.cn?from=article_bottom 發佈!


分享到:


相關文章: