kaptcha会员注册和登录:
第一步,在Maven仓库中引入Kaptcha依赖(注意将新的jar,进行发布)
第二步,在applicationContext.xml文件中配置Kaptcha
第三步,创建Controller类,生成Kaptcha代码,并输出到前端
提示:以下是本篇文章正文内容,下面案例可供参考
使用kaptcha生成验证码的作用:进行人机校验--防止机器脚本,自动大量注册用户。
1.Kaptcha是谷歌开源的可高度配置的实用验证码生成工具。
2.通过Kaptcha可阻拦大多数机器人脚本操作。
3.kaptcha典型殷勇于注册、登录、重要信息提交等用户交互
com.github.penggle
kaptcha
2.3.2
no
120
blue
40
4
@Controller
public class KaptchaController {
@Resource
private Producer kaptchaProducer;//Producer是一个接口,属性名和applicationContext.xml里面的配置文件里面一致
@GetMapping("/verify_code")
public void createVerifyCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
//响应立即过期
response.setDateHeader("Expires",0);
//不缓存任何图片数据
response.setHeader("Cache-Control" , "no-store,no-cache,must-revalidate");
response.setHeader("Cache-Control" , "post-check=0,pre-check=0");
response.setHeader("Pragma" , "no-cache");
response.setContentType("image/png");
//生成验证码字符文本
String verifyCode = kaptchaProducer.createText();
request.getSession().setAttribute("kaptchaVerifyCode",verifyCode);
System.out.println(request.getSession().getAttribute("kaptchaVerifyCode"));
BufferedImage image = kaptchaProducer.createImage(verifyCode);//创建验证码图片
ServletOutputStream out = response.getOutputStream();//二进制的图片,用getOutputStream()方法
ImageIO.write(image,"png",out);
out.flush();//立即输出
out.close();//关闭输出
}
}
package com.imooc.reader.controller;
import com.imooc.reader.service.MemberService;
import com.imooc.reader.service.exception.BussinessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@Controller
public class MemberController {
@Resource
private MemberService memberService;
@GetMapping("/register.html")
public ModelAndView showRegister(){
return new ModelAndView("/register");
}
@PostMapping("/registe")
@ResponseBody
public Map registe(String vc, String username, String password , String nickname , HttpServletRequest request){
//正确验证码
String verifyCode = (String)request.getSession().getAttribute("kaptchaVerifyCode");//与KaptchaController里面的request.getSession().setAttribute("kaptchaVerifyCode",verifyCode)类似
//验证码对比
Map result = new HashMap();
if(vc == null || verifyCode == null || !vc.equalsIgnoreCase(verifyCode)){
result.put("code", "VC01");
result.put("msg", "验证码错误");
}else{
try {
memberService.createMember(username, password, nickname);
result.put("code", "0");
result.put("msg", "success");
}catch (BussinessException ex){
ex.printStackTrace();
result.put("code", ex.getCode());
result.put("msg", ex.getMsg());
}
}
return result;
}
}
@PostMapping("/registe")
@ResponseBody
public Map registe(String vc, String username, String password , String nickname , HttpServletRequest request){
//正确验证码
String verifyCode = (String)request.getSession().getAttribute("kaptchaVerifyCode");//与KaptchaController里面的request.getSession().setAttribute("kaptchaVerifyCode",verifyCode)类似
//验证码对比
Map result = new HashMap();
if(vc == null || verifyCode == null || !vc.equalsIgnoreCase(verifyCode)){
result.put("code", "VC01");
result.put("msg", "验证码错误");
}else{
try {
memberService.createMember(username, password, nickname);
result.put("code", "0");
result.put("msg", "success");
}catch (BussinessException ex){
ex.printStackTrace();
result.put("code", ex.getCode());
result.put("msg", ex.getMsg());
}
}
return result;
}