Spring Security基于自定義的認證提供器實現(xiàn)圖形驗證碼流程解析
前言
在上一個章節(jié)中,一一哥 帶大家實現(xiàn)了如何在Spring Security中添加執(zhí)行自定義的過濾器,進而實現(xiàn)驗證碼校驗功能。這種實現(xiàn)方式,只是實現(xiàn)驗證碼功能的方式之一,接下來我們再學習另一種實現(xiàn)方式,就是利用AuthenticationProver來實現(xiàn)驗證碼功能,通過這個案例,我們學習如何進行自定義AuthenticationProver。
一. 認證提供器簡介
在上一章節(jié)中,我?guī)Ц魑焕米远x的過濾器實現(xiàn)了圖形驗證碼效果,接下來我們利用另一種方式,基于自定義的認證提供器來實現(xiàn)圖形驗證碼。
1. 認證提供器AuthenticationProver
在第11章節(jié)中,壹哥 給大家講過Spring Security的認證授權實現(xiàn)流程,其中就給大家講解過AuthenticationProver的作用,接下來我們看一下AuthenticationProver接口的類關系圖:
從上圖中可知,AuthenticationProver是一個接口,該接口有一個直接的子類AbstractUserDetailsAuthenticationProver,該類有2個抽象的方法:additionalAuthenticationChecks() 和 retrieveUser(),如下圖:
我們可以通過編寫一個子類繼承AbstractUserDetailsAuthenticationProver,復寫這2個抽象方法,進行滿足自己需求的擴展實現(xiàn)。Spring Security中的DaoAuthenticationProver子類就是通過復寫這2個抽象方法,實現(xiàn)了基于數(shù)據(jù)庫模型的認證授權。
我們今天會通過繼承DaoAuthenticationProver,來實現(xiàn)圖形驗證碼的校驗功能。
2. WebAuthenticationDetails類介紹
了解完上面的AuthenticationProver類之后,我們還需要了解另一個類WebAuthenticationDetails。
我們知道在Spring Security中有一個UsernamePasswordAuthenticationToken類,封裝了用戶的principal、credentials信息,該類還從它的父類AbstractAuthenticationToken中繼承了details信息。其中這個details信息表示認證用戶的額外信息,比如請求用戶的remoteAddress和sessionId等信息,這兩個信息都是在另一個WebAuthenticationDetails類中定義的,所以我們可以利用WebAuthenticationDetails來封裝用戶的額外信息。
了解完上面的這些必要的API,我們就可以實現(xiàn)今天的需求了。
二. 實現(xiàn)圖形驗證碼
1. 添加依賴包
我們還是和之前的案例一樣,可以先創(chuàng)建一個新的module,創(chuàng)建過程略。
在本案例中我們依然采用github上的開源驗證碼解決方案kaptcha,所以需要在原有項目的基礎上添加kaptcha的依賴包。
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2. 創(chuàng)建Producer對象
跟上一個案例一樣,創(chuàng)建CaptchaConfig配置類,在該類中創(chuàng)建一個Producer對象,對驗證碼對象進行必要的配置。
@Configuration public class CaptchaConfig { @Bean public Producer captcha() { // 配置圖形驗證碼的基本參數(shù) Properties properties = new Properties(); // 圖片寬度 properties.setProperty("kaptcha.image.wth", "150"); // 圖片長度 properties.setProperty("kaptcha.image.height", "50"); // 字符集 properties.setProperty("kaptcha.textproducer.char.string", "0123456789"); // 字符長度 properties.setProperty("kaptcha.textproducer.char.length", "4"); Config config = new Config(properties); // 使用默認的圖形驗證碼實現(xiàn),當然也可以自定義實現(xiàn) DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); defaultKaptcha.setConfig(config); return defaultKaptcha; } }
3. 創(chuàng)建生成驗證碼的接口
在上面創(chuàng)建了Producer對象后,接著創(chuàng)建一個生成驗證碼的接口,該接口中負責生成驗證碼圖片,并將驗證碼存儲到session中。
@Controller public class CaptchaController { @Autowired private Producer captchaProducer; @GetMapping("/captcha.jpg") public vo getCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException { // 設置內容類型 response.setContentType("image/jpeg"); // 創(chuàng)建驗證碼文本 String capText = captchaProducer.createText(); // 將驗證碼文本設置到session request.getSession().setAttribute("captcha", capText); // 創(chuàng)建驗證碼圖片 BufferedImage bi = captchaProducer.createImage(capText); // 獲取響應輸出流 ServletOutputStream out = response.getOutputStream(); // 將圖片驗證碼數(shù)據(jù)寫到響應輸出流 ImageIO.write(bi, "jpg", out); // 推送并關閉響應輸出流 try { out.flush(); } finally { out.close(); } } }
4. 自定義異常
接下來自定義一個運行時異常,用于處理驗證碼校驗失敗時拋出異常提示信息。
public class VerificationCodeException extends AuthenticationException { public VerificationCodeException() { super("圖形驗證碼校驗失敗"); } }
5. 自定義WebAuthenticationDetails
我在上面給大家介紹過WebAuthenticationDetails這個類,知道該類中可以封裝用戶的額外信息,所以在這里我們自定義一個WebAuthenticationDetails類,封裝驗證碼信息,并把用戶傳遞過來的驗證碼與session中保存的驗證碼進行對比。
/** * 添加額外的用戶認證信息 */ public class MyWebAuthenticationDetails extends WebAuthenticationDetails { private String imageCode; private String savedImageCode; public String getImageCode() { return imageCode; } public String getSavedImageCode() { return savedImageCode; } /** * 補充用戶提交的驗證碼和session保存的驗證碼 */ public MyWebAuthenticationDetails(HttpServletRequest request) { super(request); this.imageCode = request.getParameter("captcha"); //獲取session對象 HttpSession session = request.getSession(); this.savedImageCode = (String) session.getAttribute("captcha"); if (!StringUtils.isEmpty(this.savedImageCode)) { // 隨手清除驗證碼,不管是失敗還是成功,所以客戶端應在登錄失敗時刷新驗證碼 session.removeAttribute("captcha"); } } }
6. 自定義AuthenticationDetailsSource
AuthenticationDetailsSource是一個接口,該接口帶有一個buildDetails方法,該方法會在創(chuàng)建一個新的authentication的details對象時被調用,而且可以在這里傳遞給details對象一個request參數(shù),如下圖所示:
所以這里我們定義一個AuthenticationDetailsSource類,通過該類構建出上面定義的WebAuthenticationDetails對象,并且給WebAuthenticationDetails傳遞進去HttpServletRequest對象。
@Component public class MyWebAuthenticationDetailsSource implements AuthenticationDetailsSource<HttpServletRequest,WebAuthenticationDetails> { /** * 創(chuàng)建一個WebAuthenticationDetails對象 */ @Overre public WebAuthenticationDetails buildDetails(HttpServletRequest request) { return new MyWebAuthenticationDetails(request); } }
7. 自定義DaoAuthenticationProver
接下來通過繼承DaoAuthenticationProver父類,來引入對圖形驗證碼的驗證操作。
/** * 在常規(guī)的數(shù)據(jù)庫認證之上,添加圖形驗證碼功能 */ @Component public class MyAuthenticationProver extends DaoAuthenticationProver { /** * 構造方法注入UserDetailService和PasswordEncoder */ public MyAuthenticationProver(UserDetailsService userDetailsService, PasswordEncoder passwordEncoder) { this.setUserDetailsService(userDetailsService); this.setPasswordEncoder(passwordEncoder); } /** * 在常規(guī)的認證之上,添加額外的圖形驗證碼功能 */ @Overre protected vo additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken) throws AuthenticationException { //獲取token令牌中關聯(lián)的details對象,并將其轉換為我們自定義的MyWebAuthenticationDetails MyWebAuthenticationDetails details = (MyWebAuthenticationDetails) usernamePasswordAuthenticationToken.getDetails(); String imageCode = details.getImageCode(); String savedImageCode = details.getSavedImageCode(); // 檢驗圖形驗證碼 if (StringUtils.isEmpty(imageCode) || StringUtils.isEmpty(savedImageCode) || !imageCode.equals(savedImageCode)) { throw new VerificationCodeException(); } //在正常的認證檢查之前,添加額外的關于圖形驗證碼的校驗 super.additionalAuthenticationChecks(userDetails, usernamePasswordAuthenticationToken); } }
8. 添加SecurityConfig
然后創(chuàng)建編寫SecurityConfig類,關聯(lián)配置我們前面編寫的AuthenticationDetailsSource和AuthenticationProver類。
@SuppressWarnings("all") @EnableWebSecurity(debug = true) public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private AuthenticationDetailsSource<HttpServletRequest, WebAuthenticationDetails> myWebAuthenticationDetailsSource; @Autowired private AuthenticationProver authenticationProver; @Overre protected vo configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/admin/api/**") .hasRole("ADMIN") .antMatchers("/user/api/**") .hasRole("USER") .antMatchers("/app/api/**", "/captcha.jpg") .permitAll() .anyRequest() .authenticated() .and() .formLogin() //這里關聯(lián)配置自定義的AuthenticationDetailsSource .authenticationDetailsSource(myWebAuthenticationDetailsSource) .failureHandler(new SecurityAuthenticationFailureHandler()) .successHandler(new SecurityAuthenticationSuccessHandler()) .loginPage("/myLogin.html") .loginProcessingUrl("/login") .permitAll() .and() .csrf() .disable(); } //在這里關聯(lián)我們自定義的AuthenticationProver @Overre protected vo configure(AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProver(authenticationProver); } @Bean public PasswordEncoder passwordEncoder() { return NoOpPasswordEncoder.getInstance(); } }
9. 編寫測試頁面
最后編寫一個自定義的登錄頁面,在這里添加對驗證碼接口的引用,我這里列出html的核心代碼。
<body> <div class="login"> <h2>Access Form</h2> <div class="login-top"> <h1>登錄驗證</h1> <form action="/login" method="post"> <input type="text" name="username" placeholder="username" /> <input type="password" name="password" placeholder="password" /> <div style="display: flex;"> <!-- 新增圖形驗證碼的輸入框 --> <input type="text" name="captcha" placeholder="captcha" /> <!-- 圖片指向圖形驗證碼API --> <img src="/captcha.jpg" alt="captcha" height="50px" wth="150px" style="margin-left: 20px;"> </div> <div class="forgot"> <a href="#" rel="external nofollow" rel="external nofollow" >忘記密碼</a> <input type="submit" value="登錄" > </div> </form> </div> <div class="login-bottom"> <h3>新用戶 <a href="#" rel="external nofollow" rel="external nofollow" >注 冊</a></h3> </div> </div> </body>
10. 代碼結構
本案例的主要代碼結構如下圖所示,各位可以參考創(chuàng)建。
11. 啟動項目測試
接下來我們啟動項目,跳轉到登錄頁面后,我們就可以看到驗證碼已經(jīng)被創(chuàng)建出來了。
此時我們可以看到生成的數(shù)字驗證碼,在我們輸入正確的用戶名、密碼、驗證碼后,就可以成功的登錄進去訪問web接口了。
至此,我們就實現(xiàn)了基于自定義的認證提供器來實現(xiàn)圖形驗證碼功能了,這種實現(xiàn)方式要比第一種實現(xiàn)方式更復雜一些,其實都能滿足我們的開發(fā)需求。有的小伙伴會問,開發(fā)時到底選擇哪一種方式呢?壹哥覺得都無所謂的!你有什么更好的見解嗎?可以在評論區(qū)留言哦!
到此這篇關于Spring Security基于自定義的認證提供器實現(xiàn)圖形驗證碼的文章就介紹到這了,更多相關Spring Security認證提供器實現(xiàn)圖形驗證碼內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解SpringBoot如何實現(xiàn)多環(huán)境配置
在實際的軟件開發(fā)過程中,一個應用程序通常會有多個環(huán)境,pring?Boot?提供了一個非常靈活和強大的方式來管理這些環(huán)境配置,下面就跟隨小編一起學習一下吧2023-07-07springmvc fastjson 反序列化時間格式化方法(推薦)
下面小編就為大家?guī)硪黄猻pringmvc fastjson 反序列化時間格式化方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04SpringMVC中事務是否可以加在Controller層的問題
這篇文章主要介紹了SpringMVC中事務是否可以加在Controller層的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02mybatis-plus @DS實現(xiàn)動態(tài)切換數(shù)據(jù)源原理
本文主要介紹了mybatis-plus @DS實現(xiàn)動態(tài)切換數(shù)據(jù)源原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07Python如何使用@property @x.setter及@x.deleter
這篇文章主要介紹了Python如何使用@property @x.setter及@x.deleter,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-05-05SpringSecurity從數(shù)據(jù)庫中獲取用戶信息進行驗證的案例詳解
這篇文章主要介紹了SpringSecurity從數(shù)據(jù)庫中獲取用戶信息進行驗證的案例詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01