微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

验证码工具-easy-captcha

   <dependency>
            <groupId>com.github.whvcse</groupId>
            <artifactId>easy-captcha</artifactId>
            <version>1.6.2</version>
        </dependency>
package com.msb.controller;

import com.msb.annotations.TokenCheck;
import com.ramostear.captcha.HappyCaptcha;
import com.ramostear.captcha.support.CaptchaStyle;
import com.ramostear.captcha.support.CaptchaType;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.ChineseCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedistemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.UUID;

/**
 * @author lcc
 * @version V1.0
 * @Package com.msb.code
 * @date 2022/5/8 7:49
 */
@RestController
@RequestMapping("/easy-captcha")
public class EasyCaptchaController {
    @Autowired
    private StringRedistemplate stringRedistemplate;
    
    @GetMapping("/generator")
    @TokenCheck(required = false)
    public void generatorCode(HttpServletRequest request, HttpServletResponse response){
        try {
            //CaptchaUtil.out(request, response);//认4位字母验证码
            /*3个算数  运算 存储到redis*/
        /*    ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(200,50);
            arithmeticCaptcha.setLen(3);//3个数的运算
            String text = arithmeticCaptcha.text();
            System.out.println("验证码:"+text);
            stringRedistemplate.opsForValue().set("验证码", text);
            CaptchaUtil.out(arithmeticCaptcha,request, response);*/
        /*中文存储*/
            ChineseCaptcha chineseCaptcha = new ChineseCaptcha(200,50);
            chineseCaptcha.setLen(4);
            String text = chineseCaptcha.text();
            System.out.println("验证码:"+text);
            CaptchaUtil.out(chineseCaptcha, request,response);
    
        } catch (IOException e) {
            e.printstacktrace();
        }
    }
    
    @GetMapping("/verify")
    @TokenCheck(required = false)
    public String verify(String verifyCode,HttpServletRequest request){
        Boolean aBoolean=CaptchaUtil.ver(verifyCode, request);
        if(aBoolean){
            CaptchaUtil.clear(request);
            return "通过";
        }
        return "不通过";
    }
    
    /*怎么将验证码存入redis  实现多机器验证*/
    @GetMapping("/generator-redis")
    @TokenCheck(required = false)
    public void generatorCodeRedis(HttpServletRequest request, HttpServletResponse response){
        
            SpecCaptcha specCaptcha = new SpecCaptcha(200,100);
            String text = specCaptcha.text();
            System.out.println("验证码:"+text);//验证码
            String id = request.getSession().getId();
            System.out.println("sessionID:"+id);
            stringRedistemplate.opsForValue().set(id, text);
        try {
            CaptchaUtil.out(specCaptcha,request, response);
        } catch (IOException e) {
            e.printstacktrace();
        }
    }
    
    @GetMapping("/verify-redis")
    @TokenCheck(required = false)
    public String verifyRedis(String verifyCode,HttpServletRequest request){
        String id = request.getSession().getId();
        String c = stringRedistemplate.opsForValue().get(id);
        System.out.println("sessionID"+id);
        Boolean aBoolean=CaptchaUtil.ver(verifyCode, request);
        if(verifyCode.equals(c)){
            CaptchaUtil.clear(request);
            return "通过";
        }
        return "不通过";
    }
    
    @GetMapping("/generatorUUid-redis")
    @TokenCheck(required = false)
    public HashMap<String, String> generatorUUidCodeRedis(HttpServletRequest request, HttpServletResponse response){
        SpecCaptcha specCaptcha = new SpecCaptcha(200,50);
        String text = specCaptcha.text();
        System.out.println("验证码:"+text);
        /*uuid 和 base64 作为key  value 返回*/
        String uuid = UUID.randomUUID().toString();
        String base64 = specCaptcha.toBase64();
        int index = base64.indexOf(",");
        String substring = base64.substring(index + 1);
        System.out.println("base64:"+substring);
       HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("uuid", uuid);//uuid 返回给前端的是截取base64图片代码
        hashMap.put("base64", substring);//uuid 返回给前端的是截取base64图片代码
        request.getSession().setAttribute("uuid", uuid);
        
        stringRedistemplate.opsForValue().set(uuid, text);//存入reids是uuid 和具体的内容结果
        return hashMap;
    }
    
    @GetMapping("/verifyUUID-redis")
    @TokenCheck(required = false)
    public String verifyUUIDRedis(String verifyCode,HttpServletRequest request){
        String uuid = request.getSession().getAttribute("uuid").toString();
        System.out.println(uuid);
        String s = stringRedistemplate.opsForValue().get(uuid);
        System.out.println(s);
    
        // Boolean aBoolean=CaptchaUtil.ver(verifyCode, request);
        if(verifyCode.equals(s)){
            CaptchaUtil.clear(request);
            return "通过";
        }
        return "不通过";
    }
}
spring:
  datasource:
    username: root
    password: root
    url: jdbc:MysqL://192.168.1.136:3306/dongbao?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    driver-class-name: com.MysqL.cj.jdbc.Driver
  profiles:
    active: dev
  redis:
    # redis数据库索引(认为0),我们使用索引为3的数据库,避免和其他数据库冲突
    database: 3
    # redis服务器地址(认为loaclhost)
    host: localhost
    # redis端口(认为6379)
    port: 6379
    # redis访问密码认为空)

    # redis连接超时时间(单位毫秒)
    timeout: 0
    # redis连接池配置
    pool:
      # 最大可用连接数(认为8,负数表示无限)
      max-active: 8
      # 最大空闲连接数(认为8,负数表示无限)
      max-idle: 8
      # 最小空闲连接数(认为0,该值只有为正数才有用)
      min-idle: 0
      # 从连接池中获取连接最大等待时间(认为-1,单位为毫秒,负数表示无限)
      max-wait: -1
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    db-config:
      logic-delete-value: 1
      logic-not-delete-value: 0
  mapper-locations:
    - classpath*:/com/msb/mapper/xml/*.xml

server:
  port: 8001

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐