com.github.axet.kaptcha制作验证码的方法
导入jar包com.github.axet生成法
①导包
<!-- 验证码 --><dependency><groupId>com.github.axet</groupId><artifactId>kaptcha</artifactId><version>0.0.9</version></dependency>
②创建配置类,用来配置验证码的生成
@Configuration
public class CaptchaProducer {@Beanpublic DefaultKaptcha producer(){Properties properties = new Properties();properties.put("kaptcha.border","yes");properties.put("kaptcha.border.color","105,179,90");properties.put("kaptcha.textproducer.font.color","72,118,255");properties.put("kaptcha.obscurificator.impl","com.google.code.kaptcha.impl.WaterRipple");properties.put("kaptcha.noise.impl","com.google.code.kaptcha.impl.DefaultNoise");properties.put("kaptcha.noise.color","72,118,255");properties.put("kaptcha.image.width","125");properties.put("kaptcha.image.height","50");properties.put("kaptcha.textproducer.font.size","40");properties.put("kaptcha.textproducer.char.length","4");properties.put("kaptcha.textproducer.char.font.names","Arial, Courier");properties.put("kaptcha.textproducer.char.space","4");properties.put("kaptcha.textproducer.impl","com.google.code.kaptcha.text.impl.DefaultTextCreator");properties.put("kaptcha.session.key","code");Config config = new Config(properties);DefaultKaptcha defaultKaptcha = new DefaultKaptcha();defaultKaptcha.setConfig(config);return defaultKaptcha;}
}
③调用图片生成的类(一般为Controller层)
@Slf4j
@Api(description = "校验码控制器")
@RestController
public class VerificationCodeController {private static final Logger logger = LoggerFactory.getLogger(VerificationCodeController.class);@Autowiredprivate Producer producer;@RequestMapping(value = {"/fis/login/genCode.action"}, method = {RequestMethod.POST, RequestMethod.GET})@ApiOperation(value = "获取检验码")public void captcha(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {HttpSession session = request.getSession();response.setDateHeader("Expires", 0);response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");response.addHeader("Cache-Control", "post-check=0, pre-check=0");response.setHeader("Pragma", "no-cache");response.setContentType("image/jpeg");//生成图片验证码Object[] objs = ImageCodeUtil.createImage();session.setAttribute(SessionConst.VERIFICATION_CODE, objs[0]);ServletOutputStream out = response.getOutputStream();ImageIO.write((BufferedImage) objs[1], "jpg", out);}@ApiOperation(value = "校验图形验证码")@RequestMapping(value = {"/fis/login/chkGenCode.action"}, method = {RequestMethod.POST})public void chkImgCode(@Context HttpServletRequest request, @Context HttpServletResponse response,@ApiParam(value = "图形验证码信息", required = true, type = "") @RequestBody String inParam) throws Exception {logger.debug(">HTTP Request param : {}", inParam);HttpSession session = request.getSession();if (StringUtils.isEmpty(inParam)) {throw new WsgException("入参不可为空。");}logger.debug(">HTTP Request param : {}", inParam);MBean mBean1 = new MBean(inParam);Map<String, Object> imgCodeMap = mBean1.getBody();String imgCode = (String) imgCodeMap.get("imgCheckCode");// 校验验证码if (!imgCode.equals(session.getAttribute(SessionConst.VERIFICATION_CODE))) {throw new WsgException(WsgError.VERIFYCODE_ERROR.getCode(), "图形验证码错误。");}MBean out = new MBean();out.setBody("RETURN_CODE", Constants.RETURN_CODE.OTHER_SUCCESS);ResponseUtil.wirteString(response, out.toString());}
Constant | 描述 | 默认值 |
---|---|---|
kaptcha.border | 图片边框,合法值:yes , no | yes |
kaptcha.border.color | 边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue. | black |
kaptcha.border.thickness | 边框厚度,合法值:>0 | 1 |
kaptcha.image.width | 图片宽 | 200 |
kaptcha.image.height | 图片高 | 50 |
kaptcha.producer.impl | 图片实现类 | com.google.code.kaptcha.impl.DefaultKaptcha |
kaptcha.textproducer.impl | 文本实现类 | com.google.code.kaptcha.text.impl.DefaultTextCreator |
kaptcha.textproducer.char.string | 文本集合,验证码值从此集合中获取 | abcde2345678gfynmnpwx |
kaptcha.textproducer.char.length | 验证码长度 | 5 |
kaptcha.textproducer.font.names | 字体 | Arial, Courier |
kaptcha.textproducer.font.size | 字体大小 | 40px. |
kaptcha.textproducer.font.color | 字体颜色,合法值: r,g,b 或者 white,black,blue. | black |
kaptcha.textproducer.char.space | 文字间隔 | 2 |
kaptcha.noise.impl | 干扰实现类 | com.google.code.kaptcha.impl.DefaultNoise |
kaptcha.noise.color | 干扰 颜色,合法值: r,g,b 或者 white,black,blue. | black |
kaptcha.obscurificator.impl | 图片样式: 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy | com.google.code.kaptcha.impl.WaterRipple |
kaptcha.background.impl | 背景实现类 | com.google.code.kaptcha.impl.DefaultBackground |
kaptcha.background.clear.from | 背景颜色渐变,开始颜色 | light grey |
kaptcha.background.clear.to | 背景颜色渐变, 结束颜色 | white |
kaptcha.word.impl | 文字渲染器 | com.google.code.kaptcha.text.impl.DefaultWordRenderer |
kaptcha.session.key | session key | KAPTCHA_SESSION_KEY |
kaptcha.session.date | session date | KAPTCHA_SESSION_DATE |
采用postman进行测试,结果如下:
kaptcha github 地址: https://github.com/penggle/kaptcha
可进行源码查看,增加你的胃动力,徐大叔不太冷,为您持续集成优秀框架。