java 前端验证码生成

添加依赖

    	<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <version>RELEASEversion>
            <scope>compilescope>
        dependency>
                
     
        <dependency>
            <groupId>cn.hutoolgroupId>
            <artifactId>hutool-allartifactId>
            <version>5.3.5version>
        dependency>

接口实现

@Slf4j
@RestController
public class TestController {

    @GetMapping("/sys/getCode")
    public void getCode(HttpServletResponse response, HttpSession session){
        //HuTool定义图形验证码的长和宽,验证码的位数,干扰线的条数
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36,4,50);
        //将验证码放入session
        session.setAttribute("code",lineCaptcha.getCode());
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            lineCaptcha.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            log.error ("{}", e );
        }
    }
}

访问效果

java 前端验证码生成_第1张图片
java 前端验证码生成_第2张图片

你可能感兴趣的:(Java,个人笔记,java,spring,spring,boot)