SpringSecurity中的表單認(rèn)證詳細(xì)解析
一、默認(rèn)表單認(rèn)證:
首先,新建一個 configuration 包用于存放通用配置;然后新建一個 WebSecurityConfig 類,使其繼承 WebSecurityConfigurerAdapter ,如下所示:
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
}在給WebSecurityConfig 類上加上 @EnableWebSecurity 注解后,便會自動被 Spring 發(fā)現(xiàn)并注冊(點(diǎn)擊@EnableWebSecurity 注解可以看到 @Configuration 注解已經(jīng)存在,所以此處不需要額外添加)。
我們接著查看WebSecurityConfigurerAdapter 類對 configure(HttpSecurity http)方法的定義。如下所示:
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();
}可以看到WebSecurityConfigurerAdapter 已經(jīng)默認(rèn)聲明了一些安全特性:
a、驗(yàn)證所有用戶請求。
b、允許用戶使用表單登錄進(jìn)行身份驗(yàn)證(Spring Security 提供了一個簡單的表單登錄頁面)。
c、允許用戶使用 HTTP 基本認(rèn)證。
現(xiàn)在重啟服務(wù),應(yīng)用新的安全配置??梢灶A(yù)見,在下次訪問 localhost:8080 時,系統(tǒng)會要求我們進(jìn)行表單認(rèn)證。如下圖所示:

在上圖中我們可以發(fā)現(xiàn),我們訪問的地址自動跳轉(zhuǎn)到 localhost:8080/login ,這正是 Spring Security 的默認(rèn)登錄頁,只需要輸入正確的用戶名和密碼便可跳轉(zhuǎn)到回原來的訪問地址。
二、自定義表單認(rèn)證
1、初步配置自定義表單登錄頁
雖然自動生成的表單登錄頁可以方便、快速地啟動,但是大多數(shù)應(yīng)用程序更希望提供自己的表單登錄頁,此時就需要覆蓋 configure() 方法,如下所示:
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
protected void configure(HttpSecurity http) throws Exception{
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin().
loginPage("/myLogin.html")
// 使登錄頁不設(shè)限訪問
.permitAll()
.and().
csrf().disable();
}
}2、認(rèn)識 HttpSecurity
HttpSecurity 實(shí)際上對應(yīng)了 Spring Security 命名空間配置方式中 xml 文件內(nèi)的標(biāo)簽。允許我們?yōu)樘囟ǖ?http 請求配置安全策略。
在 xml 文件中,聲明大量配置早已司空見慣;但在 Java 配置中,按照傳統(tǒng)的方式,我們需要這樣來調(diào)用,如下所示:
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
protected void configure(HttpSecurity http) throws Exception{
ExpressionUrlAuthorizationConfigurer.ExpressionInterceptUrlRegistry
urlRegistry=http.authorizeRequests();
ExpressionUrlAuthorizationConfigurer.AuthorizedUrl authorizedUrl =
(ExpressionUrlAuthorizationConfigurer.AuthorizedUrl)urlRegistry
.anyRequest();
authorizedUrl.authenticated();
// more
FormLoginConfigurer<HttpSecurity> formLoginConfigurer =
http.formLogin();
formLoginConfigurer.loginPage("/myLogin.html");
formLoginConfigurer.permitAll();
// more
}
}可以想象出這是多么煩瑣且令人痛苦的一件事。HttpSecurity 首先被設(shè)計為鏈?zhǔn)秸{(diào)用,在執(zhí)行每個方法后,都會返回一個預(yù)期的上下文,便于連續(xù)調(diào)用。我們不需要關(guān)心每個方法究竟返回了什么、如何進(jìn)行下一個配置等細(xì)節(jié)。
HttpSecurity 提供了很多配置相關(guān)的方法,分別對應(yīng)命名空間配置中的子標(biāo)簽 <http>。例如,authorizeRequests()、formLogin()、httpBasic() 和csrf() 分別對應(yīng) <intercept-url>、<form-login>、<http-basic> 和 <csrf> 標(biāo)簽。調(diào)用這些方法之后,除非使用 and() 方法結(jié)束當(dāng)前標(biāo)簽,上下文才會回到 HttpSecurity ,否則鏈?zhǔn)秸{(diào)用的上下文將自動進(jìn)入對應(yīng)的標(biāo)簽域。
authorizeRequests() 方法實(shí)際上返回了一個 URL 攔截注冊器,我們可以調(diào)用它提供的 anyRequest()、antMatchers() 和regexMatchers() 等方法來匹配系統(tǒng)的 URL ,并為其指定安全策略。
formLogin() 和httpBasic() 方法都聲明了需要 Spring Security 提供的表單認(rèn)證方式,分別返回對應(yīng)的配置器。其中,formLogin.loginPage("/myLogin.html") 指定自定義的登錄頁為/myLogin.html ,同時,Spring Security 會用 /myLogin.html 注冊一個 POST 路由,用于接收登錄請求。
csrf() 方法是 Spring Security 提供的跨站請求偽造防護(hù)功能,當(dāng)我們繼承WebSecurityConfigurerAdapter 會默認(rèn)開啟 csrf() 方法,關(guān)于 csrf() 方法的更多內(nèi)容會在后面的章節(jié)專門探討,以使測試進(jìn)程更加順利。
重新啟動服務(wù)后在此訪問 localhost:8080 ,頁面會自動跳轉(zhuǎn)到 localhost:8080/myLogin.html。由于 /myLogin.html 無法定位到頁面資源,所以會顯示一個 404 頁面,如下所示:

3、編寫表單登錄頁
表單登錄頁myLogin.html的代碼如下所示:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登錄</title> </head> <body> <div class = "login" style="width:300px;height:300px"> <h2>Acced Form</h2> <div class ="login-top"></div> <h1>LOGIN FORM</h1> <form action="myLogin.html" method="post"> <input type="text" name="username" placeholder="username"/> <input type="password" name="password" placeholder="password"/> <div class="forgot" style="margin-top:20px;"> <a href="#">forgot Password</a> <input type="submit" value="login"> </div> </form> <div class="login-bottom"> <h3>New User <a href ="">Register</a> </h3> </div> </div> </body> </html>
在表單登錄頁中,僅有一個表單,用戶名和密碼分別為 username 和 password,并以 POST 的方式提交到 /myLogin.html。
我們將該文件放置在 resources/static/ 下。重啟服務(wù),再次訪問 localhost:8080,即可看到自定義的表單登錄頁,如下所示:

輸入正確的用戶名和密碼后,單擊 login 按鈕,即可成功跳轉(zhuǎn)。
4、其他表單配置項(xiàng)
在自定義表單登錄頁之后,處理登錄請求的 URL 也會相應(yīng)改變,如何自定義 URL 呢?很簡單, Spring Security 在表單定制里提供了相應(yīng)的支持,代碼如下所示:
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
protected void configure(HttpSecurity http) throws Exception{
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/myLogin.html")
.loginProcessingUrl("/login")
.permitAll()
.and()
.csrf().disable();
}
}此時,有些讀者可能會有疑問,因?yàn)榘凑諔T例,在發(fā)送登錄請求并認(rèn)證成功之后,頁面會跳轉(zhuǎn)回原訪問頁。在某些系統(tǒng)中的確是跳轉(zhuǎn)回原訪問頁的,但在部分前后端完全分離、僅靠 json完成所有交互的系統(tǒng)中,一般會在登錄時返回一段 json數(shù)據(jù),告知前端成功登錄成功與否,由前端決定如何處理后續(xù)邏輯,而非由服務(wù)器主動執(zhí)行頁面跳轉(zhuǎn)。這在 Spring Security 中同樣可以實(shí)現(xiàn):
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
protected void configure(HttpSecurity http) throws Exception{
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/myLogin.html")
.loginProcessingUrl("/login")
.successHandler(new AuthenticationSuccessHandler() {
@Override
public void onAuthenticationSuccess(HttpServletRequest arg0,
HttpServletResponse arg1, Authentication arg2)
throws IOException, ServletException {
arg1.setContentType("application/json;charset=UTF-8");
PrintWriter out = arg1.getWriter();
out.write("{\"error_code\":\"0\",\"message\":\"歡迎登錄系統(tǒng)\"}");
}
})
.failureHandler(new AuthenticationFailureHandler() {
@Override
public void onAuthenticationFailure(HttpServletRequest arg0,
HttpServletResponse arg1, AuthenticationException arg2)
throws IOException, ServletException {
arg1.setContentType("application/json;charset=UTF-8");
arg1.setStatus(401);
PrintWriter out = arg1.getWriter();
// 輸出失敗的原因
out.write("{\"error_code\":\"401\",\"name\":\""+arg2.getCause()+"\","
+ "\"message\":\""+arg2.getMessage()+"\"}}");
}
})
.and()
.csrf().disable();
}
}表單登錄配置模塊提供了 successHandler() 和 failureHandler() 兩個方法,分別處理登錄成功和登錄失敗的邏輯。
其中,successHandler() 方法帶有一個 Authentication 參數(shù),攜帶當(dāng)前登錄用戶名及其角色等信息;而 failureHandler() 方法攜帶一個 AuthenticationException 異常參數(shù)。
具體處理方式需按照系統(tǒng)的情況自定義。
在形式上,我們確實(shí)使用了 Spring Security 的表單認(rèn)證功能,并且自定義了表單登錄頁。但實(shí)際上,這還遠(yuǎn)遠(yuǎn)不夠。
例如,在實(shí)際系統(tǒng)中,我們正常登錄時使用的用戶名和密碼都來自數(shù)據(jù)庫,這里卻都寫在配置上。
更進(jìn)一步,我們可以對每個登錄用戶都設(shè)定詳細(xì)的權(quán)限,而并非一個通用角色。這些內(nèi)容將在后面章節(jié)講解。
到此這篇關(guān)于SpringSecurity中的表單認(rèn)證詳細(xì)解析的文章就介紹到這了,更多相關(guān)SpringSecurity表單認(rèn)證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Swing SpringLayout彈性布局的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java Swing SpringLayout彈性布局的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Java動態(tài)數(shù)組ArrayList實(shí)現(xiàn)動態(tài)原理
ArrayList是一種動態(tài)數(shù)組,它可以在運(yùn)行時自動調(diào)整大小以適應(yīng)元素的添加和刪除,在Java中,你可以使用ArrayList類來實(shí)現(xiàn)動態(tài)數(shù)組,本文將給大家介紹一下ArrayList動態(tài)數(shù)組,是怎么實(shí)現(xiàn)動態(tài)的2023-08-08
String?concat(String?str)使用小結(jié)
這篇文章主要介紹了String?concat(String?str)使用小結(jié),在了解concat()之前,首先需要明確的是String的兩點(diǎn)特殊性,一是長度不可變二是值不可變,本文給大家詳細(xì)講解,需要的朋友可以參考下2022-11-11
淺析Spring容器原始Bean是如何創(chuàng)建的
這篇文章主要是想和小伙伴們一起聊聊?Spring?容器創(chuàng)建?Bean?最最核心的?createBeanInstance?方法,文中的示例代碼講解詳細(xì),需要的可以參考一下2023-08-08
Java中Stringbuild,Date和Calendar類的用法詳解
這篇文章主要為大家詳細(xì)介紹了Java中Stringbuild、Date和Calendar類的用法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-04-04
spring boot 實(shí)現(xiàn)阿里云視頻點(diǎn)播功能(刪除視頻)
這篇文章主要介紹了spring boot 實(shí)現(xiàn)阿里云視頻點(diǎn)播(刪除視頻功能),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
Nacos通過RefreshScope實(shí)現(xiàn)配置自動更新的方式分享
這篇文章主要給大家介紹了Nacos如何通過RefreshScope實(shí)現(xiàn)配置自動更新,文中給了兩種實(shí)現(xiàn)方式供大家參考,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-09-09

