| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- POST 전송
- 다음메일
- 스팸글 차단
- 암호화&복호화
- WEB-INF 노출
- MARGIN
- 고창
- 치환
- 자바스크립트
- 2012 사진공모전
- apache tomcat 연동 보안
- 비밀번호 유효성
- Java
- XSS 차단
- column명비교
- html5
- 네이버 지도API
- addbatch
- 중복필드
- 배경이 가려진 레이어 팝업
- fckeditor
- 클라우드
- 스크롤 이동
- PADDING
- 자동 로봇 글등록
- CSS
- 퀵메뉴
- @tistory.com
- 청보리밭
- 일괄처리
Archives
- Today
- Total
그곰의 생활
[스크랩]jsp 자동가입 방지 이미지 생성 본문
출처 : Posted by 미야프 http://miyaf.tistory.com/archive/20090718모든 파일과 설명은 http://forge.octo.com/jcaptcha/confluence/display/general/Developer+Documentation
이곳에서 확인 가능.
매이븐 2 를 사용하지 않았으므로 그 아래의 내용대로...
http://sourceforge.net/projects/jcaptcha/files/
에서 jcaptcha-all.jar 파일을 받고
http://commons.apache.org/downloads/download_collections.cgi
에서 commons-collection-3.2 혹은 상위 버전을 받아서
webcontent 밑의 WEB-INF 밑의 lib에 넣어 준다.
ImageCaptchaServlet 클래스 생성
사용할 페이지에
입력값과 이미지가 맞는지 확인할 다음 소스를 서블릿이나 적적한 곳에 삽입
Could not initialize class CaptchaServiceSingleton에러가 나네..... 살펴 보니 commons-logging.jar 파일이 라이브러리에 더 추가 되어야 함./////////////////////////////////////////////////////////위의 자동생성 이미지 커스터마이징 부분다음과 같은 클래스 하나 더 생성
//위의 WordGenerator wgen = new RandomWordGenerator("ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"); 부분에서 따옴표 안쪽을 숫자만으로 바꾸면 숫자만으로 출력되며, 원하는 문자로 바꿀수 있음그리고 싱글턴 클래스를 다음으로 수정
이곳에서 확인 가능.
매이븐 2 를 사용하지 않았으므로 그 아래의 내용대로...
http://sourceforge.net/projects/jcaptcha/files/
에서 jcaptcha-all.jar 파일을 받고
http://commons.apache.org/downloads/download_collections.cgi
에서 commons-collection-3.2 혹은 상위 버전을 받아서
webcontent 밑의 WEB-INF 밑의 lib에 넣어 준다.
CaptchaServiceSingleton 클래스 생성
import com.octo.captcha.service.image.ImageCaptchaService;
import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
public class CaptchaServiceSingleton {
private static ImageCaptchaService instance = new DefaultManageableImageCaptchaService();
public static ImageCaptchaService getInstance(){
return instance;
}
}
import com.octo.captcha.service.CaptchaServiceException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
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 java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class ImageCaptchaServlet extends HttpServlet {
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
}
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 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 =
CaptchaServiceSingleton.getInstance().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();
}
}
web.xml에
<servlet> <servlet-name>jcaptcha</servlet-name> <servlet-class>ImageCaptchaServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jcaptcha</servlet-name> <url-pattern>/jcaptcha</url-pattern> </servlet-mapping>
추가
<img src="/jcaptcha"> <input type='text' name='j_captcha_response' value=''>
추가
입력값과 이미지가 맞는지 확인할 다음 소스를 서블릿이나 적적한 곳에 삽입
Boolean isResponseCorrect =Boolean.FALSE;
//remenber that we need an id to validate!
String captchaId = httpServletRequest.getSession().getId();
//retrieve the response
String response = httpServletRequest.getParameter("j_captcha_response");
// Call the Service method
try {
isResponseCorrect = CaptchaServiceSingleton.getInstance().validateResponseForID(captchaId,
response);
} catch (CaptchaServiceException e) {
//should not happen, may be thrown if the id is not valid
}
//do something according to the result!
해주면 끝... 이라고 나와있지만...
public class MyImageCaptchaEngine extends ListImageCaptchaEngine {
protected void buildInitialFactories() {
WordGenerator wgen = new RandomWordGenerator("ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789");
RandomRangeColorGenerator cgen = new RandomRangeColorGenerator(
new int[] {0, 100},
new int[] {0, 100},
new int[] {0, 100});
TextPaster textPaster = new RandomTextPaster(new Integer(7), new Integer(7), cgen, true);
BackgroundGenerator backgroundGenerator = new FunkyBackgroundGenerator(new Integer(200), newInteger(100));
Font[] fontsList = new Font[] {
new Font("Arial", 0, 10),
new Font("Tahoma", 0, 10),
new Font("Verdana", 0, 10),
};
FontGenerator fontGenerator = new RandomFontGenerator(new Integer(20), new Integer(35), fontsList);
WordToImage wordToImage = new ComposedWordToImage(fontGenerator, backgroundGenerator, textPaster);
this.addFactory(new GimpyFactory(wgen, wordToImage));
}
}
public class CaptchaServiceSingleton {
private static ImageCaptchaService instance;
static {
instance = new DefaultManageableImageCaptchaService(
new FastHashMapCaptchaStore(),
new MyImageCaptchaEngine(),
180,
100000,
75000);
}
public static ImageCaptchaService getInstance(){
return instance;
}
}
하면, 앞의 기본형보다 조금더 보기 좋은 이미지를 생성하게 됨
'Server-side > Lang' 카테고리의 다른 글
| WGS-84 방식의 좌표값을 가지는 두 좌표의 거리를 구하는 함수 (0) | 2012.03.29 |
|---|---|
| 숫자 단위 표시 방법 (0) | 2012.01.20 |
| [스크랩]JSP 스팸글 차단 - 조그마한거 (0) | 2011.11.17 |
| [스크랩]JSP 자동등록 방지 (0) | 2011.11.17 |
| XSS 차단 java 선언 (2) | 2011.10.04 |
Comments