Spring boot 实现验证码

本文介绍了如何在Spring Boot中实现验证码功能。通过`image()`方法生成验证码,设置HTTP响应头,然后在HTML中展示。`authImage()`方法用于获取验证码图片,使用`VerifyCodeUtils`工具类生成随机字串并保存到session中,最后`validImage()`方法校验输入的验证码,确保其有效性和时效性。
摘要由CSDN通过智能技术生成

public String image() {

return “verify_code”;

}

1.2 html

Title

<label

style=“color:black;”>看不清?

验证

1.3.获取验证码图片

@RequestMapping(value=“image”,method=RequestMethod.GET)

public void authImage() throws IOException {

response.setHeader(“Pragma”, “No-cache”);

response.setHeader(“Cache-Control”, “no-cache”);

response.setDateHeader(“Expires”, 0);

response.setContentType(“image/jpeg”);

// 生成随机字串

String verifyCode = VerifyCodeUtils.generateVerifyCode(4);

session.removeAttribute(“verCode”);

session.removeAttribute(“codeTime”);

session.setAttribute(“verCode”, verifyCode.toLowerCase());

session.setAttribute(“codeTime”, LocalDateTime.now());

// 生成图片

int w = 100, h = 30;

OutputStream out = response.getOutputStream();

VerifyCodeUtils.outputImage(w, h, out, verifyCode);

}

1.4 核对验证码

@RequestMapping(“valid”)

@ResponseBody

public Wrapper validImage(String code, @SessionAttribute(“verCode”) Object verCode){

if (null == verCode) {

request.setAttribute(“error”, “验证码已失效,请重新输入”);

return WrapMapper.wrap(Wrapper.ERROR_CODE,“验证码已失效,请重新输入”);

}

String verCodeStr = verCode.toString();

LocalDateTime localDateTime = (LocalDateTime)session.getAttribute(“codeTime”);

long past = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();

long now = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();

if(verCodeStr == null || code == null || code.isEmpty() || !verCodeStr.equalsIgnoreCase(code)){ </

  • 18
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Boot实现登录图形验证码,可以按照以下步骤进行: 1. 添加依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>com.github.axet</groupId> <artifactId>kaptcha</artifactId> <version>0.0.9</version> </dependency> ``` 2. 配置Kaptcha 在application.properties文件中添加以下配置: ``` #Kaptcha验证码配置 kaptcha.border=no kaptcha.textproducer.font.color=black kaptcha.textproducer.char.space=5 kaptcha.image.width=160 kaptcha.image.height=60 kaptcha.textproducer.char.length=4 kaptcha.textproducer.font.size=30 ``` 3. 创建验证码接口 创建一个验证码接口,用于生成和返回验证码图片。例如: ``` @RestController @RequestMapping("/captcha") public class CaptchaController { @Autowired private Producer kaptchaProducer; @GetMapping("/image") public void getCaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setDateHeader("Expires", 0); // Set standard HTTP/1.1 no-cache headers. response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); // Set IE extended HTTP/1.1 no-cache headers (use addHeader). response.addHeader("Cache-Control", "post-check=0, pre-check=0"); // Set standard HTTP/1.0 no-cache header. response.setHeader("Pragma", "no-cache"); // return a jpeg response.setContentType("image/jpeg"); // create the text for the image String capText = kaptchaProducer.createText(); // store the text in the session request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText); // create the image with the text BufferedImage bi = kaptchaProducer.createImage(capText); ServletOutputStream out = response.getOutputStream(); // write the data out ImageIO.write(bi, "jpg", out); try { out.flush(); } finally { out.close(); } } } ``` 4. 校验验证码 在登录接口中校验验证码。例如: ``` @RestController @RequestMapping("/login") public class LoginController { @PostMapping("/auth") public ResultBean<String> doLogin(@RequestParam String username, @RequestParam String password, @RequestParam String captcha, HttpServletRequest request) { String kaptcha = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY); if (StringUtils.isBlank(kaptcha) || !kaptcha.equalsIgnoreCase(captcha)) { return ResultBean.failed("验证码错误!"); } // TODO: 登录验证逻辑 return ResultBean.success("登录成功!"); } } ``` 这样就可以在Spring Boot实现登录图形验证码了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值