springboot實(shí)現(xiàn)極驗(yàn)校驗(yàn)的項(xiàng)目實(shí)踐
概述
在系統(tǒng)業(yè)務(wù)中,需要想客戶(hù)發(fā)送手機(jī)驗(yàn)證碼,進(jìn)行驗(yàn)證后,才能提交。但為了防止不正當(dāng)?shù)亩绦虐l(fā)送(攻擊,惡意操作等),需要在發(fā)送短信前添加一個(gè)行為驗(yàn)證(這里使用的是極驗(yàn))
詳細(xì)
一、運(yùn)行效果
二、實(shí)現(xiàn)過(guò)程
①、步驟
②、搭建geetest后臺(tái)
gt3-java-sdk-master\src\sdk\GeetestLib.java 這個(gè)文件相當(dāng)java中的實(shí)體類(lèi),直接放在我的domain文件下。
gt3-java-sdk-master\src\demo\demo1\GeetestConfig.java ,是geetest的配置文件,用來(lái)放我們?cè)跇O驗(yàn)后臺(tái)注冊(cè)應(yīng)用得到的captcha_id和private_key。
VerifyLoginServlet.java(驗(yàn)證) 和 StartCaptchaServlet.java(初始化),這兩個(gè)文件就是兩個(gè)servlet,我直接放到了我寫(xiě)的一個(gè)Controller里面;
package com.zxh.config; /** * GeetestWeb配置文件 * * */ public class GeetestConfig { // 填入自己的captcha_id和private_key private static final String geetest_id = "xxx"; private static final String geetest_key = "xxxx"; private static final boolean newfailback = true; public static final String getGeetest_id() { return geetest_id; } public static final String getGeetest_key() { return geetest_key; } public static final boolean isnewfailback() { return newfailback; } } package com.zxh.controller; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Objects; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.zxh.config.GeetestConfig; import com.zxh.sdk.GeetestLib; import com.zxh.util.IpUtil; @Controller @RequestMapping("/public") public class PublicController { @RequestMapping(value = "/register", method = RequestMethod.GET) @ResponseBody public void register(HttpServletRequest request, HttpServletResponse response) throws IOException { GeetestLib gtSdk = new GeetestLib(GeetestConfig.getGeetest_id(), GeetestConfig.getGeetest_key(), GeetestConfig.isnewfailback()); String resStr = "{}"; // 自定義userid HttpSession session = request.getSession(); // User baseUser = (User) session.getAttribute("baseUser"); // String userid = null; // if (Objects.nonNull(baseUser)) { // userid = baseUser.getUserName(); // } // 自定義參數(shù),可選擇添加 HashMap<String, String> param = new HashMap<String, String>(); param.put("user_id", "username"); // 網(wǎng)站用戶(hù)id param.put("ip_address", IpUtil.getIpAddr(request)); // 傳輸用戶(hù)請(qǐng)求驗(yàn)證時(shí)所攜帶的IP // 進(jìn)行驗(yàn)證預(yù)處理 int gtServerStatus = gtSdk.preProcess(param); // 將服務(wù)器狀態(tài)設(shè)置到session中 request.getSession().setAttribute(gtSdk.gtServerStatusSessionKey, gtServerStatus); // 將userid設(shè)置到session中 request.getSession().setAttribute("userid", "username"); resStr = gtSdk.getResponseStr(); PrintWriter out = response.getWriter(); out.println(resStr); } }
③、構(gòu)建geetest的前端
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>springboot-jytest-demo</title> <style> body { margin: 50px 0; text-align: center; font-family: "PingFangSC-Regular", "Open Sans", Arial, "Hiragino Sans GB", "Microsoft YaHei", "STHeiti", "WenQuanYi Micro Hei", SimSun, sans-serif; } .inp { border: 1px solid #cccccc; border-radius: 2px; padding: 0 10px; width: 278px; height: 40px; font-size: 18px; } .btn { border: 1px solid #cccccc; border-radius: 2px; width: 100px; height: 40px; font-size: 16px; color: #666; cursor: pointer; background: white linear-gradient(180deg, #ffffff 0%, #f3f3f3 100%); } .btn:hover { background: white linear-gradient(0deg, #ffffff 0%, #f3f3f3 100%) } #captcha1, #captcha2 { width: 300px; display: inline-block; } .show { display: block; } .hide { display: none; } #notice1, #notice2 { color: red; } label { vertical-align: top; display: inline-block; width: 80px; text-align: right; } #wait1, #wait2 { text-align: left; color: #666; margin: 0; } </style> </head> <body> <h1>極驗(yàn)驗(yàn)證SDKDemo</h1> <hr> <!-- 注意,驗(yàn)證碼本身是不需要 jquery 庫(kù),此處使用 jquery 僅為了在 demo 使用,減少代碼量 --> <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.js"></script> <!-- 引入 gt.js,既可以使用其中提供的 initGeetest 初始化函數(shù) --> <script src="gt.js"></script> <br><br> <hr> <form> <h2>滑動(dòng)demo,使用ajax進(jìn)行二次驗(yàn)證</h2> <br> <div> <label for="username2">用戶(hù)名:</label> <input class="inp" id="username2" type="text" value="admin"> </div> <br> <div> <label for="password2">密碼:</label> <input class="inp" id="password2" type="password" value="123456"> </div> <br> <div> <label>完成驗(yàn)證:</label> <div id="captcha2"> <p id="wait2" class="show">正在加載驗(yàn)證碼......</p> </div> </div> <br> <p id="notice2" class="hide">請(qǐng)先完成驗(yàn)證</p> <input class="btn" id="submit2" type="submit" value="提交"> </form> <script> var handler2 = function (captchaObj) { $("#submit2").click(function (e) { var result = captchaObj.getValidate(); if (!result) { $("#notice2").show(); setTimeout(function () { $("#notice2").hide(); }, 2000); } else { $.ajax({ url: 'login/test', type: 'POST', dataType: 'json', data: { username: $('#username2').val(), password: $('#password2').val(), geetest_challenge: result.geetest_challenge, geetest_validate: result.geetest_validate, geetest_seccode: result.geetest_seccode }, success: function (data) { if (data.status === 'success') { alert('登錄成功'); } else if (data.status === 'fail') { alert('登錄失敗'); } } }) } e.preventDefault(); }); // 將驗(yàn)證碼加到id為captcha的元素里,同時(shí)會(huì)有三個(gè)input的值用于表單提交 captchaObj.appendTo("#captcha2"); captchaObj.onReady(function () { $("#wait2").hide(); }); // 更多接口參考:http://www.geetest.com/install/sections/idx-client-sdk.html }; $.ajax({ url: "public/register?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { // 調(diào)用 initGeetest 初始化參數(shù) // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ gt: data.gt, challenge: data.challenge, new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) offline: !data.success, // 表示用戶(hù)后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī),一般不需要關(guān)注 product: "popup", // 產(chǎn)品形式,包括:float,popup width: "100%" // 更多配置參數(shù)請(qǐng)參見(jiàn):http://www.geetest.com/install/sections/idx-client-sdk.html#config }, handler2); } }); </script> </body> </html>
三、項(xiàng)目結(jié)構(gòu)圖
四、補(bǔ)充
極驗(yàn)驗(yàn)證碼,是用于部署在網(wǎng)站或移動(dòng)應(yīng)用的交互環(huán)節(jié),能保障網(wǎng)站或移動(dòng)應(yīng)用的驗(yàn)證安全。
到此這篇關(guān)于springboot實(shí)現(xiàn)極驗(yàn)校驗(yàn)的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)springboot 極驗(yàn)校驗(yàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot實(shí)現(xiàn)前端驗(yàn)證碼圖片生成和校驗(yàn)
- SpringBoot實(shí)現(xiàn)短信驗(yàn)證碼校驗(yàn)方法思路詳解
- SpringBoot中@Pattern注解對(duì)時(shí)間格式校驗(yàn)方式
- SpringBoot如何優(yōu)雅的處理校驗(yàn)參數(shù)的方法
- SpringBoot進(jìn)行參數(shù)校驗(yàn)的方法詳解
- SpringBoot集成JWT生成token及校驗(yàn)方法過(guò)程解析
- springboot使用hibernate validator校驗(yàn)方式
- springboot使用Validator校驗(yàn)方式
- springboot validator枚舉值校驗(yàn)功能實(shí)現(xiàn)
相關(guān)文章
一篇文章教你用Java使用JVM工具檢測(cè)問(wèn)題
這篇文章主要介紹了深入理解Java使用JVM工具檢測(cè)問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-09-09解決BigDecimal轉(zhuǎn)long丟失精度的問(wèn)題
這篇文章主要介紹了解決BigDecimal轉(zhuǎn)long丟失精度的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12Java日期工具類(lèi)DateUtils實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了Java日期工具類(lèi)DateUtils實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Jmeter對(duì)響應(yīng)數(shù)據(jù)實(shí)現(xiàn)斷言代碼實(shí)例
這篇文章主要介紹了Jmeter對(duì)響應(yīng)數(shù)據(jù)實(shí)現(xiàn)斷言代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09JDK1.7 之java.nio.file.Files 讀取文件僅需一行代碼實(shí)現(xiàn)
下面小編就為大家分享一篇JDK1.7 之java.nio.file.Files 讀取文件僅需一行代碼實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助2017-11-11MyBatis-Plus實(shí)現(xiàn)多數(shù)據(jù)源的示例代碼
這篇文章主要介紹了MyBatis-Plus實(shí)現(xiàn)多數(shù)據(jù)源的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Springboot項(xiàng)目?jī)?yōu)雅地處理日志的方法詳解
這篇文章主要介紹了Springboot項(xiàng)目---優(yōu)雅地處理日志,本文通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07Spring的RedisTemplate存儲(chǔ)的key和value有特殊字符的處理
這篇文章主要介紹了Spring的RedisTemplate存儲(chǔ)的key和value有特殊字符的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12