Java 登陆验证码
2017-02-22 13:18
281 查看
生成验证的类:RandomCode
import Java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@SuppressWarnings("serial")
public class RandomCode extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form. has its tag value method equals to
* get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form. has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Random random = new Random();
int width = 60, height = 20;
// 创建BufferedImage对象,设置图片的长度宽度和色彩。
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
OutputStream os = response.getOutputStream();
// 取得Graphics对象,用来绘制图片
Graphics g = image.getGraphics();
// 绘制图片背景和文字,释放Graphics对象所占用的资源。
g.setColor(getRandColor(200, 250));
// 设置内容生成的位置
g.fillRect(0, 0, width, height);
// 设置内容的字体和大小
g.setFont(new Font("Times New Roman", Font.PLAIN, 20));
// 设置内容的颜色:主要为生成图片背景的线条
g.setColor(getRandColor(160, 200));
// 画边框。
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1);
// 随机产生160条干扰线,使图象中的认证码不易被其它程序探测到。
g.setColor(Color.BLACK);
// 图片背景上随机生成155条线条,避免通过图片识别破解验证码
for (int i = 0; i < 30; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
// 生成四位的随机数,生成一个数,写一个
String[] s = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
"X", "Y", "Z" };
String content = "";
for (int i = 0; i < 4; i++) {
String rand = "";
if (random.nextBoolean()) {
rand = String.valueOf(random.nextInt(10));
} else {
int index = random.nextInt(25);
rand = s[index];
}
content += rand;
g.setColor(new Color(20 + random.nextInt(10), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g.drawString(rand, 13 * i + 6, 16);
}
// 释放此图形的上下文以及它使用的所有系统资源,类似于关闭流
g.dispose();
// 将生成的验证码值(即运算结果的值)放到session中,以便于后台做验证。
HttpSession session = request.getSession();
session.setAttribute("result", content);
// 通过ImageIO对象的write静态方法将图片输出。
ImageIO.write(image, "JPEG", os);
os.close();
}
/**
* 生成随机颜色
*
* @param fc
* @param bc
* @return
*/
public Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
}
web.xml配置:
<!-- 验证码 -->
<servlet>
<description>生成验证码的Servlet</description>
<display-name>RandomCode</display-name>
<servlet-name>RandomCode</servlet-name>
<servlet-class>com.gxh.tools.RandomCode</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RandomCode</servlet-name>
<url-pattern>*.RandomCode</url-pattern>
</servlet-mapping>
login.jsp页面:
<script type="text/JavaScript">
function changeImg()
{ document.getElementById("validatecodeimg").src=Math.round(Math.random()*10000)+".RandomCode ";
}
</script>
<body>
<div align="center">
<img id="validatecodeimg" alt="看不清?点击刷新" onclick="javascript:changeImg()" src="first.RandomCode " align="top" />
<a href="javascript:changeImg()">看不清?点击刷新</a>
</div>
</body>
</html>
import Java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@SuppressWarnings("serial")
public class RandomCode extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form. has its tag value method equals to
* get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form. has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Random random = new Random();
int width = 60, height = 20;
// 创建BufferedImage对象,设置图片的长度宽度和色彩。
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
OutputStream os = response.getOutputStream();
// 取得Graphics对象,用来绘制图片
Graphics g = image.getGraphics();
// 绘制图片背景和文字,释放Graphics对象所占用的资源。
g.setColor(getRandColor(200, 250));
// 设置内容生成的位置
g.fillRect(0, 0, width, height);
// 设置内容的字体和大小
g.setFont(new Font("Times New Roman", Font.PLAIN, 20));
// 设置内容的颜色:主要为生成图片背景的线条
g.setColor(getRandColor(160, 200));
// 画边框。
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1);
// 随机产生160条干扰线,使图象中的认证码不易被其它程序探测到。
g.setColor(Color.BLACK);
// 图片背景上随机生成155条线条,避免通过图片识别破解验证码
for (int i = 0; i < 30; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
// 生成四位的随机数,生成一个数,写一个
String[] s = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
"X", "Y", "Z" };
String content = "";
for (int i = 0; i < 4; i++) {
String rand = "";
if (random.nextBoolean()) {
rand = String.valueOf(random.nextInt(10));
} else {
int index = random.nextInt(25);
rand = s[index];
}
content += rand;
g.setColor(new Color(20 + random.nextInt(10), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g.drawString(rand, 13 * i + 6, 16);
}
// 释放此图形的上下文以及它使用的所有系统资源,类似于关闭流
g.dispose();
// 将生成的验证码值(即运算结果的值)放到session中,以便于后台做验证。
HttpSession session = request.getSession();
session.setAttribute("result", content);
// 通过ImageIO对象的write静态方法将图片输出。
ImageIO.write(image, "JPEG", os);
os.close();
}
/**
* 生成随机颜色
*
* @param fc
* @param bc
* @return
*/
public Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
}
web.xml配置:
<!-- 验证码 -->
<servlet>
<description>生成验证码的Servlet</description>
<display-name>RandomCode</display-name>
<servlet-name>RandomCode</servlet-name>
<servlet-class>com.gxh.tools.RandomCode</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RandomCode</servlet-name>
<url-pattern>*.RandomCode</url-pattern>
</servlet-mapping>
login.jsp页面:
<script type="text/JavaScript">
function changeImg()
{ document.getElementById("validatecodeimg").src=Math.round(Math.random()*10000)+".RandomCode ";
}
</script>
<body>
<div align="center">
<img id="validatecodeimg" alt="看不清?点击刷新" onclick="javascript:changeImg()" src="first.RandomCode " align="top" />
<a href="javascript:changeImg()">看不清?点击刷新</a>
</div>
</body>
</html>
相关文章推荐
- java抓取带验证码登陆后的页面
- java web 程序---登陆的验证码实现显示
- Extjs 带验证码的登陆窗口,java版
- java登陆验证码与JS无刷新验证
- JavaWeb登陆验证码
- Java Web开发过程中登陆模块的验证码的实现方式总结
- 【Android】java生成炫酷验证码,不区分大小写。登陆,发送手机验证码,防止注册机,android开发
- java中在Ext环境中实现带验证码的登陆窗口
- Java Web开发过程中登陆模块的验证码的实现方式总结
- Java绘制登陆验证码
- java 如何实现登陆和注册的验证码功能
- Java绘制登陆验证码
- Java 登陆验证码
- Java登陆带验证码的网站
- [置顶] 豆瓣网络爬虫-java网络爬虫[验证码模拟登陆]详细介绍
- [置顶] 【Android】java生成炫酷验证码,不区分大小写。登陆,发送手机验证码,防止注册机,android开发
- 验证码的java通用类
- java 实现web 登陆
- java生成数字验证码