Commit d0409377 authored by jiaorz's avatar jiaorz

验证码图片问题

parent 41c39b6e
......@@ -7,4 +7,10 @@ public class RedisKey {
*/
public static final String CONSTANT_CODE_PREFIX ="cache:mobilecode:";
/**
* 图片验证码
*/
public static final String CAPTCHA_PHONE_PREFIX = "captcha:phone:";
}
......@@ -8,7 +8,6 @@ import com.github.wxiaoqi.security.admin.rpc.service.AppPermissionService;
import com.github.wxiaoqi.security.admin.vo.ImiVo;
import com.github.wxiaoqi.security.api.vo.authority.PermissionInfo;
import com.github.wxiaoqi.security.api.vo.user.AppUserInfo;
import com.github.wxiaoqi.security.auth.client.annotation.IgnoreUserToken;
import com.github.wxiaoqi.security.auth.client.config.UserAuthConfig;
import com.github.wxiaoqi.security.auth.client.jwt.UserAuthUtil;
import com.github.wxiaoqi.security.auth.common.util.jwt.IJWTInfo;
......@@ -72,8 +71,8 @@ public class AppUserRest {
*/
@RequestMapping(value = "/user/sendsms", method = RequestMethod.POST)
public @ResponseBody
JSONObject sendsms(@RequestParam(value="username",defaultValue="")String username, @RequestParam(value="type",defaultValue="0")Integer type){
return appPermissionService.sendSMS(username,type);
JSONObject sendsms(@RequestParam(value="username",defaultValue="")String username, @RequestParam(value="type",defaultValue="0")Integer type, String pointList){
return appPermissionService.sendSMS(username,type, pointList);
}
/**
......
......@@ -160,7 +160,7 @@ public class AppPermissionService {
* @return phone手机号 type:类型(0用户注册,1微信绑定,2人脸注册,3忘记密码,4直接发送验证码) 改写发送验证码方法(暂时通过测试)
*/
public JSONObject sendSMS(String phone, Integer type) {
public JSONObject sendSMS(String phone, Integer type, String pointList) {
if (StringUtils.isBlank(phone) || type == null) {
return JsonResultUtil.createFailedResult(ResultCode.NULL_CODE, "参数为空");
}
......@@ -170,11 +170,15 @@ public class AppPermissionService {
}
// 组织返回结果集
JSONObject result = new JSONObject();
if (type == 0) {
if (type == 0) {// 注册验证码
AppUserLogin rsUserLogin = appUserLoginBiz.checkeUserLogin(phone);
if (rsUserLogin != null) {
return JsonResultUtil.createFailedResult(ResultCode.EXIST_CODE, "用户已存在");
}
ObjectRestResponse<Boolean> objectRestResponse = thirdFeign.verify(phone, pointList);
if (objectRestResponse.getData() == null || objectRestResponse.getData() == false) {
return JsonResultUtil.createFailedResult(ResultCode.FAILED_CODE, "图片验证码验证失败");
}
} else if (type == 1) {
AppUserLogin rsUserLogin = appUserLoginBiz.checkeUserLogin(phone);
......
......@@ -103,4 +103,7 @@ public class RedisKey {
* 服务器上传压缩包文件序号
*/
public static final String UPLOAD_ZIP_NO_PREFIX ="upload:zip:no:";
public static final String CAPTCHA_PHONE_PREFIX = "captcha:phone:";
}
package com.xxfc.platform.universal.dto;
import lombok.Data;
@Data
public class VerifyDto {
String list;
String pointList;
}
\ No newline at end of file
......@@ -90,4 +90,7 @@ public interface ThirdFeign {
@GetMapping("/info/app/unauth/getAliPayUserInfo")
public ObjectRestResponse<String> getAliPayUserInfo(@RequestParam(value = "code")String code);
@RequestMapping("/captcha/app/unauth/verify")
ObjectRestResponse<Boolean> verify(@RequestParam(value = "phone")String phone, @RequestParam(value = "pointList")String pointList);
}
package com.xxfc.platform.universal.controller;
import com.alibaba.fastjson.JSONArray;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.util.process.ResultCode;
import com.xxfc.platform.universal.constant.CaptchaType;
import com.xxfc.platform.universal.constant.RedisKey;
import com.xxfc.platform.universal.dto.CaptchaBaseParam;
import com.xxfc.platform.universal.service.AbstractCaptcha;
import com.xxfc.platform.universal.service.CaptchaFactory;
import com.xxfc.platform.universal.service.ICaptchaFactory;
import com.xxfc.platform.universal.utils.ImageUtils;
import com.xxfc.platform.universal.vo.CaptchaBaseVO;
import com.xxfc.platform.universal.vo.ClickWordCaptchaVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.awt.*;
import java.util.concurrent.TimeUnit;
/**
* @author stx
* @date 2018/11/5 14:58
......@@ -19,8 +30,12 @@ import org.springframework.web.bind.annotation.ResponseBody;
*/
@Controller
@RequestMapping(value = "/captcha")
@Slf4j
public class CaptchaController {
@Autowired
RedisTemplate redisTemplate;
@RequestMapping("/app/unauth/blockPuzzle")
@ResponseBody
ObjectRestResponse blockPuzzle() {
......@@ -36,17 +51,47 @@ public class CaptchaController {
@RequestMapping("/app/unauth/clickWord")
@ResponseBody
ObjectRestResponse clickWord() {
ObjectRestResponse clickWord(String phone) {
ICaptchaFactory captchaFactory = new CaptchaFactory();
AbstractCaptcha captcha = captchaFactory.getInstance(CaptchaType.CLICK_WORD);
CaptchaBaseParam captchaBaseParam = new CaptchaBaseParam();
captchaBaseParam.setUrlOrPath(ImageUtils.getClickWordBgPath());
captcha.setFontColorRandom(false);
CaptchaBaseVO dataVO = captcha.create(captchaBaseParam);
ClickWordCaptchaVO dataVO = (ClickWordCaptchaVO) captcha.create(captchaBaseParam);
if (dataVO.getPointList() != null) {
try {
String redisLockKey = RedisKey.CAPTCHA_PHONE_PREFIX + phone;
Boolean suc = redisTemplate.opsForValue().setIfAbsent(redisLockKey, JSONArray.toJSONString(dataVO.getPointList()));
if (suc) {
redisTemplate.expire(redisLockKey, 5, TimeUnit.MINUTES);//5分钟内过期
}
} catch (Exception e) {
log.error(e.getMessage(), e);
return ObjectRestResponse.createFailedResult(ResultCode.EXCEPTION_CODE, "出现异常");
}
}
return ObjectRestResponse.succ(dataVO);
}
@RequestMapping("/app/unauth/verify")
@ResponseBody
ObjectRestResponse<Boolean> verify(String phone, String pointList) {
if (StringUtils.isBlank(phone) || StringUtils.isBlank(pointList)) {
return ObjectRestResponse.paramIsEmpty();
}
String redisLockKey = RedisKey.CAPTCHA_PHONE_PREFIX + phone;
String captchaCode = redisTemplate.opsForValue().get(redisLockKey) == null ? "" : redisTemplate.opsForValue().get(redisLockKey).toString();
log.info("【注册验证码坐标】:》》》》》 {}", captchaCode);
if (StringUtils.isBlank(captchaCode)) {
log.error("【注册验证码坐标为空,请重新验证】");
return ObjectRestResponse.succ(false);
}
ICaptchaFactory captchaFactory = new CaptchaFactory();
AbstractCaptcha captcha = captchaFactory.getInstance(CaptchaType.CLICK_WORD);
boolean flag = captcha.verification(JSONArray.parseArray(captchaCode, Point.class), JSONArray.parseArray(pointList,Point.class));
return ObjectRestResponse.succ(flag);
}
}
package com.xxfc.platform.universal.service;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.xxfc.platform.universal.dto.CaptchaBaseParam;
import com.xxfc.platform.universal.interceptor.CaptchaException;
import com.xxfc.platform.universal.utils.RandomUtils;
import com.xxfc.platform.universal.vo.CaptchaBaseVO;
import com.xxfc.platform.universal.vo.ClickWordCaptchaVO;
import org.apache.commons.lang3.StringUtils;
......@@ -32,8 +32,9 @@ public class ClickWordCaptcha extends AbstractCaptcha {
private static int HAN_ZI_SIZE_HALF = 30/2;
@Override
public CaptchaBaseVO create(CaptchaBaseParam captchaParam) {
public ClickWordCaptchaVO create(CaptchaBaseParam captchaParam) {
if (captchaParam == null
|| StringUtils.isBlank(captchaParam.getUrlOrPath())) {
throw new CaptchaException("参数不正确,captchaParam:"+JSON.toJSONString(captchaParam));
......@@ -64,10 +65,10 @@ public class ClickWordCaptcha extends AbstractCaptcha {
return false;
}
}
return false;
return true;
}
private CaptchaBaseVO getImageData(BufferedImage backgroundImage) {
private ClickWordCaptchaVO getImageData(BufferedImage backgroundImage) {
ClickWordCaptchaVO dataVO = new ClickWordCaptchaVO();
List<String> wordList = new ArrayList<String>();
List<Point> pointList = new ArrayList();
......@@ -106,7 +107,6 @@ public class ClickWordCaptcha extends AbstractCaptcha {
if ((num-1) != i) {
wordList.add(word);
pointList.add(point);
}
}
......@@ -114,9 +114,11 @@ public class ClickWordCaptcha extends AbstractCaptcha {
BufferedImage combinedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics combinedGraphics = combinedImage.getGraphics();
combinedGraphics.drawImage(backgroundImage, 0, 0, null);
Point point = pointList.get(RandomUtils.getRandomInt(0, wordList.size()-1).intValue());
dataVO.setOriginalImageBase64(getImageToBase64Str(backgroundImage));
dataVO.setPointList(pointList);
List<Point> list = Lists.newArrayList();
list.add(point);
dataVO.setPointList(list);
dataVO.setWordList(wordList);
return dataVO;
}
......
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 to comment