验证码制作
制作验证码接口
@Controller
@RequestMapping("/captcha")
public class CaptchaController {
private int width=120; //图片宽度
private int height=30; //图片高度
private int drawY=18; //图片内容在图片的起始位置
private int charcount=6;//验证码位数
private String chars[]={"0","1","2","3","4","5","6","7","8","9",
"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",
"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"};
private int space=15;//文字间隔
@RequestMapping("/code")
public void makeCaptchaCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);//创建BufferedImage对象,其作用相当于一图片
Graphics g=image.getGraphics(); //创建Graphics对象,其作用相当于画笔
g.setColor(Color.white); //设置画笔颜色
g.fillRect(0,0,width,height); //矩形大小
Font mfont=new Font("楷体",Font.BOLD,16); //定义字体样式
g.setColor(Color.black);
StringBuffer stringBuffer = new StringBuffer();//记录验证码文字
//文字
int ran=0;
int len=chars.length;
for (int i=0;i<charcount;i++){
ran= new Random().nextInt(len);
g.setColor(getRandColor());
g.drawString(chars[ran],(i+1)*space,drawY);
stringBuffer.append(chars[ran]);
}
//干扰线
for(int i=1;i<4;i++){
g.setColor(getRandColor());
int[] ints = makeLineDot();
g.drawLine(ints[0],ints[1],ints[2],ints[3]);
}
//设置不缓存图片
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "No-cache");
response.setDateHeader("Expires", 0);
//将验证码存入session中
request.getSession().setAttribute("code",stringBuffer.toString());
response.setContentType("image/png");
ServletOutputStream out = response.getOutputStream();
ImageIO.write(image,"png",out);
out.flush();
out.close();
}
/*该方法主要作用是获得随机生成的颜色*/
public Color getRandColor(){
Random random=new Random ();
int r=random.nextInt(255); //随机生成RGB颜色中的r值
int g=random.nextInt(255); //随机生成RGB颜色中的g值
int b=random.nextInt(255); //随机生成RGB颜色中的b值
return new Color(r,g,b);
}
//干扰线数组
public int[] makeLineDot(){
Random random=new Random ();
int x1= random.nextInt(width/2);
int y1= random.nextInt(height);
int x2= random.nextInt(width);
int y2= random.nextInt(height);
return new int[]{x1,y1,x2,x2};
}
}
登录页面
<!DOCTYPE html>
<html lang="en" >
<script src="webjars/jquery/3.5.1/jquery.min.js"></script>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<div style="text-align: center">
登录名:<input id="name" /> <br/>
密码:<input type="password" id="passwd"/><br/>
验证码:<input id="yzm"/><br/>
<img id="imagecode" src="/captcha/code">
<a href="javascript:void(0)" onclick="changeCode()" >看不清?换一个</a><br/>
<input type="checkbox" id="jzw"> 记住我<br/>
<input type="button" value="登录" id="login"/>
</div>
</body>
<script type="application/javascript">
$("#login").click(function () {
var name=$("#name").val();
var passwd=$("#passwd").val();
var yzm=$("#yzm").val();
var jzw=$("#jzw").val();
$.ajax({
url:"/login",
type:"POST",
dataType:"json",
data:{
name:name,
passwd:passwd,
yzm:yzm,
jzw:jzw
},
success: function (resq) {
if(resq.success){
window.location.href="/index";
}
}
})
})
//重新获取验证码
function changeCode() {
//new Date()的目的是浏览器不使用缓存,每次获取新的验证码
var url="/captcha/code?t="+new Date();
$("#imagecode").attr("src",url);
}
</script>
</html>
效果展示