This example shares the specific code for Springboot verification code login for your reference. The specific content is as follows
Because verification codes are needed in the project, I will summarize how to quickly solve project requirements in the project~ verification code. The following is a recommended example of verification codes for everyone to get started.
1. Write verification code tools
import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.Random;import javax.imageio.ImageIO;/** * @author zct * @date February 6, 2018* @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', '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. Use the controller layer
Verify that the username and password are consistent with the verification code
/** * Login portal* * @param username Username* @param password Password* @param code Verification code* @param response The callback json data successfully returns 200, and the failed return 500 */ @ApiOperation("Login") @PostMapping("/login") public void adminLoginByPasswword(@ApiParam("Username") @RequestParam String username, @ApiParam("Password") @RequestParam String password, @ApiParam("Verification code") @RequestParam String code, HttpServletResponse response,HttpServletRequest request) { HttpSession session=request.getSession(); if(session.getAttribute("imageCode")==null){ renderFail(response, "Reget verification code"); }else { if(session.getAttribute("imageCode").toString().equalsIgnoreCase(code)){ Map<String, Object> user = adminService.checkAdminLogin(username, password); if (user == null) { renderFail(response, "Login failed"); } else { renderSuccess(response, "Login succeeded"); } }else { renderFail(response, "Verification code error"); } } }Here, get request is used to obtain the verification code. The interface to obtain the verification code is as follows
@ApiOperation("Generate verification code") @GetMapping("/getcode") public void getCode(HttpServletResponse response, HttpServletRequest request) throws Exception{ HttpSession session=request.getSession(); //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 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. Code testing
Here is a test using springboot Swagger2
The above is the get request to obtain the verification code, the following is the login verification, the verification result is successful.
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.