验证码生成
本效果是利用easy-captcha工具包实现,首先需要添加相关依赖到pom.xml中,代码如下:
com.github.whvcse easy-captcha 1.6.2
登录后复制
验证码格式
easy-captcha验证码工具支持GIF、中文、算术等类型,分别通过下面几个实例对象实现:
SpecCaptcha(PNG类型的静态图片验证码)
GifCaptcha(Gif类型的图片验证码)
ChineseCaptcha(GIF类型中文图片验证码)
立即学习“Java免费学习笔记(深入)”;
ArithmeticCaptcha(算术类型的图片验证码)
字符类型分为以下几种:
TYPE_DEFAULT:数字和字母混合
TYPEONLYNUMBER:纯数字
TYPEONLYCHAR:纯字母
TYPEONLYUPPER:纯大写字母
TYPEONLYLOWER:纯小写字母
TYPENUMAND_UPPER:数字和大写字母混合
后端逻辑的实现
package com.yanx.controller; import com.wf.captcha.SpecCaptcha;import com.wf.captcha.base.Captcha;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.thymeleaf.util.StringUtils; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException; @Controllerpublic class KapchaController { @GetMapping("/kaptcha") public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException { httpServletResponse.setHeader("Cache-Control","no-store"); httpServletResponse.setHeader("Pragma","no-cache"); httpServletResponse.setDateHeader("Expires",0); httpServletResponse.setContentType("image/gif"); //三个参数分别为宽、高、位数 SpecCaptcha captcha=new SpecCaptcha(75,30,4); //设置类型为数字和字母混合 captcha.setCharType(Captcha.TYPE_DEFAULT); //设置字体 captcha.setCharType(Captcha.FONT_9); //验证码存入session httpServletRequest.getSession().setAttribute("verifyCode",captcha.text().toLowerCase()); //输出图片流 captcha.out(httpServletResponse.getOutputStream()); } }
登录后复制
这里控制器新增了defaultKaptcha()方法,该方法所拦截处理的路径为/kaptcha
前端逻辑的实现
在static目录中新建kaptcha.html页面,代码如下:
nbsp;html>验证码
登录后复制
访问后端验证码路径/kaptcha,验证码为图片形式。onclick方法为点击该标签时可以动态切换显示验证码。
启动Spring Boot项目,打开浏览器输入地址:
http://localhost:8080/kaptcha.html
效果如下:
验证码验证
后端代码
package com.yanx.controller; import com.wf.captcha.SpecCaptcha;import com.wf.captcha.base.Captcha;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.thymeleaf.util.StringUtils; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.io.IOException; @Controllerpublic class KapchaController { @GetMapping("/kaptcha") public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException { httpServletResponse.setHeader("Cache-Control","no-store"); httpServletResponse.setHeader("Pragma","no-cache"); httpServletResponse.setDateHeader("Expires",0); httpServletResponse.setContentType("image/gif"); //三个参数分别为宽、高、位数 SpecCaptcha captcha=new SpecCaptcha(75,30,4); //设置类型为数字和字母混合 captcha.setCharType(Captcha.TYPE_DEFAULT); //设置字体 captcha.setCharType(Captcha.FONT_9); //验证码存入session httpServletRequest.getSession().setAttribute("verifyCode",captcha.text().toLowerCase()); //输出图片流 captcha.out(httpServletResponse.getOutputStream()); } @GetMapping("/verify") @ResponseBody public String verify(@RequestParam("code") String code, HttpSession session){ if(StringUtils.isEmpty(code)){ return "验证码不能为空"; } String kapchaCode = session.getAttribute("verifyCode")+""; if(StringUtils.isEmpty(kapchaCode)||!code.toLowerCase().equals(kapchaCode)){ return "验证码输入错误"; } return "验证成功"; }}
登录后复制
前端代码
nbsp;html>验证码验证 $(function(){ //验证按钮点击事件 $('#verify').click(function(){ var code=$('#code').val(); $.ajax({ type:'GET',//方法类型 url:'/verify?code='+code, success:function(result){ $('#verifyResult').html(result); }, error:function(){ alert('请求失败'); }, }); }); });
登录后复制
效果
以上就是怎么使用java搞定网站登录验证码的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2627237.html