其他分享
首页 > 其他分享> > 验证码工具-easy-captcha

验证码工具-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

 

 

标签:redis,String,text,request,验证码,captcha,easy,import,uuid
来源: https://www.cnblogs.com/Lcch/p/16245072.html