springboot?security驗證碼的登錄實例
前言
在spring boot security自定義認證一文,基本給出了一個完整的自定義的用戶登錄認證的示例,但是未涉及到驗證的使用,本文介紹登錄的時候如何使用驗證碼。
本文介紹一個驗證碼生成工具,比較老的一個庫了,僅作demo使用,不太建議生產(chǎn)用了,因為如果你的代碼需要進行安全掃描,這個庫已經(jīng)不再維護了,如果掃出漏洞,也沒法升級修復(fù)了。
但是如果沒有安全掃描的要求,還是可以用的。
github: https://github.com/penggle/kaptcha
代碼示例
引入依賴
<dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
定義驗證碼生成器
@Configuration public class CaptchaConfiguration { @Bean public Producer defaultKaptcha() { Properties properties = new Properties(); // 還有一些其它屬性,可以進行源碼自己看相關(guān)配置,比較清楚了,根據(jù)變量名也能猜出來什么意思了 properties.setProperty(Constants.KAPTCHA_IMAGE_WIDTH, "150"); properties.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT, "50"); properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, "0123456789abcdefghigklmnopqrstuvwxyz"); properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4"); Config config = new Config(properties); DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); defaultKaptcha.setConfig(config); return defaultKaptcha; } }
示例是作為spring 的bean注冊到spring 容器了,當然也可以作為一個單例對象放到一個靜態(tài)類里。
定義獲取驗證碼及認證接口
這個接口在前面的文章里已經(jīng)提到過了,這里只是完善驗證碼的部分.
@RequestMapping("/login") @RestController public class LoginController { private final AuthenticationManager authenticationManager; private final Producer producer; public LoginController(AuthenticationManager authenticationManager, Producer producer) { this.authenticationManager = authenticationManager; this.producer = producer; } @PostMapping() public Object login(@RequestBody User user, HttpSession session) { Object captcha = session.getAttribute(Constants.KAPTCHA_SESSION_KEY); if (captcha == null || !captcha.toString().equalsIgnoreCase(user.getCaptcha())) { return "captcha is not correct."; } try { // 使用定義的AuthenticationManager進行認證處理 Authentication authenticate = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword())); // 認證通過,設(shè)置到當前上下文,如果當前認證過程后續(xù)還有處理的邏輯需要的話。這個示例是沒有必要了 SecurityContextHolder.getContext().setAuthentication(authenticate); return "login success"; } catch (Exception e) { return "login failed"; } } /** * 獲取驗證碼,需要的話,可以提供一個驗證碼獲取的接口,在上面的login里把驗證碼傳進來進行比對 */ @GetMapping("/captcha") public void captcha(HttpServletResponse response, HttpSession session) throws IOException { response.setContentType("image/jpeg"); String text = producer.createText(); session.setAttribute(Constants.KAPTCHA_SESSION_KEY, text); BufferedImage image = producer.createImage(text); try (ServletOutputStream out = response.getOutputStream()) { ImageIO.write(image, "jpg", out); } } }
測試
看一下效果,
獲取驗證碼
登錄
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java生成excel并導(dǎo)出到對應(yīng)位置的方式
這篇文章主要介紹了java生成excel并導(dǎo)出到對應(yīng)位置的方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01Java 實戰(zhàn)項目之疫情人員流動管理系統(tǒng)詳解
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java實現(xiàn)一個疫情人員流動管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11IDEA上面搭建一個SpringBoot的web-mvc項目遇到的問題
這篇文章主要介紹了IDEA上面搭建一個SpringBoot的web-mvc項目遇到的問題小結(jié),需要的朋友可以參考下2017-04-04如何使用Jenkins編譯并打包SpringCloud微服務(wù)目錄
這篇文章主要介紹了如何使用Jenkins編譯并打包SpringCloud微服務(wù)目錄,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下2019-11-11springboot application.yml使用@@pom文件配置問題
這篇文章主要介紹了springboot application.yml使用@@pom文件配置問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07SpringSecurity整合jwt權(quán)限認證的全流程講解
這篇文章主要介紹了SpringSecurity整合jwt權(quán)限認證的全流程講解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06JavaEE Spring MyBatis如何一步一步實現(xiàn)數(shù)據(jù)庫查詢功能
這篇文章主要介紹了JavaEE Spring MyBatis如何一步一步實現(xiàn)數(shù)據(jù)庫查詢功能,本文給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08