Spring Boot is a new framework provided by the Pivotal team. It is designed to simplify the initial construction and development process of new Spring applications. The framework uses a specific way to configure it, so that developers no longer need to define boilerplate configurations. In this way, Spring Boot is committed to becoming a leader in the booming rapid application development.
The following is an example code to introduce the verification code function of spring boot to you. The specific details are as follows:
1. Create a tool class and configure verification code-related parameters
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.util.Random; /** * @author ld * @date November 6, 2017* @param * @desc Graphic verification code generation* */ public class VerifyUtil { // Verification code character set private static final char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; // Number of characters private static final int SIZE = 4; // Number of interference lines private static final int LINES = 5; // Width private static final int WIDTH = 80; // Height private static final int HEIGHT = 40; // Font size private static final int FONT_SIZE = 30; /** * Generate random verification code and picture* Object[0]: verification code string; * Object[1]: verification code picture. */ public static Object[] createImage() { StringBuffer sb = new StringBuffer(); // 1. Create a blank image BufferedImage image = new BufferedImage( WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); // 2. Get the image brush Graphics graphic = image.getGraphics(); // 3. Set the brush color graphics.setColor(Color.LIGHT_GRAY); // 4. Draw the rectangle background graphics.fillRect(0, 0, WIDTH, HEIGHT); // 5. Draw random characters Random ran = new Random(); for (int i = 0; i <SIZE; i++) { // Take the random character index int n = ran.nextInt(chars.length); // Set the random color graphic.setColor(getRandomColor()); // Set the font size graphic.setFont(new Font( null, Font.BOLD + Font.ITALIC, FONT_SIZE)); // Draw character graphic.drawString( chars[n] + "", i * WIDTH / SIZE, HEIGHT*2/3); // Record characters sb.append(chars[n]); } // 6. Draw the interference line for (int i = 0; i < LINES; i++) { // Set random color graphic.setColor(getRandomColor()); // Random color graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT), ran.nextInt(WIDTH), ran.nextInt(HEIGHT)); } // 7. Return verification code and image return new Object[]{sb.toString(), image}; } /** * Random color*/ public static Color getRandomColor() { Random ran = new Random(); Color color = new Color(ran.nextInt(256), ran.nextInt(256), ran.nextInt(256)); return color; } }2. Interface
@RequestMapping(value="/createValicode",method=RequestMethod.GET) public void valicode(HttpServletResponse response,HttpSession session) throws Exception{ //Use image tools to generate images//The first parameter is the generated verification code, and the second parameter is the generated image Object[] objs = VerifyUtil.createImage(); //Store the verification code into Session session.setAttribute("imageCode",objs[0]); //Output the image to the browser BufferedImage image = (BufferedImage) objs[1]; response.setContentType("image/png"); OutputStream os = response.getOutputStream(); ImageIO.write(image, "png", os); }3. Test page call
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8" /> <title>hello</title> </head> <body> <h1 th:text="${info}" /> <div> <!-- <img src="/img/001.png"/> --> <img onclick = "this.src='/iot-frame/createValicode?' + Math.floor(Math.random() * 100)" src="/iot-frame/createValicode" /> </div> <form action="imgvrifyControllerDefaultKaptcha"> <input type="text" name="vrifyCode" /> <input type="submit" value="submit"></input> </form> </body> </html>Summarize
The above is the spring boot implementation verification code function introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!