com.google.code.kaptcha.Producer Java Examples

The following examples show how to use com.google.code.kaptcha.Producer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KaptchaConfig.java    From MyCommunity with Apache License 2.0 6 votes vote down vote up
@Bean
public Producer kaptchaProducer()  {
    Properties properties = new Properties();
    properties.setProperty("kaptcha.image.width", "100");
    properties.setProperty("kaptcha.image.height", "40");
    properties.setProperty("kaptcha.textproducer.font.size", "32");  // 字号
    properties.setProperty("kaptcha.textproducer.font.color", "0,0,0");  // 字体颜色
    properties.setProperty("kaptcha.textproducer.char.string", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYAZ");
    properties.setProperty("kaptcha.textproducer.char.length", "4");  // 验证码数量
    properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");  // 设置干扰

    DefaultKaptcha kaptcha = new DefaultKaptcha();
    Config config = new Config(properties);
    kaptcha.setConfig(config);
    return kaptcha;
}
 
Example #2
Source File: LoginController.java    From dpCms with Apache License 2.0 5 votes vote down vote up
/**
 * 获取登录的图片验证码
 */
@RequestMapping(value = "/imgcode", method = RequestMethod.GET)
public void captcha(HttpServletRequest request, HttpServletResponse response )
		throws ServletException, IOException {
	Subject currentUser = SecurityUtils.getSubject();
	Session session = currentUser.getSession();
	Producer captchaProducer = KaptchaProducerAgency.getKaptchaProducerExample();
	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();
	log.debug("******************验证码是: " + capText + "******************");
	// store the text in the session
	session.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();
	}
}
 
Example #3
Source File: KaptchaAutoConfiguration.java    From MaxKey with Apache License 2.0 5 votes vote down vote up
/**
 * Captcha Producer  Config .
 * @return Producer
 * @throws IOException kaptcha.properties is null
 */
@Bean (name = "captchaProducer")
public Producer captchaProducer() throws IOException {
    Resource resource = new ClassPathResource(
            ConstantsProperties.classPathResource(ConstantsProperties.kaptchaPropertySource));
    _logger.debug("Kaptcha config file " + resource.getURL());
    DefaultKaptcha  kaptcha = new DefaultKaptcha();
    Properties properties = new Properties();
    properties.load(resource.getInputStream());
    Config config = new Config(properties);
    kaptcha.setConfig(config);
    return kaptcha;
}
 
Example #4
Source File: CodeController.java    From sanshanblog with Apache License 2.0 4 votes vote down vote up
@Autowired
public void setCaptchaProducer(Producer captchaProducer){
    this.captchaProducer = captchaProducer;
}
 
Example #5
Source File: CaptchaUtil.java    From carbon-identity-framework with Apache License 2.0 4 votes vote down vote up
/**
 * Generate the captcha image.
 *
 * @return CaptchaInfoBean
 * @throws Exception - no exception handling here.
 *                   Exceptions in generating the captcha are thrown as they are.
 */
public static CaptchaInfoBean generateCaptchaImage() throws Exception {
    String randomSecretKey = UUID.randomUUID().toString();  //random string for the captcha.
    String imagePath = CaptchaMgtConstants.CAPTCHA_IMAGES_PATH +
            RegistryConstants.PATH_SEPARATOR + randomSecretKey + ".jpg";

    Config config = new Config(new Properties());
    Producer captchaProducer = config.getProducerImpl();
    String captchaText = captchaProducer.createText();

    BufferedImage image = captchaProducer.createImage(captchaText);

    File tempFile = File.createTempFile("temp-", ".jpg");

    try {
        ImageIO.write(image, "jpg", tempFile);

        byte[] imageBytes = CarbonUtils.getBytesFromFile(tempFile);

        // saving the image
        Registry superTenantRegistry = CaptchaMgtServiceComponent.getConfigSystemRegistry(
                MultitenantConstants.SUPER_TENANT_ID);
        Resource imageResource = superTenantRegistry.newResource();
        imageResource.setContent(imageBytes);
        superTenantRegistry.put(imagePath, imageResource);


        // prepare the captcha info bean
        CaptchaInfoBean captchaInfoBean = new CaptchaInfoBean();
        captchaInfoBean.setSecretKey(randomSecretKey);   //random generated value as secret key
        captchaInfoBean.setImagePath("registry" + RegistryConstants.PATH_SEPARATOR + "resource" +
                RegistryConstants.CONFIG_REGISTRY_BASE_PATH + imagePath);

        // now create an entry in the registry on the captcha
        Resource recordResource = superTenantRegistry.newResource();
        ((ResourceImpl) recordResource).setVersionableChange(false); // no need to version
        recordResource.setProperty(CaptchaMgtConstants.CAPTCHA_TEXT_PROPERTY_KEY, captchaText);
        recordResource.setProperty(CaptchaMgtConstants.CAPTCHA_PATH_PROPERTY_KEY, imagePath);

        superTenantRegistry.put(CaptchaMgtConstants.CAPTCHA_DETAILS_PATH +
                        RegistryConstants.PATH_SEPARATOR + randomSecretKey,
                recordResource);
        if (log.isDebugEnabled()) {
            log.debug("Successfully generated the captcha image.");
        }
        return captchaInfoBean;
    } finally {
        if (!tempFile.delete()) {
            log.warn("Could not delete " + tempFile.getAbsolutePath());
        }
    }
}
 
Example #6
Source File: CaptchaImageCreateController.java    From PhrackCTF-Platform-Team with Apache License 2.0 4 votes vote down vote up
@Autowired  
public void setCaptchaProducer(Producer captchaProducer) {  
    this.captchaProducer = captchaProducer;  
}
 
Example #7
Source File: ImageCaptchaEndpoint.java    From MaxKey with Apache License 2.0 4 votes vote down vote up
public void setCaptchaProducer(Producer captchaProducer) {
    this.captchaProducer = captchaProducer;
}
 
Example #8
Source File: CaptchaImageCreateController.java    From PhrackCTF-Platform-Personal with Apache License 2.0 4 votes vote down vote up
@Autowired  
public void setCaptchaProducer(Producer captchaProducer) {  
    this.captchaProducer = captchaProducer;  
}
 
Example #9
Source File: CaptchaImageCreateController.java    From MultimediaDesktop with Apache License 2.0 4 votes vote down vote up
public void setCaptchaProducer(Producer captchaProducer) {
	this.captchaProducer = captchaProducer;
}
 
Example #10
Source File: ImageCodeGenerator.java    From paascloud-master with Apache License 2.0 2 votes vote down vote up
/**
 * Sets captcha producer.
 *
 * @param captchaProducer the captcha producer
 */
public void setCaptchaProducer(Producer captchaProducer) {
	this.captchaProducer = captchaProducer;
}