인증 코드 서비스
com.github.penggle
kaptcha
2.3.2
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.apache.commons.collections4.map.PassiveExpiringMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @author Kelvin
* @createDate 2018 11 15
*/
@Service
public class CaptchaServiceImpl implements CaptchaService,InitializingBean {
private Logger logger = LoggerFactory.getLogger(getClass());
// msg
final static String MSG_TIMEOUT = " ";
final static String MSG_FAILED = " ";
//config
private int minutesToLive=3;
private String imgFormat="png";
private DefaultKaptcha producer;
private Map captchaMap = Collections.synchronizedMap(
new PassiveExpiringMap(minutesToLive,TimeUnit.MINUTES,new HashMap<>()));
@Override
public void afterPropertiesSet() {
producer = new DefaultKaptcha();
producer.setConfig(new Config(new Properties()));
}
@Override
public String generateCaptchaKey() throws CaptchaException {
String key = UUID.randomUUID().toString();
captchaMap.put(key, producer.createText());
return key;
}
@Override
public byte[] generateCaptchaImage(String captchaKey) throws CaptchaException {
String text = Optional.ofNullable(captchaMap.get(captchaKey))
.orElseThrow(()->new CaptchaException(MSG_TIMEOUT));
BufferedImage image = producer.createImage(text);
ByteArrayOutputStream out = new ByteArrayOutputStream();
try{
ImageIO.write(image, imgFormat, out);
}catch(IOException e){
logger.error(MSG_FAILED,e);
throw new CaptchaException(MSG_FAILED,e);
}
return out.toByteArray();
}
@Override
public boolean validateCaptcha(String captchaKey, String captchaValue) throws CaptchaException {
if(Optional.ofNullable(captchaMap.get(captchaKey)).orElseThrow(()->new CaptchaException(MSG_TIMEOUT))
.equalsIgnoreCase(captchaValue)){
captchaMap.remove(captchaKey);
return true;
}
return false;
}
}
@RestController
@RequestMapping("/captcha")
public class CaptchaController {
final CaptchaService captchaService;
public CaptchaController(CaptchaService captchaService) {
this.captchaService = captchaService;
}
@GetMapping
public ResponseEntity getImgCaptcha(){
Map map= new HashMap();
map.put("key", captchaService.generateCaptchaKey());
map.put("img", "data:image/png;base64,"+Base64.encodeBase64String(
captchaService.generateCaptchaImage(map.get("key").toString())));
return ResponseEntity.ok(map);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.