SpringBoot集成图形验证码(支持gif、中英文、算术)

Java图形验证码,支持gif、中英文、算术

前言

文中有引用 Redis  可参考 https://blog.csdn.net/javanbme/article/details/114645759

通用Response中的代码就不贴了  自己去掉返回字符串 或替换成自己项目中的

集成示例

1. 依赖

<!-- 图形验证码 -->
<dependency>
    <groupId>com.github.whvcse</groupId>
    <artifactId>easy-captcha</artifactId>
    <version>1.6.2</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.10</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.9</version>
</dependency>

2. 核心工具类

public class CaptchaUtil {

    private static SpecCaptcha specCaptcha = new SpecCaptcha(130, 48);
    private static GifCaptcha gifCaptcha = new GifCaptcha(130, 48);
    private static ChineseCaptcha chineseCaptcha = new ChineseCaptcha(130, 48);
    private static ChineseGifCaptcha chineseGifCaptcha = new ChineseGifCaptcha(130, 48);
    private static ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(130, 48);

    /**
     * TODO  释义
     * SpecCaptcha 和 GifCaptcha 可设置
     * TYPE_DEFAULT	数字和字母混合
     * TYPE_ONLY_NUMBER	纯数字
     * TYPE_ONLY_CHAR	纯字母
     * TYPE_ONLY_UPPER	纯大写字母
     * TYPE_ONLY_LOWER	纯小写字母
     * TYPE_NUM_AND_UPPER	数字和大写字母
     *
     * 内置字体类型 中文方式勿设置
     * Captcha.FONT_1
     * Captcha.FONT_2
     * Captcha.FONT_3
     * Captcha.FONT_4
     * Captcha.FONT_5
     * Captcha.FONT_6
     * Captcha.FONT_7
     * Captcha.FONT_8
     * Captcha.FONT_9
     * Captcha.FONT_10
     */

    /**
     * 英文
     */
    public static SpecCaptcha specCaptcha() throws Exception{
        //默认4位
        specCaptcha.setLen(6);
        specCaptcha.setCharType(Captcha.TYPE_ONLY_CHAR);
        specCaptcha.setFont(Captcha.FONT_2);
        return specCaptcha;
    }

    /**
     * Gif
     */
    public static GifCaptcha gifCaptcha() throws Exception{
        gifCaptcha.setCharType(Captcha.TYPE_ONLY_CHAR);
        gifCaptcha.setFont(Captcha.FONT_2);
        return gifCaptcha;
    }

    /**
     * 中文
     */
    public static ChineseCaptcha chineseCaptcha() throws Exception{
        return chineseCaptcha;
    }

    /**
     * 中文Gif
     */
    public static ChineseGifCaptcha chineseGifCaptcha() throws Exception{
        return chineseGifCaptcha;
    }

    /**
     * 算术
     */
    public static ArithmeticCaptcha arithmeticCaptcha() throws Exception{
        arithmeticCaptcha.setCharType(Captcha.TYPE_ONLY_CHAR);
        arithmeticCaptcha.setFont(Captcha.FONT_2);
        //几位数运算
        arithmeticCaptcha.setLen(3);
        return arithmeticCaptcha;
    }


    /**
     * 输出至本地
     */
    public static void main(String[] args) throws Exception{
        FileOutputStream outputStream = new FileOutputStream(new File("/data/file/captcha.png"));
        specCaptcha().out(outputStream);
        outputStream.close();
        //输出结果
        System.out.println(specCaptcha.text());

    }
}

3. controller

@RestController
public class LoginController {

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 获取图形验证码
     */
    @GetMapping("/captcha")
    public void getCaptcha(HttpServletResponse response,String token) throws Exception{
        SpecCaptcha specCaptcha = CaptchaUtil.specCaptcha();
        specCaptcha.out(response.getOutputStream());

        String verCode = specCaptcha.text().toLowerCase();
        //5分钟过期
        redisUtils.set(token, verCode, 5L * 60);

        response.getOutputStream().close();

    }

    /**
     * 登录校验示例
     */
    @GetMapping("/login")
    public Response login(String username,String password,String verCode,String verKey){
        // 获取redis中的验证码
        Object redisCode = redisUtils.get(verKey);

        // 判断验证码是否过期
        if (redisCode == null){
            return Response.build().err(Error.CAPTCHA_KEY_ERROR);
        }
        if (StringUtils.isEmpty(verCode)  || !redisCode.toString().equals(verCode.trim().toLowerCase())) {
            return Response.build().err(Error.CAPTCHA_KEY_VALUE_ERROR);
        }

        // todo 登录业务

        return Response.build().ok();
    }


}

4. 效果

5.  具体使用说明

1.  修改redis地址
2.  启动应用
3.  访问验证码接口 http://localhost:8080/captcha?token=abc     token值可自定义
4.  访问登录接口  http://localhost:8080/login?verKey=abc&verCode=kgsudm  
vekey为上述token值  verCode 为图形验证码的值  自己修改调试

6. 源码

https://download.csdn.net/download/javanbme/15747564

猜你喜欢

转载自blog.csdn.net/javanbme/article/details/114703274