package com.roy.jcaptcha;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

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

@RestController
@RequestMapping("/jcaptcha/")
public class JCaptchaController {

//    @PostMapping("/login")
//    public Object login(String verifyCode, HttpServletRequest request){
//        Boolean result = JCaptchaUtil.getService().validateResponseForID(request.getSession().getId(), verifyCode);
//        if(result){
//            return "login";
//        }else{
//            return null;
//        }
//    }
//
//
//    @GetMapping("/verifyCode")
//    public void generateImg(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//        byte[] captChallengeAsJpeg = null;
//
//        ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
//
//        String captchaId = req.getSession().getId();
//        BufferedImage challenge = JCaptchaUtil.getService().getImageChallengeForID(captchaId,req.getLocale());
//
//        JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(jpegOutputStream);
//        jpegEncoder.encode(challenge);
//
//        captChallengeAsJpeg = jpegOutputStream.toByteArray();
//
//        resp.setHeader("Cache-Control", "no-store");
//        resp.setHeader("Pragma", "no-cache");
//        resp.setDateHeader("Expires", 0);
//        resp.setContentType("image/jpeg");
//
//        ServletOutputStream respOutputStream = resp.getOutputStream();
//        respOutputStream.write(captChallengeAsJpeg);
//        respOutputStream.flush();
//        respOutputStream.close();
//    }
}
