springboot implements verification code generation and verification functions

1.springboot integrates hutool-captcha to generate verification codes. First, add dependencies:

<dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-captcha</artifactId>
            <version>${hutool-captcha.version}</version>
        </dependency>

2. Generate and display verification code 

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import ltd.newbee.mall.common.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 */
@Controller
public class CommonController {

    @GetMapping("/common/kaptcha")
    public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        httpServletResponse.setHeader("Cache-Control", "no-store");
        httpServletResponse.setHeader("Pragma", "no-cache");
        httpServletResponse.setDateHeader("Expires", 0);
        httpServletResponse.setContentType("image/png");

        ShearCaptcha shearCaptcha= CaptchaUtil.createShearCaptcha(150, 30, 4, 2);

        // 验证码存入session
        httpServletRequest.getSession().setAttribute("verifyCode", shearCaptcha);

        // 输出图片流
        shearCaptcha.write(httpServletResponse.getOutputStream());
    }

    @GetMapping("/common/mall/kaptcha")
    public void mallKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        httpServletResponse.setHeader("Cache-Control", "no-store");
        httpServletResponse.setHeader("Pragma", "no-cache");
        httpServletResponse.setDateHeader("Expires", 0);
        httpServletResponse.setContentType("image/png");

        ShearCaptcha shearCaptcha= CaptchaUtil.createShearCaptcha(110, 40, 4, 2);

        // 验证码存入session
        httpServletRequest.getSession().setAttribute(Constants.MALL_VERIFY_CODE_KEY, shearCaptcha);

        // 输出图片流
        shearCaptcha.write(httpServletResponse.getOutputStream());
    }
}

3. Front-end logic implementation

<!Do.. html>
<html>
<head>
<emta charset=""/>
<title>..
</head>
<bode>
<img src = "/kaptcha" onclick="this.src='/kaptcha?d = '+new Date()*1" />
</body>
</html>

4. Input verification of verification code

The general approach is to save the currently generated verification code content after the backend generates the verification code. You can choose to save it in the session, in the redis cache, or in the database. Then return the verification code image and display it to the front-end page. After the user enters the verification code and sends a verification request to the backend, the backend will verify the verification code entered by the user after receiving the request.

@GetMapping("/verify")
@ResponseBody
public String verify(@RequestParam("code") String code,HttpSession session ) {
        if(!StringUtils.hasLength(code)){
                return "验证码不能为空";
}
        String kaptchaCode = session.getAttribute("verifyCode")+"";
        if(!StringUtils.hasLength(kaptchaCode) || !code.toLowerCase().equals(kaptchaCode)){
                return "验证码错误";
}
                return“验证成功”;
}

5. Front-end logic implementation

omission....

Guess you like

Origin blog.csdn.net/qq_35207086/article/details/128462605