SpringSecurity中的表單認證詳細解析
一、默認表單認證:
首先,新建一個 configuration 包用于存放通用配置;然后新建一個 WebSecurityConfig 類,使其繼承 WebSecurityConfigurerAdapter ,如下所示:
@EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ }
在給WebSecurityConfig 類上加上 @EnableWebSecurity 注解后,便會自動被 Spring 發(fā)現(xià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)默認聲明了一些安全特性:
a、驗證所有用戶請求。
b、允許用戶使用表單登錄進行身份驗證(Spring Security 提供了一個簡單的表單登錄頁面)。
c、允許用戶使用 HTTP 基本認證。
現(xiàn)在重啟服務(wù),應(yīng)用新的安全配置??梢灶A(yù)見,在下次訪問 localhost:8080 時,系統(tǒng)會要求我們進行表單認證。如下圖所示:
在上圖中我們可以發(fā)現(xiàn),我們訪問的地址自動跳轉(zhuǎn)到 localhost:8080/login ,這正是 Spring Security 的默認登錄頁,只需要輸入正確的用戶名和密碼便可跳轉(zhuǎ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、認識 HttpSecurity
HttpSecurity 實際上對應(yīng)了 Spring Security 命名空間配置方式中 xml 文件內(nèi)的標簽。允許我們?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è)計為鏈式調(diào)用,在執(zhí)行每個方法后,都會返回一個預(yù)期的上下文,便于連續(xù)調(diào)用。我們不需要關(guān)心每個方法究竟返回了什么、如何進行下一個配置等細節(jié)。
HttpSecurity 提供了很多配置相關(guān)的方法,分別對應(yīng)命名空間配置中的子標簽 <http>。例如,authorizeRequests()、formLogin()、httpBasic() 和csrf() 分別對應(yīng) <intercept-url>、<form-login>、<http-basic> 和 <csrf> 標簽。調(diào)用這些方法之后,除非使用 and() 方法結(jié)束當(dāng)前標簽,上下文才會回到 HttpSecurity ,否則鏈式調(diào)用的上下文將自動進入對應(yīng)的標簽域。
authorizeRequests() 方法實際上返回了一個 URL 攔截注冊器,我們可以調(diào)用它提供的 anyRequest()、antMatchers() 和regexMatchers() 等方法來匹配系統(tǒng)的 URL ,并為其指定安全策略。
formLogin() 和httpBasic() 方法都聲明了需要 Spring Security 提供的表單認證方式,分別返回對應(yīng)的配置器。其中,formLogin.loginPage("/myLogin.html") 指定自定義的登錄頁為/myLogin.html ,同時,Spring Security 會用 /myLogin.html 注冊一個 POST 路由,用于接收登錄請求。
csrf() 方法是 Spring Security 提供的跨站請求偽造防護功能,當(dāng)我們繼承WebSecurityConfigurerAdapter 會默認開啟 csrf() 方法,關(guān)于 csrf() 方法的更多內(nèi)容會在后面的章節(jié)專門探討,以使測試進程更加順利。
重新啟動服務(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、其他表單配置項
在自定義表單登錄頁之后,處理登錄請求的 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(); } }
此時,有些讀者可能會有疑問,因為按照慣例,在發(fā)送登錄請求并認證成功之后,頁面會跳轉(zhuǎn)回原訪問頁。在某些系統(tǒng)中的確是跳轉(zhuǎn)回原訪問頁的,但在部分前后端完全分離、僅靠 json完成所有交互的系統(tǒng)中,一般會在登錄時返回一段 json數(shù)據(jù),告知前端成功登錄成功與否,由前端決定如何處理后續(xù)邏輯,而非由服務(wù)器主動執(zhí)行頁面跳轉(zhuǎn)。這在 Spring Security 中同樣可以實現(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)的情況自定義。
在形式上,我們確實使用了 Spring Security 的表單認證功能,并且自定義了表單登錄頁。但實際上,這還遠遠不夠。
例如,在實際系統(tǒng)中,我們正常登錄時使用的用戶名和密碼都來自數(shù)據(jù)庫,這里卻都寫在配置上。
更進一步,我們可以對每個登錄用戶都設(shè)定詳細的權(quán)限,而并非一個通用角色。這些內(nèi)容將在后面章節(jié)講解。
到此這篇關(guān)于SpringSecurity中的表單認證詳細解析的文章就介紹到這了,更多相關(guān)SpringSecurity表單認證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Swing SpringLayout彈性布局的實現(xiàn)代碼
這篇文章主要介紹了Java Swing SpringLayout彈性布局的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Java動態(tài)數(shù)組ArrayList實現(xiàn)動態(tài)原理
ArrayList是一種動態(tài)數(shù)組,它可以在運行時自動調(diào)整大小以適應(yīng)元素的添加和刪除,在Java中,你可以使用ArrayList類來實現(xiàn)動態(tài)數(shù)組,本文將給大家介紹一下ArrayList動態(tài)數(shù)組,是怎么實現(xiàn)動態(tài)的2023-08-08String?concat(String?str)使用小結(jié)
這篇文章主要介紹了String?concat(String?str)使用小結(jié),在了解concat()之前,首先需要明確的是String的兩點特殊性,一是長度不可變二是值不可變,本文給大家詳細講解,需要的朋友可以參考下2022-11-11淺析Spring容器原始Bean是如何創(chuàng)建的
這篇文章主要是想和小伙伴們一起聊聊?Spring?容器創(chuàng)建?Bean?最最核心的?createBeanInstance?方法,文中的示例代碼講解詳細,需要的可以參考一下2023-08-08Java中Stringbuild,Date和Calendar類的用法詳解
這篇文章主要為大家詳細介紹了Java中Stringbuild、Date和Calendar類的用法,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下2023-04-04spring boot 實現(xiàn)阿里云視頻點播功能(刪除視頻)
這篇文章主要介紹了spring boot 實現(xiàn)阿里云視頻點播(刪除視頻功能),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12Nacos通過RefreshScope實現(xiàn)配置自動更新的方式分享
這篇文章主要給大家介紹了Nacos如何通過RefreshScope實現(xiàn)配置自動更新,文中給了兩種實現(xiàn)方式供大家參考,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-09-09