com.github.penggle
kaptcha
2.3.2
package cn.penghf.lovemaster.config;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class KaptchaConfiguration {
@Bean(name="captchaProducer")
public DefaultKaptcha getKaptchaBean(){
DefaultKaptcha defaultKaptcha=new DefaultKaptcha();
Properties properties=new Properties();
properties.setProperty("kaptcha.image.width", "160");
properties.setProperty("kaptcha.image.height", "50");
properties.setProperty("kaptcha.border", "no");
//properties.setProperty("kaptcha.border.color", "105,179,90");
properties.setProperty("kaptcha.textproducer.font.color", "255,103,51");
properties.setProperty("kaptcha.textproducer.font.size","43");
properties.setProperty("kaptcha.noise.color", "152,204,102");
properties.setProperty("kaptcha.background.clear.from","130,129,179");
properties.setProperty("kaptcha.background.clear.to","182,188,242");
//properties.setProperty("kaptcha.session.key", "code");
properties.setProperty("kaptcha.textproducer.char.length", "4");
properties.setProperty("kaptcha.textproducer.char.space","5");
properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
properties.setProperty("kaptcha.obscurificator.impl","com.google.code.kaptcha.impl.ShadowGimpy");
properties.setProperty("kaptcha.background.impl","com.google.code.kaptcha.impl.DefaultBackground");
Config config=new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
package cn.penghf.lovemaster.controller.security;
import cn.penghf.common.entity.security.Person;
import cn.penghf.common.utils.PKGenerator;
import cn.penghf.lovemaster.constants.CookieConstants;
import cn.penghf.lovemaster.constants.RedisConstants;
import cn.penghf.lovemaster.service.security.PersonService;
import cn.penghf.lovemaster.utils.http.CookieUtil;
import cn.penghf.lovemaster.utils.redis.RedisUtil;
import com.google.code.kaptcha.Producer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
@Api("登录控制")
@RestController
@RequestMapping("/master/security/login")
@Slf4j
public class LoginController {
@Autowired
private Producer captchaProducer;
@ApiOperation("验证码生成")
@RequestMapping("/getValidateCode")
public void getValidateCode(HttpServletResponse response){
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.info("验证码:"+capText);
// store the text in the session
String validateCodeId = PKGenerator.generateId();
RedisUtil.set(String.format(RedisConstants.VALIDATECODE_PREFIX,validateCodeId),capText,RedisConstants.EXPIRE);
//设置token到cookie
CookieUtil.set(response, CookieConstants.VALIDATECODE , validateCodeId,CookieConstants.EXPIRE);
// create the image with the text
BufferedImage bi = captchaProducer.createImage(capText);
// write the data out
ServletOutputStream os = null;
try {
os = response.getOutputStream();
ImageIO.write(bi, "jpg", os);
os.flush();
//return new RespBody(RespBody.StatusEnum.OK);
} catch (IOException e) {
log.error("获取验证码输出流异常",e);
//return new RespBody(RespBody.StatusEnum.ERROR,"验证码获取异常");
}finally {
try {
os.close();
} catch (IOException e) {
log.error("验证码输出流关闭异常",e);
//return new RespBody(RespBody.StatusEnum.ERROR,"验证码获取异常");
}
}
}
}
plugin
com.spotify
docker-maven-plugin
1.0.0
${docker.image.prefix}.${project.artifactId}
src/main/docker
/
${project.build.directory}
${project.build.finalName}.jar
Dockerfile
FROM openjdk:8-jdk-alpine
VOLUME /tmp
##时区
RUN echo "Asia/Shanghai" > /etc/timezone
#captcher 字体包
RUN set -xe \
&& apk --no-cache add ttf-dejavu fontconfig
ADD love-master-0.0.1-SNAPSHOT.jar love-master.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/love-master.jar"]
openjdk:8-jdk-alpine 没有相关字体包,调用createImage()方法时,会报空指针异常,此处已经指定docker容器安装字体包
kaptcha相关配置参数参考
参考文章:
https://www.jianshu.com/p/370199b1625f