SpringBoot前后端分離實(shí)現(xiàn)驗(yàn)證碼操作
1.SpringBoot版本
本文基于的Spring Boot的版本是2.6.7 。
2.引入依賴
captcha一款超簡(jiǎn)單的驗(yàn)證碼生成,還挺好玩的.還有中文驗(yàn)證碼,動(dòng)態(tài)驗(yàn)證碼. 。在項(xiàng)目中pom.xml配置文件中添加依賴,如下:
<!--驗(yàn)證碼--> <dependency> <groupId>com.github.whvcse</groupId> <artifactId>easy-captcha</artifactId> <version>1.6.2</version> </dependency>
3.實(shí)現(xiàn)思路
把生成的驗(yàn)證碼結(jié)果保存到redis緩存中,并設(shè)置過(guò)期時(shí)間。
前端通過(guò)提交驗(yàn)證碼和key,其中key就是保存到redis中的鍵,通過(guò)這個(gè)鍵獲取到對(duì)應(yīng)的值,再與前端提交的值對(duì)比,相同就通過(guò)驗(yàn)證。
新建驗(yàn)證碼枚舉類
由于captcha這款驗(yàn)證碼提供了好幾種驗(yàn)證碼方法,有中文驗(yàn)證碼,動(dòng)態(tài)驗(yàn)證碼,算術(shù)驗(yàn)證碼等等,新建一個(gè)驗(yàn)證碼每周類存放這幾種驗(yàn)證碼類型。代碼如下:
public enum LoginCodeEnum { /** * 算數(shù) */ ARITHMETIC, /** * 中文 */ CHINESE, /** * 中文閃圖 */ CHINESE_GIF, /** * 閃圖 */ GIF, SPEC }
定義驗(yàn)證碼配置信息
該類是定義驗(yàn)證碼的基本信息,例如高度、寬度、字體類型、驗(yàn)證碼類型等等、并且我們把它轉(zhuǎn)成通過(guò)SpringBoot配置文件類型來(lái)定義更加方便。
@Data public class LoginCode { /** * 驗(yàn)證碼配置 */ private LoginCodeEnum codeType; /** * 驗(yàn)證碼有效期 分鐘 */ private Long expiration = 2L; /** * 驗(yàn)證碼內(nèi)容長(zhǎng)度 */ private int length = 2; /** * 驗(yàn)證碼寬度 */ private int width = 111; /** * 驗(yàn)證碼高度 */ private int height = 36; /** * 驗(yàn)證碼字體 */ private String fontName; /** * 字體大小 */ private int fontSize = 25; /** * 驗(yàn)證碼前綴 * @return */ private String codeKey; public LoginCodeEnum getCodeType() { return codeType; } }
把配置文件轉(zhuǎn)換Pojo類的統(tǒng)一配置類
@Configuration public class ConfigBeanConfiguration { @Bean @ConfigurationProperties(prefix = "login") public LoginProperties loginProperties() { return new LoginProperties(); } }
定義驗(yàn)證邏輯生成類
@Data public class LoginProperties { private LoginCode loginCode; /** * 獲取驗(yàn)證碼生產(chǎn)類 * @return */ public Captcha getCaptcha(){ if(Objects.isNull(loginCode)){ loginCode = new LoginCode(); if(Objects.isNull(loginCode.getCodeType())){ loginCode.setCodeType(LoginCodeEnum.ARITHMETIC); } } return switchCaptcha(loginCode); } /** * 依據(jù)配置信息生產(chǎn)驗(yàn)證碼 * @param loginCode * @return */ private Captcha switchCaptcha(LoginCode loginCode){ Captcha captcha = null; synchronized (this){ switch (loginCode.getCodeType()){ case ARITHMETIC: captcha = new FixedArithmeticCaptcha(loginCode.getWidth(),loginCode.getHeight()); captcha.setLen(loginCode.getLength()); break; case CHINESE: captcha = new ChineseCaptcha(loginCode.getWidth(),loginCode.getHeight()); captcha.setLen(loginCode.getLength()); break; case CHINESE_GIF: captcha = new ChineseGifCaptcha(loginCode.getWidth(),loginCode.getHeight()); captcha.setLen(loginCode.getLength()); break; case GIF: captcha = new GifCaptcha(loginCode.getWidth(),loginCode.getHeight()); captcha.setLen(loginCode.getLength()); break; case SPEC: captcha = new SpecCaptcha(loginCode.getWidth(),loginCode.getHeight()); captcha.setLen(loginCode.getLength()); default: System.out.println("驗(yàn)證碼配置信息錯(cuò)誤!正確配置查看 LoginCodeEnum "); } } if(StringUtils.isNotBlank(loginCode.getFontName())){ captcha.setFont(new Font(loginCode.getFontName(),Font.PLAIN,loginCode.getFontSize())); } return captcha; } static class FixedArithmeticCaptcha extends ArithmeticCaptcha{ public FixedArithmeticCaptcha(int width,int height){ super(width,height); } @Override protected char[] alphas() { // 生成隨機(jī)數(shù)字和運(yùn)算符 int n1 = num(1, 10), n2 = num(1, 10); int opt = num(3); // 計(jì)算結(jié)果 int res = new int[]{n1 + n2, n1 - n2, n1 * n2}[opt]; // 轉(zhuǎn)換為字符運(yùn)算符 char optChar = "+-x".charAt(opt); this.setArithmeticString(String.format("%s%c%s=?", n1, optChar, n2)); this.chars = String.valueOf(res); return chars.toCharArray(); } } }
在控制層上定義驗(yàn)證碼生成接口
@ApiOperation(value = "獲取驗(yàn)證碼", notes = "獲取驗(yàn)證碼") @GetMapping("/code") public Object getCode(){ Captcha captcha = loginProperties.getCaptcha(); String uuid = "code-key-"+IdUtil.simpleUUID(); //當(dāng)驗(yàn)證碼類型為 arithmetic時(shí)且長(zhǎng)度 >= 2 時(shí),captcha.text()的結(jié)果有幾率為浮點(diǎn)型 String captchaValue = captcha.text(); if(captcha.getCharType()-1 == LoginCodeEnum.ARITHMETIC.ordinal() && captchaValue.contains(".")){ captchaValue = captchaValue.split("\\.")[0]; } // 保存 redisUtils.set(uuid,captchaValue,loginProperties.getLoginCode().getExpiration(), TimeUnit.MINUTES); // 驗(yàn)證碼信息 Map<String,Object> imgResult = new HashMap<String,Object>(2){{ put("img",captcha.toBase64()); put("uuid",uuid); }}; return imgResult; }
效果體驗(yàn)
在前端調(diào)用接口
<template> <div class="login-code"> <img :src="codeUrl" @click="getCode"> </div> </template> <script> methods: { getCode() { getCodeImg().then(res => { this.codeUrl = res.data.img this.loginForm.uuid = res.data.uuid }) }, } created() { // 獲取驗(yàn)證碼 this.getCode() }, </script>
到此這篇關(guān)于SpringBoot前后端分離實(shí)現(xiàn)驗(yàn)證碼操作的文章就介紹到這了,更多相關(guān)SpringBoot驗(yàn)證碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot實(shí)現(xiàn)發(fā)送驗(yàn)證碼功能(圖片驗(yàn)證碼)
- 基于SpringBoot和Hutool工具包實(shí)現(xiàn)驗(yàn)證碼的案例
- SpringBoot使用hutool-captcha實(shí)現(xiàn)驗(yàn)證碼生成與驗(yàn)證
- SpringBoot 分布式驗(yàn)證碼登錄方案示例詳解
- SpringBoot整合Kaptcha實(shí)現(xiàn)圖形驗(yàn)證碼功能
- springboot驗(yàn)證碼的生成與驗(yàn)證的兩種方法
- vue+springboot實(shí)現(xiàn)登錄驗(yàn)證碼
- SpringBoot登錄驗(yàn)證碼實(shí)現(xiàn)過(guò)程詳解
- 基于SpringBoot實(shí)現(xiàn)驗(yàn)證碼功能(兩種驗(yàn)證碼方式)
相關(guān)文章
Windows下gradle的安裝與配置的超詳細(xì)教程
這篇文章主要介紹了Windows下gradle的安裝與配置,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)詳解流程
這篇文章主要為大家詳細(xì)介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06詳解Spring依賴注入的三種方式使用及優(yōu)缺點(diǎn)
這篇文章主要介紹了spring依賴注入的三種方式的使用方法,以及優(yōu)缺點(diǎn)的介紹,通過(guò)代碼示例介紹的非常詳細(xì),感興趣的小伙伴可以參考一下2023-04-04java隨機(jī)數(shù)生成具體實(shí)現(xiàn)代碼
這篇文章主要為大家分享了java隨機(jī)數(shù)生成具體實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04Java 獲取網(wǎng)絡(luò)302重定向URL的方法
在本篇文章里小編給大家整理的是關(guān)于Java 獲取網(wǎng)絡(luò)302重定向URL的方法以及相關(guān)知識(shí)點(diǎn),有興趣的朋友們參考下。2019-08-08Spring boot從安裝到交互功能實(shí)現(xiàn)零基礎(chǔ)全程詳解
這篇文章主要介紹了Spring boot從安裝到交互功能得實(shí)現(xiàn)全程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07springboot中的controller注意事項(xiàng)說(shuō)明
這篇文章主要介紹了springboot中的controller注意事項(xiàng)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Java實(shí)現(xiàn)雪花算法(snowflake)
這篇文章主要介紹了Java實(shí)現(xiàn)雪花算法(snowflake),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08