1、新建两个工具类
import java.util.Arrays;
public class CreateVerificationCode {
/**
* 验证码难度级别
*/
public enum SecurityCodeLevel {
Simple,
Medium,
Hard
}
public static String getSecurityCode() {
return (String) getSecurityCode(4, SecurityCodeLevel.Medium, false);
}
public static String getSecurityCode(int length, SecurityCodeLevel level, boolean isCanRepeat) {
int len = length;
//除去容易混淆的0和o,1和l
char[] codes = {
'1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'm', 'n', 'p', 'q', 'r', 's', 't','u',
'v', 'w', 'x', 'y', 'z','A','B','C','D','E',
'F','G','H','J','K','L','M','N','P','Q','R','S',
'T','U','V','W','X','Y','Z'};
if(level==SecurityCodeLevel.Simple){
codes= Arrays.copyOfRange(codes,0,9);
}else if (level==SecurityCodeLevel.Medium){
codes= Arrays.copyOfRange(codes,0,33);
}
int n=codes.length;
//抛出运行时异常
if (len>n&&isCanRepeat==false){
throw new RuntimeException(
String.format("调用securitycode.getSecurityCode(%1$s,len,level,isCanRepeat,n)"));}
char[] result=new char[len];
//判断能否出现重复的字符
if (isCanRepeat){
for(int i=0;i<result.length;i++){
//索引0 and n-1
int r=(int)(Math.random()*n);
//将result中的第i个元素设为codes[r]存放的数值
result[i]=codes[r];
}
}else {
for (int i=0;i<result.length;i++){
int r=(int)(Math.random()*n);
//将result中的第i个元素设为codes[r]存放的数值
result[i]=codes[r];
codes[r]=codes[n-1];
n--;
}
}
return String.valueOf(result);
}
}
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
/**
* 可生成数字,大写,小写字母及三者混合类型的验证码,支持自定义干扰线,图文颜色
*/
public class CreateVerificationCodeImage {
private String securityCode;
public CreateVerificationCodeImage(String securityCode){
this.securityCode=securityCode;
}
//高度
private static final int CAPTCHA_HEIGHT = 35;
//宽度
private static final int CAPTCHA_WIDTH = 100;
//数字的长度
//private static final int NUMBER_CNT = 6;
private Random r = new Random();
// 字体
private String[] fontNames = { "宋体", "华文楷体", "黑体", "华文新魏", "华文隶书", "微软雅黑", "楷体_GB2312" };
//private String[] fontNames = { "宋体", "黑体", "微软雅黑"};
/**
* 机能概要:生成随机的颜色
* @return
*/
private Color randomColor() {
int red = r.nextInt(150);
int green = r.nextInt(150);
int blue = r.nextInt(150);
return new Color(red, green, blue);
}
/**
* 机能概要:生成随机的字体
* @return
*/
private Font randomFont() {
int index = r.nextInt(fontNames.length);
String fontName = fontNames[index];// 生成随机的字体名称
int style = r.nextInt(4);// 生成随机的样式, 0(无样式), 1(粗体), 2(斜体), 3(粗体+斜体)
int size = r.nextInt(5) + 24; // 生成随机字号, 24 ~ 28
// int size = r.nextInt(5) + 15; // 生成随机字号, 20 ~ 24
return new Font(fontName, style, size);
}
// 画干扰线
private void drawLine(BufferedImage image) {
int num = 5;// 一共画5条
Graphics2D g2 = (Graphics2D) image.getGraphics();
for (int i = 0; i < num; i++) {// 生成两个点的坐标,即4个值
int x1 = r.nextInt(CAPTCHA_WIDTH);
int y1 = r.nextInt(CAPTCHA_HEIGHT);
int x2 = r.nextInt(CAPTCHA_WIDTH);
int y2 = r.nextInt(CAPTCHA_HEIGHT);
g2.setStroke(new BasicStroke(1.5F));
g2.setColor(randomColor()); // 随机生成干扰线颜色
g2.drawLine(x1, y1, x2, y2);// 画线
}
}
// 创建BufferedImage,生成图片
public BufferedImage createImage() {
BufferedImage image = new BufferedImage(CAPTCHA_WIDTH, CAPTCHA_HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) image.getGraphics();
// 背景色,白色
g2.setColor(new Color(255, 255, 255));
g2.fillRect(0, 0, CAPTCHA_WIDTH, CAPTCHA_HEIGHT);
// 向图片中画4个字符 String securityCode
for (int i = 0; i < securityCode.length(); i++) {// 循环四次,每次生成一个字符
String s = securityCode.charAt(i) + "";// 随机生成一个字母
// float x = i * 1.0F * CAPTCHA_WIDTH / NUMBER_CNT; // 设置当前字符的x轴坐标
float x = i * 1.0F * CAPTCHA_WIDTH / 4+7F; // 设置当前字符的x轴坐标
g2.setFont(randomFont()); // 设置随机字体
g2.setColor(randomColor()); // 设置随机颜色
g2.drawString(s, x, CAPTCHA_HEIGHT-7); // 画图,依次将字符写入到图片的相应位置-------------------
}
drawLine(image); // 添加干扰线
return image;
}
}
2、前端显示验证码及js变换验证码
<input name="vericode" placeholder="验证码" style="height: 35px" >
<img style="position: relative;top: 11px;" id="vericodeImg" src="imageCode" class="captcha-image" onclick="changeImg()">
<script>
function changeImg() {
//需要让每次请求的url都发生变化。否则服务器会认为访问的时一张图片,就不会刷新请求了
//每次url一样,服务器会认为访问的url是同一张图片,没变化啊
$("#vericodeImg").attr("src","imageCode?"+Math.random())
}
</script>
3、servlet设置返回内容及清楚缓存
import com.xszx.util.CreateVerificationCode;
import com.xszx.util.CreateVerificationCodeImage;
import javax.imageio.ImageIO;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/imageCode")
public class ImageCodeServelt extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String vericode= CreateVerificationCode.getSecurityCode();
HttpSession session=req.getSession();
session.setAttribute("verityCode",vericode);
//设置返回的内容
resp.setContentType("img/jpeg");
//浏览器不缓存响应内容--验证码图片,点一次就要刷新一次,所以不能有缓存出现
resp.setHeader("Pragma","No-cache");
resp.setHeader("Cache-Control","no-cache");
//设置验证码失效时间
resp.setDateHeader("Expires",0);
//以字节流发过去,交给img的src属性去解析即可
ImageIO.write(new CreateVerificationCodeImage(vericode).createImage(),"JPEG",resp.getOutputStream());
}
}
4、登录servlet判断前端返回值
String vericode=req.getParameter("vericode");
String generatedCode= (String) req.getSession().getAttribute("verityCode");
5、实现效果
标签:String,登录,nextInt,int,验证码,CAPTCHA,import,g2,JavaWeb From: https://blog.csdn.net/weixin_68489989/article/details/141166425