2021与蓝度共同重构项目,服务端
liuhaonan
2022-11-07 ddfbc40f9ca8546a2c34865abef51630f054d5e9
ximon-admin/src/main/java/com/sandu/ximon/admin/security/authcode/VerifyCodeController.java
@@ -1,17 +1,24 @@
package com.sandu.ximon.admin.security.authcode;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sandu.common.security.annotation.AnonymousAccess;
import com.sandu.ximon.admin.utils.RedisUtils;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
/**
@@ -24,34 +31,58 @@
@RequestMapping("/v1/authcode")
public class VerifyCodeController {
    private final Snowflake snowflake;
//    private final RedisUtils redisUtils;
    @AnonymousAccess
    @RequestMapping("/getImg")
    @ResponseBody
    public void getVerifiCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
    public Map getVerifiCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        /*
             1.生成验证码
             2.把验证码上的文本存在session中
             3.把验证码图片发送给客户端
             */
        ImageVerificationCode ivc = new ImageVerificationCode();     //用我们的验证码类,生成验证码类对象
        long key = snowflake.nextId();
        ImageVerificationCode ivc = new ImageVerificationCode();     //生成验证码类对象
        BufferedImage image = ivc.getImage();  //获取验证码
        request.getSession().setAttribute("text", ivc.getText()); //将验证码的文本存在session中
        ImageVerificationCode.output(image, response.getOutputStream());//将验证码图片响应给客户端
        System.out.println(ivc.getText() + "----加密前的验证码");
        RedisUtils.getBean().set(key + "", MD5Util.md5(ivc.getText().toUpperCase()), 60);
        String img = getBufferedImageToBase64(image, "JPEG");
        Map map = new HashMap();
        map.put("img", img);
        map.put("key", key);
        return map;
//        return image;
    }
    @RequestMapping("Login_authentication")
    @AnonymousAccess
    @RequestMapping("/Login_authentication/{key}")
    @ResponseBody
    public String Login_authentication(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    public String Login_authentication(HttpServletRequest request, HttpServletResponse response, @PathVariable String key) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
//        String vcode = (String)map.get("session_vcode");
//        if(vcode.isEmpty()||vcode==null){
//            return "验证码不能为空";
//        }
        String session_vcode = (String) request.getSession().getAttribute("text");    //从session中获取真正的验证码
        //比较输入的验证码和真正的验证码
//        if(StrUtil.equalsIgnoreCase(session_vcode, vcode)){
//            return "true";
//        }
        String session_vcode = (String) request.getSession().getAttribute(key);    //从session中获取真正的验证码
        return session_vcode;
    }
    /**
     * BufferedImage转成 base64
     *
     * @param bufferedImage
     * @param imageFormatName
     * @return
     * @throws IOException
     */
    public static String getBufferedImageToBase64(BufferedImage bufferedImage, String imageFormatName) throws IOException {
        if (StringUtils.isBlank(imageFormatName)) {
            imageFormatName = "JPEG";
        }
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, imageFormatName, stream);
        String s = Base64.getEncoder().encodeToString(stream.toByteArray());
        return s;
    }
}