验证码的生成以及使用
工具类:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.util.concurrent.TimeUnit; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * 生成验证码工具 * @Author ChenWenChao */ @RestController @RequestMapping("/code") public class CodeUtils { @Autowired private RedisTemplate redisTemplate; /** * 生成验证码 * @param request * @param response * @throws Exception */ @GetMapping("/verificationCode") public void verificationCode(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setContentType("image/jpeg"); HttpSession session = request.getSession(); int width = 60; int height = 20; //设置浏览器不要缓存此图片 response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); //创建内存图像并获得其图形上下文 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); //产生随机验证码 //定义验证码的字符表(去除01OI降低输错率) String chars = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"; char[] rands = new char[4]; for (int i = 0; i < 4; i++) { int rand = (int) (Math.random() * 32); rands[i] = chars.charAt(rand); } //产生图像 //画背景 g.setColor(new Color(0xDCDCDC)); g.fillRect(0, 0, width, height); //随机产生120个干扰点 for (int i = 0; i < 120; i++) { int x = (int) (Math.random() * width); int y = (int) (Math.random() * height); int red = (int) (Math.random() * 255); int green = (int) (Math.random() * 255); int blue = (int) (Math.random() * 255); g.setColor(new Color(red, green, blue)); g.drawOval(x, y, 1, 0); } g.setColor(Color.BLACK); g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 18)); //在不同的高度上输出验证码的不同字符 g.drawString("" + rands[0], 1, 17); g.drawString("" + rands[1], 16, 15); g.drawString("" + rands[2], 31, 18); g.drawString("" + rands[3], 46, 16); g.dispose(); //将图像输出到客户端 ServletOutputStream sos = response.getOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image, "JPEG", baos); byte[] buffer = baos.toByteArray(); response.setContentLength(buffer.length); sos.write(buffer); baos.close(); sos.close(); //将验证码放到session中 //session.setAttribute("verificationCode", new String(rands)); //将验证码缓存到redis中(键名用ip来区分) redisTemplate.opsForValue().set("verification_code:" + IpUtils.getRealIp(request), new String(rands), 600L, TimeUnit.SECONDS); } }
用法:
<img onclick="this.src='/code/verificationCode'+'?t='+new Date().getTime()" src="/code/verificationCode"/>
@Autowired private RedisTemplate<Object, Object> redisTemplate; //以下代码在登录方法里调用 //String verificationCode = (String) request.getSession().getAttribute("verificationCode"); String verificationCode ="0000"; if (redisTemplate.hasKey("verification_code:" + IpUtils.getRealIp(request))) { verificationCode = (String) redisTemplate.opsForValue().get("verification_code:" + IpUtils.getRealIp(request)); } if (!code.equalsIgnoreCase(verificationCode)) { //抛的异常大家根据自己公司封装的自定义异常来使用 throw new ResultInfo(ResultUtils.error("验证码错误","")); }