Recently, I have been busy working on the final project. At present, I have achieved the function of user message board to prevent users from submitting messages maliciously, so I plan to add the verification code function. The following is to generate a simple verification code, which can be used by copying the code!

The front-end effect is as follows:

 SSM generates a simple verification code and integrates it into the project

Front end jsp page code:

 <div class="layui-form-item"> <label class="layui form label">Verification code</label> <div class="layui-input-inline"> <input type="text" name="inputCode" lay verify="required" placeholder="Please enter a verification code" class="layui input"> </div> <img src="Fill in the verification code generation interface here" alt="" width="100" height="38px" class="passcode" style="height: 38px; cursor: pointer;" onclick="this. src=this. src+'?'"> </div>

The backend first creates a tool class for generating verification codes: RandomValidateCode.class

 /** * @author lcry * @create 2018/12/29 */ package com.lcry.util; import javax.imageio. ImageIO; import javax.servlet.http. HttpServletRequest; import javax.servlet.http. HttpServletResponse; import javax.servlet.http. HttpSession; import java.awt.*; import java.awt.image. BufferedImage; import java.util. Random; /** *Verification code tool class * Created on 2018/12/29. * * @author lcry */ public class RandomValidateCode { public static final String RANDOMCODEKEY = "randomcode_key"; //The key placed in the session, which is then taken from the session and compared with the one entered by the user private Random random = new Random(); private String randString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";// Random string private int width = 80;// Picture width private int height = 26;// Picture high private int lineSize = 40;// Number of interference lines private int stringNum = 4;// Number of randomly generated characters /** *Generate random pictures */ public void getRandcode(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); //BufferedImage class is an Image class with a buffer, and Image class is a class used to describe image information BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); //The Graphics object that generates the Image object, and the modified object can perform various drawing operations on the image Graphics g = image.getGraphics(); g.fillRect(0, 0, width, height); g.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE, 18)); g.setColor(getRandColor(160, 200)); //Draw interference line for (int i = 0; i <= lineSize; i++) { drowLine(g); } //Draw random characters String randomString = ""; for (int i = 1; i <= stringNum; i++) { randomString = drowString(g, randomString, i); } session.removeAttribute(RANDOMCODEKEY); session.setAttribute(RANDOMCODEKEY, randomString); g.dispose(); try { //Output the pictures in memory to the client in flowing form ImageIO.write(image, "JPEG", response.getOutputStream()); } catch (Exception e) { e.printStackTrace(); } } /* *Get Font */ private Font getFont() { return new Font("Fixedsys", Font.CENTER_BASELINE, 18); } /* *Get Color */ private Color getRandColor(int fc, int bc) { if (fc > 255) fc = 255; if (bc > 255) bc = 255; int r = fc + random.nextInt(bc - fc - 16); int g = fc + random.nextInt(bc - fc - 14); int b = fc + random.nextInt(bc - fc - 18); return new Color(r, g, b); } /* *Draw String */ private String drowString(Graphics g, String randomString, int i) { g.setFont(getFont()); g.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121))); String rand = String.valueOf(getRandomString(random.nextInt(randString.length()))); randomString += rand; g.translate(random.nextInt(3), random.nextInt(3)); g.drawString(rand, 13 * i, 16); return randomString; } /* *Draw interference line */ private void drowLine(Graphics g) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(13); int yl = random.nextInt(15); g.drawLine(x, y, x + xl, y + yl); } /* *Get random characters */ public String getRandomString(int num) { return String.valueOf(randString.charAt(num)); } }

Verification code generation interface

 /** *Get the generated verification code and display it in the UI interface * * @param request * @param response * @throws ServletException * @throws IOException */ @RequestMapping(value = "/checkCode") public void checkCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Set the corresponding type and tell the browser that the output content is a picture response.setContentType("image/jpeg"); //Set the response header information to tell the browser not to cache this content response.setHeader("pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expire", 0); RandomValidateCode randomValidateCode = new RandomValidateCode(); try { randomValidateCode.getRandcode(request, response);// Output picture method } catch (Exception e) { e.printStackTrace(); } }

Finally, use it in the project (only paste the core code):

 @RequestMapping(value = "addureq") @ResponseBody public String insertureq(Mureq mureq, HttpServletResponse response, HttpServletRequest req, @RequestParam(value = "inputCode") String inputCode) throws Exception { //Get the verification code stored in the session String code = (String) req.getSession().getAttribute("randomcode_key"); System. out. println ("verification code comparison"+code+inputCode); //Get the verification code submitted by the page JSONObject result = new JSONObject(); If (code. toLowerCase(). equals (inputCode. toLowerCase())) {//The verification code is not case sensitive boolean istrue = mureqService.insertreq(mureq); if (istrue) { Result. put ("msg", "Message Succeeded"); } else { Result. put ("msg", "Message failed"); } }Else {//Verification failed Result. put ("msg", "Verification code error"); } ResponseUtil.write(response, result); return null; }

Reference link:
https://blog.csdn.net/zys15256630193/article/details/80518800
https://blog.csdn.net/sinat_32133675/article/details/77247892
https://blog.csdn.net/yhflyl/article/details/80944684