This article has shared the specific code of the javaweb login verification code for your reference. The specific content is as follows
use:
Controller: Generate verification code
@RequestMapping("/user/check.jpg") public void createCode(HttpServletRequest request, HttpServletResponse response) throws IOException { // Notify the browser not to cache response.setHeader("Expires", "-1"); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "-1"); CaptchaUtil util = CaptchaUtil.Instance(); // Input the verification code into the session to verify String code = util.getString(); request.getSession().setAttribute("code", code); // Output the web page ImageIO.write(util.getImage(), "jpg", response.getOutputStream()); } jsp: Show verification code
<img id="img" src="<%=basePath%>user/check.jpg" onclick="refresh()">
function refresh() { var url = $("#basePath").val() + "user/check.jpg?number="+Math.random(); $("#img").attr("src",url); } verify:
Get whether the code in the session is consistent with the code sent back in the foreground
/** * Verification code verification* * @param session * @param code */ private void checkCode(HttpSession session, String code) { String codeSession = (String) session.getAttribute("code"); if (StringUtils.isEmpty(codeSession)) { log.error("No verification code information was generated"); throw new IllegalStateException("ERR-01000"); } if (StringUtils.isEmpty(code)) { log.error("No verification code information was filled in"); throw new BusinessException("ERR-06018"); } if (codeSession.equalsIgnoreCase(code)) { // The verification code is passed} else { log.error("verification code error"); throw new BusinessException("ERR-06019"); } } Tools:
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.util.Random; /** * Verification code generation tool* @author HXL * */ public class CaptchaUtil { private BufferedImage image;// Image private String str;// Verification code private static char code[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789".toCharArray(); public static final String SESSION_CODE_NAME="code"; private CaptchaUtil() { init();// Initialization property} /* * Get RandomNumUtil instance*/ public static CaptchaUtil Instance() { return new CaptchaUtil(); } /* * Get verification code picture*/ public BufferedImage getImage() { return this.image; } /* * Get the verification code for the image*/ public String getString() { return this.str; } private void init() { // Create image int width = 85, height = 20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // Get the graphical context Graphics g = image.getGraphics(); // Generate random random = new Random(); // Set background color g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); // Set the font g.setFont(new Font("Times New Roman", Font.PLAIN, 18)); // Randomly generate 155 interference lines, making the authentication code in the image less likely to be detected by other programs g.setColor(getRandColor(160, 200)); for (int i = 0; i < 155; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); } // Take the randomly generated authentication code (4-digit number) String sRand = ""; for (int i = 0; i < 4; i++) { String rand = String.valueOf(code[random.nextInt(code.length)]); sRand += rand; // Show the authentication code in the image g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110))); // The color of the call function is the same, maybe because the seed is too close, so you can only directly generate g.drawString(rand, 13 * i + 6, 16); } // Assignment verification code this.str = sRand; // Image takes effect g.dispose(); // ByteArrayInputStream input = null; // ByteArrayOutputStream output = new ByteArrayOutputStream(); // try { // ImageOutputStream imageOut = ImageIO.createImageOutputStream(output); // ImageIO.write(image, "JPEG", imageOut); // imageOut.close(); // input = new ByteArrayInputStream(output.toByteArray()); // } catch (Exception e) { // System.out.println("Error occurred when the verification code image is generated: " + e.toString()); // } // this.image = input this.image = image;/* Assign image*/ } /* * Get random color for a given range*/ private Color getRandColor(int fc, int bc) { Random random = new Random(); if (fc > 255) fc = 255; if (bc > 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); } } Finally, let’s explain: The login interceptor must release the path to generate the verification code! Login interceptor must release the path to generate verification code! ! Login interceptor must release the path to generate verification code! ! ! Say important things three times ~~
Last display:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.