Generate the verification code with the guide packet method

Generate the verification code with the guide packet method

Recently, I found that the guide packet method generates the verification code. It is not very novel, but there is only one point. The method of calculating the string using javascript needs to be paid attention to. It works.

Step 1: Guide package
  <!--kaptcha-->
        <dependency>
            <groupId>com.github.penggle</groupId>
            <artifactId>kaptcha</artifactId>
            <version>2.3.2</version>
        </dependency>
Step 2: The control layer outputs pictures outward
@GetMapping(value = "/verifyCode")
    @ResponseBody
    public void verifyCode(HttpServletResponse response, User user, @RequestParam("goodsId") long goodsId) {
        BufferedImage image = miaoshaOrderService.createVerifyCode(user, goodsId);
        try {
            OutputStream out = response.getOutputStream();
            ImageIO.write(image, "jpg", out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
Step 3: Generate verification code in the service layer
 // 生成验证码图片
    public BufferedImage createVerifyCode(User user, Long goodsId) {
        DefaultKaptcha defaultKaptcha = kaptchaConfig.getDefaultKaptcha();
        String text = generateText();
        redisService.set(MiaoshaOrderPrefix.verifyCode, "" + user.getId() + goodsId, eval(text));
        return defaultKaptcha.createImage(text);
    }

    // 生成验证码文本
    public String generateText() {
        char opt[] = new char[]{'+', '-', '*'};
        Random random = new Random();
        int num1 = random.nextInt(9) + 1;
        int num2 = random.nextInt(9) + 1;
        int num3 = random.nextInt(9) + 1;
        char opt1 = opt[random.nextInt(3)];
        char opt2 = opt[random.nextInt(3)];
        return "" + num1 + opt1 + num2 + opt2 + num3;
    }

    public Integer eval(String expression) {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        try {
            return (Integer) engine.eval(expression);
        } catch (ScriptException e) {
            e.printStackTrace();
            return null;
        }
    }
Step 4: Verification code configuration
@Configuration
public class KaptchaConfig {

    @Bean
    public DefaultKaptcha getDefaultKaptcha() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        properties.setProperty("kaptcha.border", "no");
        properties.setProperty("kaptcha.textproducer.char.length", "4");
        properties.setProperty("kaptcha.session.key", "code");
        //字体颜色
        properties.setProperty("kaptcha.textproducer.font.color", "red");
        //干扰线
        properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.DefaultNoise");
        //干扰线颜色
        properties.setProperty("kaptcha.noise.color", "red");
        properties.setProperty("kaptcha.textproducer.char.space", "4");
        
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}

It should be noted here: script support is embedded. That is, the eval () function here is a function for calculating a character string. You can pay attention, so that you can use it later.

Published 67 original articles · Liked12 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/m0_37635053/article/details/105365313