JavaWeb 使用Session实现一次性验证码

2019-08-29 09:04:02来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

JavaWeb 使用Session实现一次性验证码

 

表单

<form action="loginServlet" method="post">
  请输入验证码:<input type="text" name="code" />
  <img src="getCodeServlet" /><br />
  <button type="submit">提交</button>
</form>

载入页面时,会自动请求getCodeServlet,获取图片(验证码)。

 

 

 

getCodeServlet,产生验证码

 1 @WebServlet("/getCodeServlet")
 2 public class GetCodeServlet extends HttpServlet {
 3     //验证码的宽、高
 4     private static int WIDTH=80;
 5     private static int HEIGHT=25;
 6 
 7     //绘制背景
 8     private void drawBg(Graphics g){
 9         //rgb
10         g.setColor(new Color(128, 128, 128));
11         //绘制矩形。x,y,wigth,height
12         g.fillRect(0,0,WIDTH,HEIGHT);
13         //随机绘制100个干扰点
14         Random random=new Random();
15         for (int i=0;i<100;i++){
16             //产生(0,1)上的小数,*WIDTH|HEIGHT,再取整也行
17             int x=random.nextInt(WIDTH);
18             int y=random.nextInt(HEIGHT);
19             g.drawOval(x,y,1,1);
20 
21             //干扰点的颜色也可以随机,随机产生red,green,blue即可
22             //g.setColor(new Color(red,green,blue));
23         }
24     }
25 
26 
27     //绘制验证码
28     private void drawCode(Graphics g,char[] code){
29         g.setColor(Color.BLACK);
30         //字体、样式(多个时竖线分隔)、字号
31         g.setFont(new Font("serif",Font.ITALIC|Font.BOLD,18));
32         //在不同位置绘制验证码字符,参数:要绘制的String、横、纵坐标。+""是为了char转String。
33         g.drawString(code[0]+"",1,17);
34         g.drawString(code[1]+"",16,15);
35         g.drawString(code[2]+"",31,18);
36         g.drawString(code[3]+"",46,16);
37     }
38 
39     //随机产生4位验证码
40     private char[] getCode(){
41         String chars="0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
42         char[] code=new char[4];
43         Random random=new Random();
44         for (int i=0;i<4;i++){
45             //[0,62)
46             int index= random.nextInt(62);
47             code[i]=chars.charAt(index);
48         }
49         return code;
50     }
51 
52 
53     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
54         HttpSession session = request.getSession();
55         ServletOutputStream sos = response.getOutputStream();
56         response.setContentType("image/jpeg");
57 
58         //设置浏览器不缓存此图片
59         response.setHeader("Pragma","No-cache");
60         response.setHeader("Cache-Control","no-cache");
61         response.setDateHeader("Expires",0);
62 
63         //创建内存图片
64         BufferedImage bufferedImage = new BufferedImage(WIDTH, HEIGHT, TYPE_INT_RGB);
65         Graphics g= bufferedImage.getGraphics();
66         char[] code=getCode();
67         //将验证码放到session域中。session对象要在提交响应之前获得
68         session.setAttribute("code",new String(code));
69         drawBg(g);
70         drawCode(g,code);
71         g.dispose();
72 
73         //将图片输出到浏览器
74         ByteArrayOutputStream baos = new ByteArrayOutputStream();
75         ImageIO.write(bufferedImage,"JPEG",baos);
76         baos.writeTo(sos);
77         baos.close();
78         sos.close();
79     }
80 
81     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
82         doPost(request,response);
83     }
84 }

 

 

 

loginServlet,处理表单

 1 @WebServlet("/loginServlet")
 2 public class LoginServlet extends HttpServlet {
 3     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 4         response.setContentType("text/html;charset=utf-8");
 5         HttpSession session = request.getSession();
 6         String trueCode= (String) session.getAttribute("code");
 7         String code=request.getParameter("code");
 8 
 9         if (code.equals(trueCode)){
10             response.getWriter().write("验证码正确");
11         }
12         else {
13             response.getWriter().write("验证码错误");
14         }
15     }
16 
17     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
18         doPost(request,response);
19     }
20 }

 

上面的处理方式要区分验证码的大小写。

 

不区分大小写:

//先转换为全大写|全小写,再判断
        trueCode=trueCode.toLowerCase();
        code=code.toLowerCase();

        //trueCode=trueCode.toUpperCase();
        //code=trueCode.toUpperCase();

 


原文链接:https://www.cnblogs.com/chy18883701161/p/11423084.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Nacos(八):Nacos持久化

下一篇:解决ajax跨域请求问题