?? imagecaptchaservlet.java
字號:
package anni.core.security.jcaptcha;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* Servlet generates CAPTCHA jpeg images based on the JCAPTCHA package. It's
* configured via spring, and requires a ImageCaptchaService bean with the
* id=imageCaptchaService
*
* @author Jason Thrasher
* @author Lingo
* @since 2007-04-07
*/
public class ImageCaptchaServlet extends HttpServlet {
/**
* serial.
*/
private static final long serialVersionUID = -1L;
/**
* service name.
*/
private String captchaServiceName = "imageCaptchaService";
/**
* 初始化.
*
* @param servletConfig ServletConfig
* @throws ServletException servlet異常
*/
public void init(ServletConfig servletConfig) throws ServletException {
if (StringUtils.isNotBlank(servletConfig.getInitParameter(
"captchaServiceName"))) {
captchaServiceName = servletConfig.getInitParameter(
"captchaServiceName");
}
super.init(servletConfig);
}
/**
* 處理get請求.
*
* @param httpServletRequest 請求
* @param httpServletResponse 響應
* @throws ServletException servlet異常
* @throws IOException io異常
*/
protected void doGet(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse)
throws ServletException, IOException {
byte[] captchaChallengeAsJpeg = null;
// the output stream to render the captcha image as jpeg into
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
try {
// get the image captcha service defined via the SpringFramework
ApplicationContext ctx = WebApplicationContextUtils
.getRequiredWebApplicationContext(getServletContext());
Object bean = ctx.getBean(captchaServiceName);
ImageCaptchaService imageCaptchaService = (ImageCaptchaService) bean;
// get the session id that will identify the generated captcha.
// the same id must be used to validate the response, the session id
// is a good candidate!
String captchaId = httpServletRequest.getSession().getId();
// call the ImageCaptchaService getChallenge method
BufferedImage challenge = imageCaptchaService
.getImageChallengeForID(captchaId,
httpServletRequest.getLocale());
// a jpeg encoder
JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(jpegOutputStream);
jpegEncoder.encode(challenge);
} catch (IllegalArgumentException e) {
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
} catch (CaptchaServiceException e) {
httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
// flush it in the response
httpServletResponse.setHeader("Cache-Control", "no-store");
httpServletResponse.setHeader("Pragma", "no-cache");
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/jpeg");
ServletOutputStream responseOutputStream = httpServletResponse
.getOutputStream();
responseOutputStream.write(captchaChallengeAsJpeg);
responseOutputStream.flush();
responseOutputStream.close();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -