java如何实现图形验证码呢?
下文笔者讲述使用java代码实现图形验证码的方法分享,如下所示
Java生成图形验证码的方法
图形验证码实现思路:
1.引入相应的jar包 easy-captcha
2.调用 CaptchaUtil.out即可生成验证码
同时验证码会存储在session(captcha)中
注意事项:
生成的验证码会存储到Session中,如下源码所示
easy-captcha部分源码分析
public class CaptchaUtil {
...
public static void out(Captcha captcha, HttpServletRequest request, HttpServletResponse response) throws IOException {
setHeader(response);
request.getSession().setAttribute("captcha", captcha.text().toLowerCase());
captcha.out(response.getOutputStream());
}
...
}
例:生成图形验证码
//引入相应依赖
pom.xml
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
//springmvc中使用
@Controller
public class CaptchaController {
@RequestMapping("/captcha")
public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
//生成图形验证码
CaptchaUtil.out(request, response);
}
}
//前端html代码
<img src="/captcha" width="130px" height="48px" />
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


