티스토리 뷰

■ CAPTCHA 란? (in http://www.captcha.net/)

CAPTCHA 는 웹 사이트를 bots로부터 보호하기 위해서 프로그램이 아닌 사람이 통과할 수 있는 테스트를 만드는 프로그램이다. 예를 들어 사람은 찌르러져있는 글자를 앍을 수 있지만 현재 컴퓨터 프로그램은 불가능 하다.

CAPTCHA (for Completely Automated Public Turing Test To Tell Computers and Humans Apart) 라는 용어는 2000년 카네기 멜런 대학은 Luis von Ahn, Manuel Blum, Nicholas Hopper, John Langford 의해 만들어 졌다.

■ Stands for J ava C ompletely A utomated P ublic T est to tell C omputers and H umans A part

JCaptcha 는 IoC 패턴을 따르기 때문에 구성 어플리케이션의 컴포넌트로 쉽게 생성할 수 있다. 반면 스프링프레임워크는 XML선언으로 결합되는 강력한 IoC를 지원한다. 그래서 jCaptcha의 모든 single 컴포넌트들은 XML에 선언되고 스프링을 통해 인스턴스화할 수 있다. jCaptcha 를 사용하는 어플리케이션은 오직 CaptchaService 인스턴스만 다루면 된다.

  1. jCaptcha 라이브러리 다운로드 및 세팅하기
  2. CaptchaServiceSingleton 작성
  3. CaptchaServlet 작성
  4. jCaptcha config 작성
  5. web.xml 설정

jCaptcha 라이브러리 다운로드 및 세팅하기

  • http://jcaptcha.sourceforge.net/ 에서 jCaptcha 라이브러리를 다운받고 압축을 푼다.
  • 압축을 푼 폴더 안에 있는 jcaptcha-1.0-all.jar 를 WEB-INF/lib 에 복사한다.

CaptchaServiceSingleton 작성

public class CaptchaServiceSingleton {
	
	private static ImageCaptchaService instance = new DefaultManageableImageCaptchaService();

	public static ImageCaptchaService getInstance() {
		return instance;
	}
}

CaptchaServlet 작성

web.xml 에 설정할 servlet 을 구현한다.

public class CaptchaServlet extends HttpServlet {

	private static final long serialVersionUID = 8067223183038103582L;

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		byte[] captchaChallengeAsJpeg = null;
		// the output stream to render the captcha image as jpeg into
		ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
		try {
			// 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 = req.getSession().getId();
			// call the ImageCaptchaService getChallenge method
			BufferedImage challenge = CaptchaServiceSingleton.getInstance()
					.getImageChallengeForID(captchaId, req.getLocale());
			// a jpeg encoder

			JPEGImageEncoder jpegEncoder = JPEGCodec
					.createJPEGEncoder(jpegOutputStream);
			jpegEncoder.encode(challenge);
		} catch (IllegalArgumentException e) {
			resp.sendError(HttpServletResponse.SC_NOT_FOUND);
			return;
		} catch (CaptchaServiceException e) {
			resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			return;
		}

		captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

		// flush it in the response
		resp.setHeader("Cache-Control", "no-store");
		resp.setHeader("Pragma", "no-cache");
		resp.setDateHeader("Expires", 0);
		resp.setContentType("image/jpeg");
		ServletOutputStream responseOutputStream = resp.getOutputStream();
		responseOutputStream.write(captchaChallengeAsJpeg);
		responseOutputStream.flush();
		responseOutputStream.close();
	}

}

jCaptcha config 작성

(Setting a captcha custom service and engine configuration in Spring)

CaptchaFactory

  • word generator(워드발생기) : 읽으려는 텍스트
  • wordToImage(워드변환기) : 텍스트를 captcha 로 만듬
<bean id="CaptchaFactory" class="com.octo.captcha.image.gimpy.GimpyFactory" >
	<constructor-arg><ref bean="wordgen"/></constructor-arg>
	<constructor-arg><ref bean="wordtoimage"/></constructor-arg>
</bean>

web.xml 설정

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			....
			/WEB-INF/context/system/applicationContext-jcaptcha.xml
		</param-value>
	</context-param>

댓글