Flask接口签名sign原理与实例代码浅析
774
2023-01-18
kaptcha验证码使用方法详解
本文为大家分享了kaptcha验证码的使用方法,供大家参考,具体内容如下
1.首先在pom.xml文件中导入Maven依赖
2.在web.xml文件中配置一个servlet
3.页面代码如下
class="form-control in" placeholder="验证码" />
onclick="changeVerifyCode(this)" src="../Kaptcha" />
向后端提交数据
var verifyCodeActual = $('#j_captcha').val();
if (!verifyCodeActual) {
$.toast('请输入验证码!');
return;
}
formData.append("verifyCodeActual", verifyCodeActual);
4.js交互
function changeVerifyCode(img) {
img.src = "../Kaptcha?" + Math.floor(Mhttp://ath.random() * 100);
}
5.后端验证
import javax.servlet.http.HttpServletRequest;
public class CodeUtil {
public static boolean checkVerifyCode(HttpServletRequest request) {
String verifyCodeExpected = (String) request.getSession().getAttribute(
com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
String verifyCodeActual = HttpServletRequestUtil.getString(request,
"verifyCodeActual");
if (verifyCodeActual == null
|| !verifyCodeActual.equalsIgnoreCase(verifyCodeExpected)) {
return false;
}
return true;
}
}
import javax.servlet.http.HttpServletRequest;
public class HttpServletRequestUtil {
public statDquchEPiLkic int getInt(HttpServletRequest request, String name) {
try {
return Integer.decode(request.getParameter(name));
} catch (Exception e) {
return -1;
}
}
public static long getLong(HttpServletRequest request, String name) {
try {
return Long.valueOf(request.getParameter(name));
} catch (Exception e) {
return -1;
}
}
public static Double getDouble(HttpServletRequest request, String name) {
try {
return Double.valueOf(request.getParameter(name));
} catch (Exception e) {
return -1d;
}
}
public static Boolean getBoolean(HttpServletRequest request, String name) {
try {
return Boolean.valueOf(request.getParameter(name));
} catch (Exception e) {
return false;
}
}
public static String getString(HttpServletRequest request, String name) {
try {
String result = request.getParameter(name);
if (result != null) {
result = result.trim();
}
if ("".equals(result))
result = null;
return result;
} catch (Exception e) {
return null;
}
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~