kaptcha 验证码在spring mvc 中的使用
来源:互联网 发布:js特效代码大全 编辑:程序博客网 时间:2024/05/16 13:00
kaptcha的配置是通过web.xml完成的,所有的参数都有默认值。
所需要的jar包:kaptcha-2.0.jar
一、Kaptcha输出验证码的Demo:
二、配置参数(web.xml)
- <servlet>
- <servlet-name>Kaptcha</servlet-name>
- <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
- <init-param>
- <param-name>kaptcha.border</param-name>
- <param-value>yes</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>Kaptcha</servlet-name>
- <url-pattern>*.code</url-pattern>
- </servlet-mapping>
三、在页面上的调用
- <form action="submit.action">
- <img src="validate.code" /> <input type="text" name="validateCode" value="" />
- </form>
四、验证码的验证
- HttpSession session = request.getSession();
- String makeValidateCode = (String) session
- .getAttribute(Constants.KAPTCHA_SESSION_KEY);
- if (validateCode != null && makeValidateCode != null) {
- return makeValidateCode.equalsIgnoreCase(validateCode);
- } else {
- return false;
- }
五、实现刷新图片
可以在请求URL后添加随机参数,避免浏览器的缓存。
- <img src="validate.code" id="kaptchaImage" />
- <script type="text/javascript">
- $(function(){
- $('#kaptchaImage').click(function () {
- $(this).attr('src', 'validate.code?' + Math.floor(Math.random()*100) ); })
- });
- </script>
- <br /><small>Can't read the image? Click it to get a new one.</small>
六、与spring MVC 的结合
在applicationContext.xml中添加
- <bean id="captchaProducer"
- class="com.google.code.kaptcha.impl.DefaultKaptcha">
- <property name="config">
- <bean class="com.google.code.kaptcha.util.Config">
- <constructor-arg type="java.util.Properties">
- <value>kaptcha.border=yes</value>
- </constructor-arg>
- </bean>
- </property>
- </bean>
源码:
- import java.awt.image.BufferedImage;
- import javax.imageio.ImageIO;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.servlet.ModelAndView;
- import com.google.code.kaptcha.Constants;
- import com.google.code.kaptcha.Producer;
- @Controller
- public class CaptchaImageCreateController {
- private Producer captchaProducer = null;
- @Autowired
- public void setCaptchaProducer(Producer captchaProducer) {
- this.captchaProducer = captchaProducer;
- }
- @RequestMapping("/captcha-image.html")
- public ModelAndView handleRequest(
- HttpServletRequest request,
- HttpServletResponse response) throws Exception {
- // Set to expire far in the past.
- response.setDateHeader("Expires", 0);
- // Set standard HTTP/1.1 no-cache headers.
- response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
- // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
- response.addHeader("Cache-Control", "post-check=0, pre-check=0");
- // Set standard HTTP/1.0 no-cache header.
- response.setHeader("Pragma", "no-cache");
- // return a jpeg
- response.setContentType("image/jpeg");
- // create the text for the image
- String capText = captchaProducer.createText();
- // store the text in the session
- request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
- // create the image with the text
- BufferedImage bi = captchaProducer.createImage(capText);
- ServletOutputStream out = response.getOutputStream();
- // write the data out
- ImageIO.write(bi, "jpg", out);
- try
- {
- out.flush();
- }
- finally
- {
- out.close();
- }
- return null;
- }
- }
ConstantDescriptionDefaultkaptcha.borderBorder around kaptcha. Legal values are yes or no.yeskaptcha.border.colorColor of the border. Legal values are r,g,b (and optional alpha) or white,black,blue.blackkaptcha.border.thicknessThickness of the border around kaptcha. Legal values are > 0.1kaptcha.image.widthWidth in pixels of the kaptcha image.200kaptcha.image.heightHeight in pixels of the kaptcha image.50kaptcha.producer.implThe image producer.com.google.code.kaptcha.impl.DefaultKaptchakaptcha.textproducer.implThe text producer.com.google.code.kaptcha.text.impl.DefaultTextCreatorkaptcha.textproducer.char.stringThe characters that will create the kaptcha.abcde2345678gfynmnpwxkaptcha.textproducer.char.lengthThe number of characters to display.5kaptcha.textproducer.font.namesA list of comma separated font names.Arial, Courierkaptcha.textproducer.font.sizeThe size of the font to use.40px.kaptcha.textproducer.font.colorThe color to use for the font. Legal values are r,g,b.blackkaptcha.noise.implThe noise producer.com.google.code.kaptcha.impl.DefaultNoisekaptcha.noise.colorThe noise color. Legal values are r,g,b.blackkaptcha.obscurificator.implThe obscurificator implementation.com.google.code.kaptcha.impl.WaterRipplekaptcha.background.implThe background implementation.com.google.code.kaptcha.impl.DefaultBackgroundkaptcha.background.clear.fromStarting background color. Legal values are r,g,b.light greykaptcha.background.clear.toEnding background color. Legal values are r,g,b.whitekaptcha.word.implThe word renderer implementation.com.google.code.kaptcha.text.impl.DefaultWordRendererkaptcha.session.keyThe value for the kaptcha is generated and is put into the HttpSession. This is the key value for that item in the session.KAPTCHA_SESSION_KEYkaptcha.session.dateThe date the kaptcha is generated is put into the HttpSession. This is the key value for that item in the session.KAPTCHA_SESSION_DATE
- kaptcha 验证码在spring mvc 中的使用
- kaptcha 验证码在spring mvc 中的使用
- kaptcha验证码在spring mvc中的使用
- kaptcha 验证码在spring mvc 中的使用
- spring mvc 使用kaptcha生成验证码
- spring mvc 使用kaptcha生成验证码
- Spring mvc 中使用 kaptcha 验证码
- Spring mvc 中使用 kaptcha 验证码
- spring mvc 使用kaptcha生成验证码
- spring mvc 使用kaptcha生成验证码
- spring mvc 使用kaptcha生成验证码
- spring mvc 使用kaptcha生成验证码
- Spring mvc 中使用 kaptcha 验证码
- spring mvc 使用kaptcha生成验证码
- spring mvc 使用kaptcha生成验证码
- spring mvc 使用kaptcha生成验证码
- spring mvc 使用kaptcha生成验证码
- Spring mvc 中使用 kaptcha 验证码
- android界面组件之进度条--ProcessBar
- struts2用DomainModel接收参数
- Web开发者不容错过的20段CSS代码
- OpenCV函数cvDistTransform-距离变换
- 关于java的HMI学习
- kaptcha 验证码在spring mvc 中的使用
- 搜索
- JAVA中synchronized与static synchronized 的区别
- LTRIM, RTRIM, and TRIM
- VMWare Workstation 7 自动化命令
- Hadoop JobControl Job迭代
- IIS总线
- A01_为Button添加监听器
- Android - ANR详解