java生成验证码
java实现验证码
1、创建画布实现验证码生成(比较复杂,如何只想使用建议直接看第二点,用hutool实现验证码生成)
成语验证码
前端代码
这里注意onclick="this.src=this.src+'?'表示刷新验证码,问号表示参数,加上问号后才能刷新,表示每次请求的不一样
<div class="layui-form-item input-item" id="imgCode">
<label for="code">验证码</label>
<input type="text" placeholder="请输入验证码" autocomplete="off" name="code" id="code" class="layui-input">
<img src="/login/getCode.action" onclick="this.src=this.src+'?'">
</div>
后端代码
主要定义所有成语数组,和把字符串转出一个一个数组
public class LoadTxt {
//所有成语的数组
public static List<String> cylist=new ArrayList<>();
//将字符串转成数组
public static ArrayList<String> tolist(String str){
ArrayList<String> arrayList=new ArrayList<>();
for (int i=0;i<str.length();i++){
arrayList.add(str.charAt(i)+"");
}
return arrayList;
}
}
主要把文件里面的成语放到上面定义的数组中,这里我放在了监听器中是为了之加载一次,你也可以放在其他地方,通过文件流拿到所有成语,放到cylist数组中。
public void contextInitialized(ServletContextEvent servletContextEvent) {
InputStream is=Thread.currentThread().getContextClassLoader().getResourceAsStream("cy.txt");
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
String cy="";
try {
while((cy=bufferedReader.readLine())!=null){
LoadTxt.cylist.add(cy);
}
} catch (IOException e) {
e.printStackTrace();
}
}
这里是核心代码,如何去处理成语,如何创建一个画布和通过画笔渲染,并且给前台
@RequestMapping("/getCode")
public void getCode(HttpSession session, HttpServletResponse response){
Random random=new Random();
//随机拿到一个成语当作正确验证码
String cy1= LoadTxt.cylist.get(random.nextInt(LoadTxt.cylist.size()));
//随机拿到一个成语取第一个字符当作干扰项
String cy2= LoadTxt.cylist.get(random.nextInt(LoadTxt.cylist.size()));
String cy3=cy1+cy2.charAt(random.nextInt(cy2.length()));
//把拼接的字符串转化成数组
ArrayList<String> arrayList=LoadTxt.tolist(cy3);
//拿到画布
BufferedImage bi = new BufferedImage(120,36,BufferedImage.TYPE_INT_RGB);
//拿到画笔
Graphics graphics=bi.getGraphics();
//设置画笔颜色
graphics.setColor(Color.white);
//将画布都涂白当背景
graphics.fillRect(0,0,120,36);
//这里设置多个画笔颜色,让画出来的成语不是一个颜色
int[] colors={0x112312,0x183632,0x1ff126,0x196326,0x698312};
int count=0;
while(arrayList.size()!=0){
//设置画笔颜色
graphics.setColor(new Color(colors[random.nextInt(colors.length)]));
//设置字体
graphics.setFont(new Font("",Font.BOLD,20));
//从转化的数组中拿一个字符
String s=arrayList.get(random.nextInt(arrayList.size()));
//画到画布上,设置位置
graphics.drawString(s,5+20*count,22);
//移除刚刚取出的字符,防止重复拿到
arrayList.remove(s);
count++;
}
//将正确的成语放到session,方便后期做验证
session.setAttribute("code",cy1);
try {
//将生成的画布发给前台
ServletOutputStream outputStream = response.getOutputStream();
ImageIO.write(bi,"png",outputStream);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
效果图

2、使用hutool生成验证码
导包(楼主用的maven就不提供jar包了)
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.2</version>
</dependency>
前台代码
这里注意onclick="this.src=this.src+'?'表示刷新验证码,问号表示参数,加上问号后才能刷新,表示每次请求的不一样
<div class="layui-form-item input-item" id="imgCode">
<label for="code">验证码</label>
<input type="text" placeholder="请输入验证码" autocomplete="off" name="code" id="code" class="layui-input">
<img src="/login/getCode.action" onclick="this.src=this.src+'?'">
</div>
后台代码
//HuTool定义图形验证码的长和宽,验证码的位数,干扰线的条数
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(90, 36,4,6);
//将验证码放入session
session.setAttribute("code",lineCaptcha.getCode());
try {
//返回前台
ServletOutputStream outputStream = response.getOutputStream();
lineCaptcha.write(outputStream);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
效果图


浙公网安备 33010602011771号