kaptcha制作验证码的方法

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 {
    @Bean
    public 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);

    @Autowired
    private 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 , noyes
kaptcha.border.color边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue.black
kaptcha.border.thickness边框厚度,合法值:>01
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.ShadowGimpycom.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.keysession keyKAPTCHA_SESSION_KEY
kaptcha.session.datesession dateKAPTCHA_SESSION_DATE

采用postman进行测试,结果如下:
在这里插入图片描述

kaptcha github 地址: https://github.com/penggle/kaptcha
可进行源码查看,增加你的胃动力,徐大叔不太冷,为您持续集成优秀框架。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值