登录验证码
有很多种实现形式,如自定义类UsernamePasswordAuthenticationFilter(默认的用户认证过滤器)、在UsernamePasswordAuthenticationFilter前添加验证码拦截器等。
下面通过第二种实现,在UsernamePasswordAuthenticationFilter前添加验证码拦截器,如果验证码不通过,那么不继续后面的认证。
验证码实现接口
通过hutool插件实现图片验证码,并将验证码写入到session中,key为verifycode,后面校验时需要从session的key中获取验证码真实内容。
@RequestMapping("/getver")
@ResponseBody
public void verifycode(HttpServletResponse response, HttpSession session) {
LineCaptcha captcha = CaptchaUtil.createLineCaptcha(100, 30, 4, 0);
session.setAttribute("verifycode", captcha.getCode());
System.out.println(session);
System.out.println(captcha.getCode());
SecurityContextHolder.clearContext();
try {
ServletOutputStream outputStream = response.getOutputStream();
captcha.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (IOExcept