Commit 72f87dc7 by xiaolang850403

图形验证码

parent 6b477c9b
......@@ -8,6 +8,7 @@ import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Date;
/*
* @author xiaol
......@@ -57,6 +58,18 @@ public class Common {
return Arrays.toString(arr);
}
//日期比较大小
public static boolean getCompareDate(String daf) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String begin = df.format(new Date());
Integer i = begin.compareTo(daf); //返回值大于0表示左侧大于右侧
if(i>0){
return true;
}else{
return false;
}
}
/**
* @param phone 字符串类型的手机号
* 传入手机号,判断后返回
......
......@@ -10,6 +10,9 @@ import com.wf.captcha.SpecCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
......@@ -32,10 +35,15 @@ public class CaptchaController {
String key = UUID.randomUUID().toString();
// 存入表并设置过期时间为2分钟
Captcha cap = new Captcha();
cap.setCaptcha_key(key);
cap.setCaptcha_value(specCaptcha.toBase64());
String ex = Common.getDateToString(Common.getTime() + 120);
cap.setCaptchaKey(key);
cap.setCaptchaValue(verCode);
String ex = Common.getDateToString(Common.getTime() + 2*60*1000);
cap.setExpire(ex);
return ResultUtil.success(CodeEnum.SUCCESS,cap);
//入库
captchaService.save(cap);
Map map = new HashMap();
map.put("key",key);
map.put("image",specCaptcha.toBase64());
return ResultUtil.success(CodeEnum.SUCCESS,map);
}
}
......@@ -4,10 +4,12 @@ import com.aliyun.oss.OSS;
import com.asset.common.*;
import com.asset.domain.Result;
import com.asset.domain.User;
import com.asset.domain.system.Captcha;
import com.asset.domain.system.Certificate;
import com.asset.domain.system.LoginLog;
import com.asset.enums.CodeEnum;
import com.asset.service.UserService;
import com.asset.service.system.CaptchaService;
import com.asset.service.system.CertificateService;
import com.asset.service.system.LoginLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
......@@ -22,6 +24,7 @@ import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.text.ParseException;
import java.util.*;
......@@ -42,6 +45,9 @@ public class UserController {
@Autowired
LoginLogService loginLogService;
@Autowired
CaptchaService captchaService;
@RequestMapping("/register")
public Result register(@Validated(value={Insert.class}) @RequestBody User user, BindingResult result) throws UnsupportedEncodingException {
if (result.hasErrors()) {
......@@ -263,4 +269,32 @@ public class UserController {
map.put("url",url);
return ResultUtil.success(CodeEnum.SUCCESS,map);
}
@GetMapping("/checkCaptcha")
public Result checkCaptcha(String name,String verKey,String captcha) throws ParseException {
//先判断账号是否存在
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("mobile",name).or().eq("email",name);
User user = userService.getOne(queryWrapper);
if(user == null){
return ResultUtil.success(CodeEnum.ISEXITE_NOT_ERROR);
}else{
QueryWrapper<Captcha> CapqueryWrapper = new QueryWrapper<>();
CapqueryWrapper.eq("captcha_key",verKey);
Captcha cap = captchaService.getOne(CapqueryWrapper);
if(cap == null){
return ResultUtil.success(CodeEnum.ISEXITE_CODE);
}else{ //比较看验证码是否过期
if(Common.getCompareDate(cap.getExpire())){
return ResultUtil.success(CodeEnum.CODE_EXpired);
}else{ //没过期判断对错
if(captcha==null || !cap.getCaptchaValue().equals(captcha.trim().toLowerCase())){
return ResultUtil.success(CodeEnum.CODE_ERROR);
}
}
}
}
return ResultUtil.success(CodeEnum.SUCCESS);
}
}
......@@ -15,9 +15,9 @@ public class Captcha {
private Integer id;
private String captcha_key;
private String captchaKey;
private String captcha_value;
private String captchaValue;
private String expire;
......
......@@ -18,7 +18,10 @@ public enum CodeEnum {
ISEXITE_NOT_ERROR(20005,"账号不存在!"),
LOGINOUT_SUCCESS(20004,"成功!"),
ISEXITE_MOBILE(20006,"手机号已经存在!"),
FORMAT_MOBILE(20007,"手机号格式不正确!")
FORMAT_MOBILE(20007,"手机号格式不正确!"),
CODE_ERROR(20008,"验证码错误!"),
ISEXITE_CODE(20010,"验证码不存在!"),
CODE_EXpired(20009,"验证码过期!")
;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment