大家好,又见面了,我是你们的朋友全栈君。
使用Kaptcha这个验证码框架用起来,比自己写简单
首先设置maven项目:
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
然后配置WEB-INF下的web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet-mapping>
<servlet-name>kaptchaDemo</servlet-name>
<url-pattern>/kaptcha</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>kaptchaDemo</servlet-name>
<servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
<init-param>
<param-name>kaptcha.border</param-name>
<param-value>no</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.font.color</param-name>
<param-value>red</param-value>
</init-param>
<init-param>
<param-name>kaptcha.image.width</param-name>
<param-value>80</param-value>
</init-param>
<init-param>
<param-name>kaptcha.image.height</param-name>
<param-value>30</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.char.string</param-name>
<param-value>0123456789</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.char.length</param-name>
<param-value>4</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.char.space</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.font.size</param-name>
<param-value>24</param-value>
</init-param>
<!-- <init-param>-->
<!-- <param-name>kaptcha.noise.impl</param-name>-->
<!-- <param-value>com.google.code.kaptcha.impl.NoNoise</param-value>-->
<!-- </init-param>-->
</servlet>
</web-app>
其中对应的jsp页面如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>kaptchaDemo</title>
<style type="text/css">
.code_a{
color: #0000ff;
font-size: 12px;
text-decoration: none;
cursor: pointer;
}
#imgCode{
cursor: pointer;
}
</style>
<script type="text/javascript">
function changeCode(){
var imgCode = document.getElementById("imgCode");
imgCode.src = "kaptcha?"+Math.random();
}
</script>
</head>
<body>
<form action="login" method="post">
<label>验证码:</label>
<input type="text" id="inCode" name="inCode" />
<img src="kaptcha" align="center" id="imgCode" onclick="changeCode()" />
<a class="code_a" onclick="changeCode()">换一张</a><br />
<input type="submit" value="登录"/>
</form>
</body>
</html>
这里src请求的是kaptcha的url。这里这个框架里面实现了一个Servlet,通过配置文件的方式,对某些属性进行设置(这里不用说了,肯定是反射)
程序运行截图如下:
输入错误时,依旧是这个界面,但输入正确后!
下面是关于他的验证方面的代码:
LoginServlet.java
import com.google.code.kaptcha.Constants;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(value = "/login")
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String vailCode = request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY).toString().toLowerCase();
String inCode = request.getParameter("inCode").toLowerCase();
if(inCode.equals(vailCode)){
response.sendRedirect("success.jsp");
}
else{
String url = request.getHeader("Referer");
response.sendRedirect(url);
}
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/150383.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...