package com.roy.controller.kcaptcha;

import com.baomidou.kaptcha.Kaptcha;
import com.baomidou.kaptcha.exception.KaptchaIncorrectException;
import com.roy.util.ImageUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * @author ：楼兰
 * @date ：Created in 2021/1/20
 * @description:
 **/

@RestController
@RequestMapping("/kcaptcha")
public class KcaptchaController {

    @Resource
    private Kaptcha kaptcha;
    //登录 验证， 这里只需要判断验证码是否正确
    @RequestMapping("/login")
    public Object login(String verifyCode,HttpServletRequest request){
        try{
            final boolean result = kaptcha.validate(verifyCode, 60);
            if(result){
                return "success";
            }else{
                return null;
            }
        }catch (KaptchaIncorrectException e){
            return null;
        }
    }

    //用来产生验证码
    @RequestMapping("/verifyCode")
    public void generateImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        kaptcha.render();
    }
}
