浏览 5657 次
锁定老帖子 主题:jcaptcha 之 声音验证码
精华帖 (1) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2012-01-07
最后修改:2012-01-07
还有图片验证码 http://www.iteye.com/topic/1119604 /* * JCaptcha, the open source java framework for captcha definition and integration * Copyright (c) 2007 jcaptcha.net. All Rights Reserved. * See the LICENSE.txt file distributed with this package. */ package captcha.servlet; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import captcha.CaptchaServiceSingleton; import com.octo.captcha.service.CaptchaServiceException; import com.octo.captcha.service.sound.SoundCaptchaService; public class SoundCaptchaServlet extends HttpServlet implements Servlet { /** * simple-servlet-image-sample * @Title: SoundCaptchaServlet.java * @Package captcha.servlet * @Description: TODO * @author liazhou * @date 2012-1-7 下午05:11:14 * @version V1.0 */ private static final long serialVersionUID = 3440317252067122452L; public static SoundCaptchaService service = CaptchaServiceSingleton.getSoundInstance(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doPost(request, response); } @Override protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { // Set to expire far in the past. httpServletResponse.setDateHeader("Expires", 0); // Set standard HTTP/1.1 no-cache headers. httpServletResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); // Set IE extended HTTP/1.1 no-cache headers (use addHeader). httpServletResponse.addHeader("Cache-Control", "post-check=0, pre-check=0"); // Set standard HTTP/1.0 no-cache header. httpServletResponse.setHeader("Pragma", "no-cache"); // return a wav httpServletResponse.setContentType("audio/wav"); AudioInputStream audioInputStream = service.getSoundChallengeForID(httpServletRequest. getSession(true).getId()); ServletOutputStream out = httpServletResponse.getOutputStream(); // write the data out ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); AudioSystem .write(audioInputStream, javax.sound.sampled.AudioFileFormat.Type.WAVE, byteOutputStream); out.write(byteOutputStream.toByteArray()); try { out.flush(); } finally { out.close(); } } public void validateResponse(HttpServletRequest request, String userCaptchaResponse) { // if no session found boolean validated = false; if (request.getSession(false) == null) // else use service and session id to validate try { validated = service.validateResponseForID(request.getSession() .getId(), userCaptchaResponse); } catch (CaptchaServiceException e) { // do nothing.. false } if(validated){ //success }else{ //error } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2012-01-12
JMS_Exception 写道 freetts .jar 没有的话 下面有附件下载
还有图片验证码 http://www.iteye.com/topic/1119604 /* * JCaptcha, the open source java framework for captcha definition and integration * Copyright (c) 2007 jcaptcha.net. All Rights Reserved. * See the LICENSE.txt file distributed with this package. */ package captcha.servlet; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import captcha.CaptchaServiceSingleton; import com.octo.captcha.service.CaptchaServiceException; import com.octo.captcha.service.sound.SoundCaptchaService; public class SoundCaptchaServlet extends HttpServlet implements Servlet { /** * simple-servlet-image-sample * @Title: SoundCaptchaServlet.java * @Package captcha.servlet * @Description: TODO * @author liazhou * @date 2012-1-7 下午05:11:14 * @version V1.0 */ private static final long serialVersionUID = 3440317252067122452L; public static SoundCaptchaService service = CaptchaServiceSingleton.getSoundInstance(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doPost(request, response); } @Override protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { // Set to expire far in the past. httpServletResponse.setDateHeader("Expires", 0); // Set standard HTTP/1.1 no-cache headers. httpServletResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); // Set IE extended HTTP/1.1 no-cache headers (use addHeader). httpServletResponse.addHeader("Cache-Control", "post-check=0, pre-check=0"); // Set standard HTTP/1.0 no-cache header. httpServletResponse.setHeader("Pragma", "no-cache"); // return a wav httpServletResponse.setContentType("audio/wav"); AudioInputStream audioInputStream = service.getSoundChallengeForID(httpServletRequest. getSession(true).getId()); ServletOutputStream out = httpServletResponse.getOutputStream(); // write the data out ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); AudioSystem .write(audioInputStream, javax.sound.sampled.AudioFileFormat.Type.WAVE, byteOutputStream); out.write(byteOutputStream.toByteArray()); try { out.flush(); } finally { out.close(); } } public void validateResponse(HttpServletRequest request, String userCaptchaResponse) { // if no session found boolean validated = false; if (request.getSession(false) == null) // else use service and session id to validate try { validated = service.validateResponseForID(request.getSession() .getId(), userCaptchaResponse); } catch (CaptchaServiceException e) { // do nothing.. false } if(validated){ //success }else{ //error } } } ! |
|
返回顶楼 | |
发表时间:2012-01-30
好东西啊。
|
|
返回顶楼 | |