| | |
| | | package com.sandu.ximon.admin.security.authcode; |
| | | |
| | | import cn.hutool.core.lang.Snowflake; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.baomidou.mybatisplus.core.toolkit.StringUtils; |
| | | import com.sandu.common.security.annotation.AnonymousAccess; |
| | | import com.sandu.ximon.admin.utils.RedisUtils; |
| | |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.ResponseBody; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import sun.misc.BASE64Encoder; |
| | | |
| | | import javax.imageio.ImageIO; |
| | | import javax.servlet.ServletException; |
| | |
| | | 3.把验证码图片发送给客户端 |
| | | */ |
| | | long key = snowflake.nextId(); |
| | | ImageVerificationCode ivc = new ImageVerificationCode(); //用我们的验证码类,生成验证码类对象 |
| | | ImageVerificationCode ivc = new ImageVerificationCode(); //生成验证码类对象 |
| | | BufferedImage image = ivc.getImage(); //获取验证码 |
| | | System.out.println(ivc.getText()+"----加密前的验证码"); |
| | | RedisUtils.getBean().set(key+"",MD5Util.md5(ivc.getText().toUpperCase()),60); |
| | | 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; |
| | | } |
| | | |
| | | @AnonymousAccess |
| | | @RequestMapping("/Login_authentication/{key}") |
| | | @ResponseBody |
| | | public String Login_authentication(HttpServletRequest request, HttpServletResponse response,@PathVariable String key) 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"); r6mt |
| | | // if(vcode.isEmpty()||vcode==null){ |
| | | // return "验证码不能为空"; |
| | | // } |
| | | |
| | | String session_vcode = (String) request.getSession().getAttribute(key); //从session中获取真正的验证码 |
| | | //比较输入的验证码和真正的验证码 |
| | | // if(StrUtil.equalsIgnoreCase(session_vcode, vcode)){ |
| | | // return "true"; |
| | | // } |
| | | return session_vcode; |
| | | } |
| | | |
| | | /** |
| | | * BufferedImage转成 base64 |
| | | * BufferedImage转成 base64 |
| | | * |
| | | * @param bufferedImage |
| | | * @param imageFormatName |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | public static String getBufferedImageToBase64(BufferedImage bufferedImage,String imageFormatName) throws IOException { |
| | | if(StringUtils.isBlank(imageFormatName)){ |
| | | public static String getBufferedImageToBase64(BufferedImage bufferedImage, String imageFormatName) throws IOException { |
| | | if (StringUtils.isBlank(imageFormatName)) { |
| | | imageFormatName = "JPEG"; |
| | | } |
| | | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |