package com.sandu.ximon.admin.security.authcode; import cn.hutool.core.util.StrUtil; 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.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Map; /** * @Author Nan * @Date 2022/04/25 * @Version 1.0 */ @RestController @AllArgsConstructor @RequestMapping("/authcode") public class VerifyCodeController { @RequestMapping("/getImg") @ResponseBody public void getVerifiCode(HttpServletRequest request, HttpServletResponse response) throws IOException { /* 1.生成验证码 2.把验证码上的文本存在session中 3.把验证码图片发送给客户端 */ ImageVerificationCode ivc = new ImageVerificationCode(); //用我们的验证码类,生成验证码类对象 BufferedImage image = ivc.getImage(); //获取验证码 request.getSession().setAttribute("text", ivc.getText()); //将验证码的文本存在session中 ImageVerificationCode.output(image, response.getOutputStream());//将验证码图片响应给客户端 } @RequestMapping("Login_authentication") @ResponseBody public String Login_authentication(HttpServletRequest request, HttpServletResponse response) 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"; // } return session_vcode; } }