SpringSecurity 自定義表單登錄的實現(xiàn)
本篇主要講解 在SpringSecurity中 如何 自定義表單登錄 , SpringSecurity默認提供了一個表單登錄,但是實際項目里肯定無法使用的,本篇就主要講解如何自定義表單登錄
1.創(chuàng)建SpringSecurity項目
1.1 使用IDEA
先通過IDEA 創(chuàng)建一個SpringBoot項目 并且依賴SpringSecurity,Web依賴

此時pom.xml會自動添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2.擴展 WebSecurityConfigurerAdapter
WebSecurityConfigurerAdapter 是SpringSecurity 提供的用于我們擴展自己的配置
實現(xiàn)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 創(chuàng)建自定義的 WebSecurityConfigurer
1.formLogin() 開啟表單登錄,該方法會應用 FormLoginConfigurer 到HttpSecurity上,后續(xù)會被轉換為對應的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
會發(fā)現(xiàn)瀏覽器報 重定向次數(shù)過多,這是什么原因呢?
這是因為 我們上面配置了 loginPage("/mylogin.html") ,但是這個路徑它沒有被允許訪問,也就是當重定向到/mylogin.html路徑后,還是會因為需要認證 被重定向道 /mylogin.html 導致該錯誤

4.允許登錄頁面路徑訪問 antMatchers("/mylogin.html").permitAll()
只需要在配置的地方 添加 .antMatchers("/mylogin.html").permitAll() 允許這個路徑
http.csrf().disable()
.formLogin()
.loginPage("/mylogin.html")
.and()
.authorizeRequests()
.antMatchers("/mylogin.html").permitAll()
.anyRequest().authenticated();
再次訪問,我們自定義的表單就顯示出來了(忽略樣式。。。)

此時我們輸入用戶名 user 密碼 : 控制臺打印
Using generated security password: 6bf253eb-c785-42b6-b147-b0fe2971586e
發(fā)現(xiàn)又跳轉到 /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**
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringBoot+Redis Bitmap實現(xiàn)活躍用戶統(tǒng)計
Redis的Bitmap數(shù)據結構是一種緊湊的位圖,它可以用于實現(xiàn)各種場景,其中統(tǒng)計活躍用戶是一種經典的業(yè)務場景,下面我們就來學習一下SpringBoot如何利用Redis中的Bitmap實現(xiàn)活躍用戶統(tǒng)計吧2023-11-11
JavaSE實戰(zhàn)之酒店訂房系統(tǒng)的實現(xiàn)
這篇文章主要為大家詳細介紹了如何利用JavaSE實現(xiàn)酒店訂房系統(tǒng),文中的示例代碼講解詳細,對我們學習JavaSE開發(fā)有一定的幫助,需要的可以參考一下2022-07-07
SpringBoot啟動security后如何關閉彈出的/login頁面
這篇文章主要介紹了SpringBoot啟動security后如何關閉彈出的login頁面問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

