ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

谷歌 Captcha验证码

2021-11-20 22:30:44  阅读:216  来源: 互联网

标签:kaptcha 谷歌 验证码 response Captcha defaultKaptcha setProperty properties


1.pom依赖

        <!-- 谷歌 Captcha验证码依赖 -->
        <dependency>
            <groupId>com.github.axet</groupId>
            <artifactId>kaptcha</artifactId>
            <version>0.0.9</version>
        </dependency>

2.新建CaptchaConfig.java文件

/**
 * 谷歌 Captcha验证码配置类
 */
@Configuration
public class CaptchaConfig {

    @Bean
    public DefaultKaptcha defaultKaptcha() {
        // 验证码生成器
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        // 配置
        Properties properties = new Properties();
        // 是否有边框
        properties.setProperty("kaptcha.border","yes");
        // 设置边框颜色
        properties.setProperty("kaptcha.border.color","105,179,90");
        // 验证码
        properties.setProperty("kaptcha.session.code","code");
        // 验证码文本字符颜色,默认为黑色
        properties.setProperty("kaptcha.textproducer.font.color","blue");
        // 设置字体样式
        properties.setProperty("kaptcha.textproducer.font.names","宋体,楷体,微软雅黑");
        // 字体大小,默认为40
        properties.setProperty("kaptcha.textproducer.font.size","30");
        // 字符长度,默认为5
        properties.setProperty("kaptcha.textproducer.char.length","4");
        // 字符间距,默认为2
        properties.setProperty("kaptcha.textproducer.char.space","4");
        // 验证码图片宽度,默认为200
        properties.setProperty("kaptcha.iamge.width","100");
        // 验证码图片高度,默认为40
        properties.setProperty("kaptcha.iamge.height", "40");
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}

3.新建CaptchaController接口

/**
 * 谷歌 Captcha验证码 接口
 */
@RestController
public class CaptchaController {
    @Autowired
    private DefaultKaptcha defaultKaptcha;

    @ApiOperation(value = "验证码")
    @GetMapping(value = "/captcha",produces = "image/jpeg")
    public void captcha(HttpServletRequest request, HttpServletResponse response) {
        // 定义response输出类型为image/jpeg类型
        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");

        // 开始生成验证码
        String text = defaultKaptcha.createText(); // 获取验证码文本内容
        System.out.println("验证码文本内容:" + text);
        request.getSession().setAttribute("captcha", text);
        BufferedImage image = defaultKaptcha.createImage(text); // 根据文本内容创建图形验证码
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            ImageIO.write(image, "jpg", outputStream); // 输出流输出图片,格式为jpg
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != outputStream) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // 生成验证码结束
    }
}

标签:kaptcha,谷歌,验证码,response,Captcha,defaultKaptcha,setProperty,properties
来源: https://blog.csdn.net/weixin_55806809/article/details/121446596

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有