captcha separate

This commit is contained in:
MaxKey 2021-11-14 13:25:27 +08:00
parent 5cbc8049dc
commit 962c50d083
4 changed files with 58 additions and 20 deletions

View File

@ -4,10 +4,5 @@ description = "maxkey-authentication-captcha"
dependencies { dependencies {
//local jars //local jars
implementation fileTree(dir: '../maxkey-lib/', include: '*/*.jar') implementation fileTree(dir: '../maxkey-lib/', include: '*/*.jar')
implementation project(":maxkey-common")
implementation project(":maxkey-core")
implementation project(":maxkey-persistence")
} }

View File

@ -22,7 +22,6 @@ import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config; import com.google.code.kaptcha.util.Config;
import java.io.IOException; import java.io.IOException;
import java.util.Properties; import java.util.Properties;
import org.maxkey.constants.ConstantsProperties;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
@ -36,6 +35,7 @@ import org.springframework.core.io.Resource;
public class KaptchaAutoConfiguration implements InitializingBean { public class KaptchaAutoConfiguration implements InitializingBean {
private static final Logger _logger = LoggerFactory.getLogger(KaptchaAutoConfiguration.class); private static final Logger _logger = LoggerFactory.getLogger(KaptchaAutoConfiguration.class);
public static final String kaptchaPropertySource = "/kaptcha.properties";
/** /**
* Captcha Producer Config . * Captcha Producer Config .
* @return Producer * @return Producer
@ -43,8 +43,7 @@ public class KaptchaAutoConfiguration implements InitializingBean {
*/ */
@Bean (name = "captchaProducer") @Bean (name = "captchaProducer")
public Producer captchaProducer() throws IOException { public Producer captchaProducer() throws IOException {
Resource resource = new ClassPathResource( Resource resource = new ClassPathResource(kaptchaPropertySource);
ConstantsProperties.classPathResource(ConstantsProperties.kaptchaPropertySource));
_logger.debug("Kaptcha config file " + resource.getURL()); _logger.debug("Kaptcha config file " + resource.getURL());
DefaultKaptcha kaptcha = new DefaultKaptcha(); DefaultKaptcha kaptcha = new DefaultKaptcha();
Properties properties = new Properties(); Properties properties = new Properties();

View File

@ -19,13 +19,16 @@ package org.maxkey.web.contorller;
import com.google.code.kaptcha.Producer; import com.google.code.kaptcha.Producer;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.maxkey.web.WebConstants;
import org.maxkey.web.image.AbstractImageEndpoint;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -36,11 +39,19 @@ import org.springframework.web.bind.annotation.RequestMapping;
* *
*/ */
@Controller @Controller
public class ImageCaptchaEndpoint extends AbstractImageEndpoint { public class ImageCaptchaEndpoint {
private static final Logger _logger = LoggerFactory.getLogger(ImageCaptchaEndpoint.class); private static final Logger _logger = LoggerFactory.getLogger(ImageCaptchaEndpoint.class);
public static final String IMAGE_GIF = "image/gif";
public static final String KAPTCHA_SESSION_KEY = "kaptcha_session_key";
@Autowired @Autowired
private Producer captchaProducer; private Producer captchaProducer;
@Value("${maxkey.login.captcha.type}")
private String captchaType;
/** /**
* captcha image Producer. * captcha image Producer.
@ -53,8 +64,7 @@ public class ImageCaptchaEndpoint extends AbstractImageEndpoint {
try { try {
String kaptchaText = captchaProducer.createText(); String kaptchaText = captchaProducer.createText();
if (applicationConfig.getLoginConfig().getCaptchaType() if (captchaType.equalsIgnoreCase("Arithmetic")) {
.equalsIgnoreCase("Arithmetic")) {
Integer intParamA = Integer.valueOf(kaptchaText.substring(0, 1)); Integer intParamA = Integer.valueOf(kaptchaText.substring(0, 1));
Integer intParamB = Integer.valueOf(kaptchaText.substring(1, 2)); Integer intParamB = Integer.valueOf(kaptchaText.substring(1, 2));
Integer calculateValue = 0; Integer calculateValue = 0;
@ -68,10 +78,10 @@ public class ImageCaptchaEndpoint extends AbstractImageEndpoint {
_logger.trace("Sesssion id " + request.getSession().getId() _logger.trace("Sesssion id " + request.getSession().getId()
+ " , Arithmetic calculate Value is " + calculateValue); + " , Arithmetic calculate Value is " + calculateValue);
request.getSession().setAttribute( request.getSession().setAttribute(
WebConstants.KAPTCHA_SESSION_KEY, calculateValue + ""); KAPTCHA_SESSION_KEY, calculateValue + "");
} else { } else {
// store the text in the session // store the text in the session
request.getSession().setAttribute(WebConstants.KAPTCHA_SESSION_KEY, kaptchaText); request.getSession().setAttribute(KAPTCHA_SESSION_KEY, kaptchaText);
} }
_logger.trace("Sesssion id " + request.getSession().getId() _logger.trace("Sesssion id " + request.getSession().getId()
+ " , Captcha Text is " + kaptchaText); + " , Captcha Text is " + kaptchaText);
@ -84,9 +94,46 @@ public class ImageCaptchaEndpoint extends AbstractImageEndpoint {
} }
} }
/**
* producerImage.
* @param request HttpServletRequest
* @param response HttpServletResponse
* @param bufferedImage BufferedImage
* @throws IOException error
*/
public static void producerImage(HttpServletRequest request,
HttpServletResponse response,
BufferedImage bufferedImage) throws IOException {
// Set to expire far in the past.
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/gif
response.setContentType(IMAGE_GIF);
_logger.trace("create the image");
// create the image
if (bufferedImage != null) {
ServletOutputStream out = response.getOutputStream();
// write the data out
ImageIO.write(bufferedImage, "gif", out);
try {
out.flush();
} finally {
out.close();
}
}
}
public void setCaptchaProducer(Producer captchaProducer) { public void setCaptchaType(String captchaType) {
this.captchaType = captchaType;
}
public void setCaptchaProducer(Producer captchaProducer) {
this.captchaProducer = captchaProducer; this.captchaProducer = captchaProducer;
} }

View File

@ -18,9 +18,6 @@
package org.maxkey.constants; package org.maxkey.constants;
public class ConstantsProperties { public class ConstantsProperties {
public static final String kaptchaPropertySource =
"classpath:/kaptcha.properties";
public static String classPathResource(String propertySource) { public static String classPathResource(String propertySource) {
return propertySource.replaceAll("classpath:",""); return propertySource.replaceAll("classpath:","");