引入依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.5</version>
</dependency>
protected void captcha(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
LineCaptcha lineCaptcha = new LineCaptcha(260, 100, 4, 5);
String code = lineCaptcha.getCode();
//将验证码存入 session
req.getSession().setAttribute("captcha",code);
//将验证码图片响应给前端页面
ServletOutputStream outputStream = resp.getOutputStream();
lineCaptcha.write(outputStream);
outputStream.close();
}