Cage验证码生成器快速上手_cage码-程序员宅基地

技术标签: c  java  线程  多线程  360  

Cage小巧,好用。这里在官方基础例子基础上做了扩展:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package  com.lavasoft.ntv.web.common;
import  com.github.cage.IGenerator;
import  java.util.Random;
/**
  * 验证码生成器
  *
  * @author leizhimin 14-5-5 下午2:42
  */
public  class  MyTokenGenerator  implements  IGenerator<String> {
     private  int  length =  4 ;
     private  String charsetdir =  "23456789abcdefghigkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ" ;
     private  static  final  Random r =  new  Random();
     public  MyTokenGenerator() {
     }
     public  MyTokenGenerator( int  length, String charsetdir) {
         this .length = length;
         this .charsetdir = charsetdir;
     }
     @Override
     public  String next() {
         StringBuffer sb =  new  StringBuffer();
         int  len = charsetdir.length();
         for  ( int  i =  0 ; i < length; i++) {
             sb.append(charsetdir.charAt(r.nextInt(len -  1 )));
         }
         return  sb.toString();
     }
     public  static  void  main(String[] args) {
         MyTokenGenerator t =  new  MyTokenGenerator();
         for  ( int  i =  0 ; i <  100 ; i++) {
             System.out.println(t.next());
         }
     }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package  com.lavasoft.ntv.web.servlet;
import  com.github.cage.Cage;
import  com.lavasoft.ntv.web.common.MyTokenGenerator;
import  javax.servlet.ServletException;
import  javax.servlet.http.HttpServlet;
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;
import  javax.servlet.http.HttpSession;
import  java.io.IOException;
public  class  CaptchaServlet  extends  HttpServlet {
     private  static  final  long  serialVersionUID = 1490947492185481844L;
     private  static  final  Cage cage =  new  Cage( null , null , null , null , null new  MyTokenGenerator(), null );
     /**
      * Generates a captcha token and stores it in the session.
      *
      * @param session where to store the captcha.
      */
     public  static  void  generateToken(HttpSession session) {
         String token = cage.getTokenGenerator().next();
         session.setAttribute( "captchaToken" , token);
         markTokenUsed(session,  false );
     }
     /**
      * Used to retrieve previously stored captcha token from session.
      *
      * @param session where the token is possibly stored.
      * @return token or null if there was none
      */
     public  static  String getToken(HttpSession session) {
         Object val = session.getAttribute( "captchaToken" );
         return  val !=  null  ? val.toString() :  null ;
     }
     /**
      * Marks token as used/unused for image generation.
      *
      * @param session where the token usage flag is possibly stored.
      * @param used    false if the token is not yet used for image generation
      */
     protected  static  void  markTokenUsed(HttpSession session,  boolean  used) {
         session.setAttribute( "captchaTokenUsed" , used);
     }
     /**
      * Checks if the token was used/unused for image generation.
      *
      * @param session where the token usage flag is possibly stored.
      * @return true if the token was marked as unused in the session
      */
     protected  static  boolean  isTokenUsed(HttpSession session) {
         return  !Boolean.FALSE.equals(session.getAttribute( "captchaTokenUsed" ));
     }
     @Override
     protected  void  doGet(HttpServletRequest req, HttpServletResponse resp)
             throws  ServletException, IOException {
         HttpSession session = req.getSession( false );
         String token = session !=  null  ? getToken(session) :  null ;
         if  (token ==  null  || isTokenUsed(session)) {
             resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Captcha not found." );
             return ;
         }
         setResponseHeaders(resp);
         markTokenUsed(session,  true );
         cage.draw(token, resp.getOutputStream());
     }
     /**
      * Helper method, disables HTTP caching.
      *
      * @param resp response object to be modified
      */
     protected  void  setResponseHeaders(HttpServletResponse resp) {
         resp.setContentType( "image/"  + cage.getFormat());
         resp.setHeader( "Cache-Control" "no-cache, no-store" );
         resp.setHeader( "Pragma" "no-cache" );
         long  time = System.currentTimeMillis();
         resp.setDateHeader( "Last-Modified" , time);
         resp.setDateHeader( "Date" , time);
         resp.setDateHeader( "Expires" , time);
     }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<%--
   Created by IntelliJ IDEA.
   User: leizhimin 14-5-5 下午12:18
--%>
<%@page import="com.lavasoft.ntv.web.servlet.CaptchaServlet"%><%@
         page contentType="text/html" pageEncoding="UTF-8"%><%
     boolean showGoodResult;
     boolean showBadResult;
     if ("POST".equals(request.getMethod())) {
         String sessionToken = CaptchaServlet.getToken(session);
         String requestToken = request.getParameter("captcha");
         showGoodResult = sessionToken != null && sessionToken.equals(requestToken);
         showBadResult = !showGoodResult;
     } else {
         showGoodResult = showBadResult = false;
     }
     CaptchaServlet.generateToken(session);
%><!DOCTYPE html>
< html  xmlns = "http://www.w3.org/1999/xhtml" >
< head >
     < meta  charset = "UTF-8"  />
     < title >Captcha Reader</ title >
</ head >
< body >
<%  if (showGoodResult) {%>
< h1  style = "color: green;" >Your kung fu is good!</ h1 >
<%  } else if (showBadResult) {%>
< h1  style = "color: red;" >This is not right. Try again!</ h1 >
<%  } %>
< p >Type in the word seen on the picture</ p >
< form  action = ""  method = "post" >
     < input  name = "captcha"  type = "text"  autocomplete = "off"  />
     < input  type = "submit"  />
</ form >
< img  alt = "captcha image"  src = "/ntv/captcha"  width = "120px"  height = "30px" />
</ body >
</ html >


1
2
3
4
5
6
7
8
< servlet >
     < servlet-name >captcha</ servlet-name >
     < servlet-class >com.lavasoft.ntv.web.servlet.CaptchaServlet</ servlet-class >
</ servlet >
< servlet-mapping >
     < servlet-name >captcha</ servlet-name >
     < url-pattern >/captcha</ url-pattern >
</ servlet-mapping >


访问页面:


wKioL1NnS2_T5q1RAADnNYpJ3h4765.jpg

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/cdeyzx002/article/details/26447671

智能推荐

第十五章:AI大模型在自然语言理解和知识图谱中的应用-程序员宅基地

文章浏览阅读905次,点赞16次,收藏21次。1.背景介绍自然语言处理(NLP)是一门研究如何让计算机理解和生成人类自然语言的学科。知识图谱(Knowledge Graph)是一种结构化的数据库,用于存储实体(如人、地点、事件等)和关系(如属性、联系、事件等)之间的信息。随着AI技术的发展,NLP和知识图谱在各种应用中发挥着越来越重要的作用。本章将探讨AI大模型在自然语言理解和知识图谱中的应用。1.1 自然语言理解的重要性自然语言...

arcgis注册数据源_ArcGIS注册数据库问题分析-程序员宅基地

文章浏览阅读333次。本文是'猴妹'师妹授权给我来发表的,介绍都是师妹的研究成果,在此,非常感谢'猴妹'师妹。用ArcGIS Server在发布地图服务时,注册数据库是很常见的,几年前就开始注册数据库,直到昨天,才有点顿悟整个注册的流程,记录下来。注册数据库的目的地图数据的数据源有多种,可以是本地文件(比如shp),可以是GeoDatabase,也可以是各种数据库(Oracle/DB2/SQL Server等)。注册数..._arcgisserver10.5 data source must be registered

51nod 1488 帕斯卡小三角_51nod1488-程序员宅基地

文章浏览阅读481次。斜率优化+单调栈+二分_51nod1488

【x264编码器】章节3——x264的码率控制-程序员宅基地

文章浏览阅读3.8k次,点赞22次,收藏21次。4.X264_AQ_AUTOVARIANCE_BIASED:会考虑所有宏块的qp调整的均值,公式=f_aq_strength * avg_adj * (qp_adj -(avg_adj - 0.5f * (avg_adj_pow2 - 14.f) / avg_adj)) + f_aq_strength * (1.f - 14.f / (qp_adj * qp_adj))B帧的QP不同于I/P帧的QP计算,而是由前后参考帧的QP经过偏移得到,详细过程可以查看rate_estimate_qscale;

基于JAX-WS的Web Service服务端/客户端 ;JAX-WS + Spring 开发webservice-程序员宅基地

文章浏览阅读213次。一、基于JAX-WS的Web Service服务端/客户端下面描述的是在main函数中使用JAX-WS的Web Service的方法,不是在web工程里访问,在web工程里访问,参加第二节。JAX-WS简介:JAX_RPC(Java API for XML-Based RPC)允许Java应用程序可以通过已知的描述信息调用一个基于Java的Web服务,描述信息与Web服务的W..._基于sum-jaxwx开发的webservice获取客户端ip的方法

[论文翻译] Machine learning: Trends, perspectives, and prospects_机器学习论文翻译3000字以上-程序员宅基地

文章浏览阅读965次。[论文题目]:Machine learning: Trends, perspectives, and prospects[论文来源]:Machine learning: Trends, perspectives, and prospects[翻译人]:BDML@CQUT实验室Machine learning:Trends,perspectives, and prospects机器学习:趋势、观点和前景M. I. Jordan1* and T. M. Mitchell2*AbstractMach_机器学习论文翻译3000字以上

随便推点

怎么用真机测试android,andriod studio如何使用真机测试 andriod studio真机测试教程-程序员宅基地

文章浏览阅读490次。在使用android studio进行项目的调试的时候,大家可能会觉得模拟器有一点的慢,或者卡,很多小伙伴可能更想使用真机进行调试,那么下面来看看andriod studio如何使用真机测试。android studio设置:1、进入Android Studio界面,找到界面左侧工具栏的app2、鼠标点击app,在弹出的列表中选择Edit Configurations这一项3、在Run/Debug..._android 真机

基于三代测序技术的微生物组学研究进展 _sanger et al.,1978-程序员宅基地

文章浏览阅读2.5k次,点赞3次,收藏9次。基于三代测序技术的微生物组学研究进展2020-09-04 09:16微生物通常指一切难以用肉眼观察到的微小生物, 包括细菌、病毒、古菌、真菌以及一些微小的原生生物。微生物体积微小、结构简单, 却又无处不在, 在人类健康(Yang & Tarng, 2018)、工农业生产(Huang et al, 2018)、环境保护(Liu et al, 2018)和食品安全(Alori & Babalola, 2018)等方面都发挥着重要的作用。微生物传统的研究方法以分离纯菌培养为主, 但由于培._sanger et al.,1978

【Android】Android布局中实现圆角边框_<corners android:radius="5sp" />-程序员宅基地

文章浏览阅读384次。设置corners_bg.xml设置边框圆角可以在drawable-mdpi目录里定义一个xml: 1xml version="1.0"encoding="utf-8"?> 2shape="http://schemas.android.com/apk/res/andro_

go语言加载windows dll(cuda dll)踩坑记_go语言导入syscall包失败-程序员宅基地

文章浏览阅读562次。今天又把windows坑踩了个遍一、重要声明!!!windows上貌似不支持go加载windows静态库,还是乖乖使用动态库吧二、 DLL生成1、首先安装vs,我是用的是vs2017,至于使用什么版本的vs,请查看https://docs.nvidia.com/cuda/cuda-installation-guide-microsoft-windows/index.html2、然后安装..._go语言导入syscall包失败

JavaSE笔记(2.1)Java基础-Java异常分析及处理_catch(filenotfoundexception)捕获异常,但控制台报错-程序员宅基地

文章浏览阅读117次。前言编程过程种会碰到各种bug,出现bug时会调用异常分析和处理方法显示在控制台或日志异常如果某个方法不能按照正常的途径完成任务,就可以通过另一种路径退出方法。在这种情况下会抛出一个封装了错误信息的对象。此时,这个方法会立刻退出同时不返回任何值。另外,调用这个方法的其他代码也无法继续执行,异常处理机制会将代码执行交给异常处理器。(copy的图,出处看标记)Throwable 是 J..._catch(filenotfoundexception)捕获异常,但控制台报错

量子计算与量子信息处理:未来的信息处理技术-程序员宅基地

文章浏览阅读521次,点赞12次,收藏5次。1.背景介绍量子计算和量子信息处理是现代科学和技术领域中的一个热门话题。它们涉及到量子力学的基本原理和应用,具有广泛的前景和潜力。在这篇文章中,我们将深入探讨量子计算和量子信息处理的基本概念、算法原理、实例代码和未来发展趋势。1.1 量子计算与量子信息处理的诞生量子计算和量子信息处理的诞生可以追溯到20 世纪60年代和70年代,当时的科学家们开始研究量子力学在计算和信息处理领域的应用。...

推荐文章

热门文章

相关标签