springbooot使用google驗證碼的功能實現(xiàn)
springbooot使用google驗證碼
1、使用場景
由于需要做一個前后端分離的項目,想著使用google驗證碼,由于年齡大了,這些知識啊,用完就忘,在這里記錄一下。
登錄時驗證碼設(shè)計:
- 使用google驗證碼工具,當(dāng)前端在登錄請求時,在后端生成驗證碼,同時也生成一個隨機數(shù)(UUID)與該驗證碼對應(yīng)。
- 使用redis作為緩存,將該隨機數(shù)和驗證碼存儲在redis中。
- 隨機數(shù)的目的是將驗證碼與發(fā)起登錄請求的用戶聯(lián)系起來。
- 當(dāng)用戶提交登錄表單時,后端根據(jù)該隨機數(shù)從redis中讀取驗證碼與用戶輸入的驗證碼進行驗證。
大概就是這樣的一個設(shè)計思路,具體如下:
2、springboot使用google驗證碼
1、引入依賴
首先在pom文件中引入該驗證碼插件kaptcha
<!-- google 驗證碼 --> <!-- https://mvnrepository.com/artifact/com.github.penggle/kaptcha --> <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
2、編寫配置類
引入依賴之后還需要編寫配置類,在配置類里設(shè)置自己想要的驗證碼樣式,包括顏色、大小、寬高等等。
我的配置類如下:
import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.Properties; @Configuration public class KaptchaConfig { @Bean DefaultKaptcha producer() { //驗證碼的配置類 Properties properties = new Properties(); properties.put("kaptcha.border", "no"); //邊框 properties.put("kaptcha.textproducer.font.color", "black"); //字體顏色 properties.put("kaptcha.textproducer.char.space", "5"); //字體間隔 properties.put("kaptcha.image.height", "40"); //圖片高度 properties.put("kaptcha.image.width", "100"); //圖片寬度 properties.put("kaptcha.textproducer.font.size", "30"); //字體大小 Config config = new Config(properties); DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); defaultKaptcha.setConfig(config); return defaultKaptcha; } }
3、編寫控制層
將下面的代碼放到需要使用驗證碼的controller中
//獲取登錄驗證碼 @GetMapping("/captcha") public Result Captcha() throws IOException { String key = UUID.randomUUID().toString(); String code = producer.createText(); BufferedImage image = producer.createImage(code); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ImageIO.write(image, "jpg", outputStream); BASE64Encoder encoder = new BASE64Encoder(); String str = "data:image/jpeg;base64,"; String base64Img = str + encoder.encode(outputStream.toByteArray()); redisUtils.hset(Constants.CAPTCHA_KEY, key, code, 120); return Result.succ( MapUtil.builder() .put("userKey", key) .put("captchaImg", base64Img) .build() ); }
上面用到了封裝的redis工具類redisUtils中的hset方法,并設(shè)置了驗證碼過期時間120秒。
hset方法如下:
/** * 向一張hash表中放入數(shù)據(jù),如果不存在將創(chuàng)建 * * @param key 鍵 * @param item 項 * @param value 值 * @param time 時間(秒) 注意:如果已存在的hash表有時間,這里將會替換原有的時間 * @return true 成功 false失敗 */ public boolean hset(String key, String item, Object value, long time) { try { redisTemplate.opsForHash().put(key, item, value); if (time > 0) { expire(key, time); } return true; } catch (Exception e) { e.printStackTrace(); return false; } }
Result是編寫的統(tǒng)一結(jié)果返回類,代碼如下所示:
@Data public class Result_ implements Serializable { private int code; private String msg; private Object data; public static Result_ succ(Object data) { return succ(200, "操作成功", data); } public static Result_ fail(String msg) { return fail(400, msg, null); } public static Result_ succ (int code, String msg, Object data) { Result_ result = new Result_(); result.setCode(code); result.setMsg(msg); result.setData(data); return result; } public static Result_ fail (int code, String msg, Object data) { Result_ result = new Result_(); result.setCode(code); result.setMsg(msg); result.setData(data); return result; } }
這里沒有編寫對于驗證碼的驗證。
4、前端實現(xiàn)
驗證碼輸入框代碼如下:
<el-form-item label="驗證碼" prop="code" style="width: 380px"> <el-input placeholder="請輸入驗證碼"v-model="loginForm.code"style="width: 172px; float: left" ></el-input> <el-image class="captchaImg" :src="captchaImg" @click="getCaptcha()"></el-image> </el-form-item>
驗證碼函數(shù)如下:
// 獲取驗證碼 getCaptcha() { this.$axios.get('/user/captcha1').then(res => { this.loginForm.token = res.data.data.token this.captchaImg = res.data.data.captchaImg this.loginForm.code = '' }) }
到此這篇關(guān)于springbooot使用google驗證碼的文章就介紹到這了,更多相關(guān)springbooot google驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java類加載器與雙親委派機制和線程上下文類加載器專項解讀分析
類加載器負責(zé)讀取Java字節(jié)代碼,并轉(zhuǎn)換成java.lang.Class類的一個實例的代碼模塊。本文主要和大家聊聊JVM類加載器ClassLoader的使用,需要的可以了解一下2022-12-12基于spring+hibernate+JQuery開發(fā)之電子相冊(附源碼下載)
本篇文章介紹了,基于spring+hibernate+JQuery開發(fā)之電子相冊(附源碼下載)。需要的朋友參考下2013-05-05解決spring cloud zuul與nginx的域名轉(zhuǎn)發(fā)問題
這篇文章主要介紹了spring cloud zuul與nginx的域名轉(zhuǎn)發(fā)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Java?HashTable與Collections.synchronizedMap源碼深入解析
HashTable是jdk?1.0中引入的產(chǎn)物,基本上現(xiàn)在很少使用了,但是會在面試中經(jīng)常被問到。本文就來帶大家一起深入了解一下Hashtable,需要的可以參考一下2022-11-11