Commit 3b0a0ded authored by zhouqian's avatar zhouqian

Merge branch 'master' of http://gitabc.xyqb.com/head_group/xyqb-user2

# Conflicts:
#	src/main/java/cn/quantgroup/xyqb/Constants.java
parent 4222d919
...@@ -9,7 +9,6 @@ import cn.quantgroup.xyqb.model.AuthBean; ...@@ -9,7 +9,6 @@ import cn.quantgroup.xyqb.model.AuthBean;
import cn.quantgroup.xyqb.model.JsonResult; import cn.quantgroup.xyqb.model.JsonResult;
import cn.quantgroup.xyqb.model.UserModel; import cn.quantgroup.xyqb.model.UserModel;
import cn.quantgroup.xyqb.model.session.SessionStruct; import cn.quantgroup.xyqb.model.session.SessionStruct;
import cn.quantgroup.xyqb.model.session.SessionValue;
import cn.quantgroup.xyqb.repository.IUserDetailRepository; import cn.quantgroup.xyqb.repository.IUserDetailRepository;
import cn.quantgroup.xyqb.service.session.ISessionService; import cn.quantgroup.xyqb.service.session.ISessionService;
import cn.quantgroup.xyqb.service.sms.ISmsService; import cn.quantgroup.xyqb.service.sms.ISmsService;
...@@ -17,14 +16,11 @@ import cn.quantgroup.xyqb.service.user.IUserService; ...@@ -17,14 +16,11 @@ import cn.quantgroup.xyqb.service.user.IUserService;
import cn.quantgroup.xyqb.util.PasswordUtil; import cn.quantgroup.xyqb.util.PasswordUtil;
import cn.quantgroup.xyqb.util.ValidationUtil; import cn.quantgroup.xyqb.util.ValidationUtil;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
...@@ -32,14 +28,7 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -32,14 +28,7 @@ import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* Created by FrankChow on 15/7/5. * Created by FrankChow on 15/7/5.
...@@ -51,9 +40,7 @@ public class UserController implements IBaseController { ...@@ -51,9 +40,7 @@ public class UserController implements IBaseController {
private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class); private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
private static final int SMS_VERIFICATION_MAXLEN = 6; private final String pwdSalt = "_lkb";
public final String pwdSalt = "_lkb";
@Autowired @Autowired
private IUserService userService; private IUserService userService;
...@@ -71,8 +58,6 @@ public class UserController implements IBaseController { ...@@ -71,8 +58,6 @@ public class UserController implements IBaseController {
@Autowired @Autowired
private IUserDetailRepository userDetailRepository; private IUserDetailRepository userDetailRepository;
public static final String SMS_VERIFY_PREFIX = "sms:verify:";
private static final char[] PWD_BASE = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', private static final char[] PWD_BASE = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
...@@ -186,7 +171,7 @@ public class UserController implements IBaseController { ...@@ -186,7 +171,7 @@ public class UserController implements IBaseController {
if(!ValidationUtil.validatePhoneNo(phoneNo)){ if(!ValidationUtil.validatePhoneNo(phoneNo)){
return JsonResult.buildErrorStateResult("登录失败", null); return JsonResult.buildErrorStateResult("登录失败", null);
} }
if(!validateVerificationCode(phoneNo, verificationCode)){ if(!smsService.validateFastLoginVerificationCode(phoneNo, verificationCode)){
LOGGER.info("用户快速登录,验证码校验失败,phoneNo:{} , verificationCode:{}", phoneNo, verificationCode); LOGGER.info("用户快速登录,验证码校验失败,phoneNo:{} , verificationCode:{}", phoneNo, verificationCode);
return JsonResult.buildErrorStateResult("验证码错误", null); return JsonResult.buildErrorStateResult("验证码错误", null);
} }
...@@ -212,27 +197,6 @@ public class UserController implements IBaseController { ...@@ -212,27 +197,6 @@ public class UserController implements IBaseController {
return new JsonResult(authBean); return new JsonResult(authBean);
} }
private boolean validateVerificationCode(String phoneNo, String verificationCode){
String key = SMS_VERIFY_PREFIX + phoneNo;
String randomCode = stringRedisTemplate.opsForValue().get(key);
if(StringUtils.isBlank(randomCode)){
return false;
}
String[] arr = randomCode.split(":");
if(arr.length != 2){
return false;
}
String uniqueId = arr[0];
String code = arr[1];
if(code == verificationCode){
stringRedisTemplate.delete(key);
//todo 发送mq消息,后续补
return true;
}
return false;
}
/** /**
* 用户快速注册 * 用户快速注册
* *
...@@ -265,7 +229,7 @@ public class UserController implements IBaseController { ...@@ -265,7 +229,7 @@ public class UserController implements IBaseController {
LOGGER.info("用户快速注册失败,该手机号已经被注册, registerFrom:{}, phoneNo:{}", registerFrom, phoneNo); LOGGER.info("用户快速注册失败,该手机号已经被注册, registerFrom:{}, phoneNo:{}", registerFrom, phoneNo);
return JsonResult.buildErrorStateResult("该手机号已经被注册", null); return JsonResult.buildErrorStateResult("该手机号已经被注册", null);
} }
if (!validSmsVerificationCode(phoneNo, verificationCode)) { if (!smsService.validRegisterOrResetPasswdVerificationCode(phoneNo, verificationCode)) {
LOGGER.info("用户快速注册失败,短信验证码错误, registerFrom:{}, phoneNo:{}, verificationCode:{}", registerFrom, phoneNo, verificationCode); LOGGER.info("用户快速注册失败,短信验证码错误, registerFrom:{}, phoneNo:{}, verificationCode:{}", registerFrom, phoneNo, verificationCode);
return JsonResult.buildErrorStateResult("短信验证码错误", null); return JsonResult.buildErrorStateResult("短信验证码错误", null);
} }
...@@ -313,7 +277,7 @@ public class UserController implements IBaseController { ...@@ -313,7 +277,7 @@ public class UserController implements IBaseController {
LOGGER.info("用户注册失败,该手机号已经被注册, registerFrom:{}, phoneNo:{}", registerFrom, phoneNo); LOGGER.info("用户注册失败,该手机号已经被注册, registerFrom:{}, phoneNo:{}", registerFrom, phoneNo);
return JsonResult.buildErrorStateResult("该手机号已经被注册", null); return JsonResult.buildErrorStateResult("该手机号已经被注册", null);
} }
if (!validSmsVerificationCode(phoneNo, verificationCode)) { if (!smsService.validRegisterOrResetPasswdVerificationCode(phoneNo, verificationCode)) {
LOGGER.info("用户注册失败,短信验证码错误, registerFrom:{}, phoneNo:{}, verificationCode:{}", registerFrom, phoneNo, verificationCode); LOGGER.info("用户注册失败,短信验证码错误, registerFrom:{}, phoneNo:{}, verificationCode:{}", registerFrom, phoneNo, verificationCode);
return JsonResult.buildErrorStateResult("短信验证码错误", null); return JsonResult.buildErrorStateResult("短信验证码错误", null);
} }
...@@ -327,36 +291,6 @@ public class UserController implements IBaseController { ...@@ -327,36 +291,6 @@ public class UserController implements IBaseController {
return JsonResult.buildSuccessResult(null, null); return JsonResult.buildSuccessResult(null, null);
} }
/**
* 检查验证码是否正确
*
* @param phoneNo
* @param smsVerificationCode
* @return
*/
private boolean validSmsVerificationCode(String phoneNo, String smsVerificationCode) {
if (StringUtils.isEmpty(smsVerificationCode) || smsVerificationCode.length() != SMS_VERIFICATION_MAXLEN) {
return false;
}
String key = Constants.REDIS_PREFIX_VERIFICATION_CODE + phoneNo;
String randomCode = stringRedisTemplate.opsForValue().get(key);
if (!randomCode.contains(":")) {
return StringUtils.equals(randomCode, smsVerificationCode);
}
String[] arr = randomCode.split(":");
String unqiueId = arr[0];
String code = arr[1];
if (StringUtils.equals(code, smsVerificationCode)) {
try {
smsService.getSmsSender().confirmSmsResult("1", unqiueId);
} catch (Exception e) {
LOGGER.info("短信验证像短信中心确认失效");
}
return true;
}
return false;
}
/** /**
* 检查用户是否存在 * 检查用户是否存在
* *
...@@ -409,7 +343,7 @@ public class UserController implements IBaseController { ...@@ -409,7 +343,7 @@ public class UserController implements IBaseController {
if (password.length() < 6 || password.length() > 20) { if (password.length() < 6 || password.length() > 20) {
return JsonResult.buildErrorStateResult("密码长度须在6位至20位之间", null); return JsonResult.buildErrorStateResult("密码长度须在6位至20位之间", null);
} }
if (!validSmsVerificationCode(phoneNo, verificationCode)) { if (!smsService.validRegisterOrResetPasswdVerificationCode(phoneNo, verificationCode)) {
return JsonResult.buildErrorStateResult("短信验证码错误", null); return JsonResult.buildErrorStateResult("短信验证码错误", null);
} }
if (!userService.resetPassword(phoneNo, password)) { if (!userService.resetPassword(phoneNo, password)) {
...@@ -478,4 +412,5 @@ public class UserController implements IBaseController { ...@@ -478,4 +412,5 @@ public class UserController implements IBaseController {
} }
} }
...@@ -13,4 +13,8 @@ public interface ISmsService { ...@@ -13,4 +13,8 @@ public interface ISmsService {
SmsSender getSmsSender(); SmsSender getSmsSender();
void sendAfterRegister(String phoneNo); void sendAfterRegister(String phoneNo);
boolean validRegisterOrResetPasswdVerificationCode(String phoneNo, String smsVerificationCode);
boolean validateFastLoginVerificationCode(String phoneNo, String verificationCode);
} }
...@@ -2,7 +2,9 @@ package cn.quantgroup.xyqb.service.sms.impl; ...@@ -2,7 +2,9 @@ package cn.quantgroup.xyqb.service.sms.impl;
import cn.quantgroup.sms.SendAndForgetMsg; import cn.quantgroup.sms.SendAndForgetMsg;
import cn.quantgroup.sms.SmsSender; import cn.quantgroup.sms.SmsSender;
import cn.quantgroup.xyqb.Constants;
import cn.quantgroup.xyqb.service.sms.ISmsService; import cn.quantgroup.xyqb.service.sms.ISmsService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -22,12 +24,15 @@ public class SmsServiceImpl implements ISmsService { ...@@ -22,12 +24,15 @@ public class SmsServiceImpl implements ISmsService {
private static final Logger LOGGER = LoggerFactory.getLogger(SmsServiceImpl.class); private static final Logger LOGGER = LoggerFactory.getLogger(SmsServiceImpl.class);
private static final int SMS_VERIFICATION_MAXLEN = 6;
private static final String SMS_VERIFY_PREFIX = "sms:verify:";
@Value("${sms.is.debug}") @Value("${sms.is.debug}")
private boolean isDebug; private boolean isDebug;
@Autowired @Autowired
@Qualifier("stringRedisTemplate") @Qualifier("stringRedisTemplate")
private RedisTemplate<String, String> redisTemplate; private RedisTemplate<String, String> stringRedisTemplate;
private SmsSender smsSender = null; private SmsSender smsSender = null;
...@@ -48,4 +53,54 @@ public class SmsServiceImpl implements ISmsService { ...@@ -48,4 +53,54 @@ public class SmsServiceImpl implements ISmsService {
e.printStackTrace(); e.printStackTrace();
} }
} }
/**
* 检查验证码是否正确
*
* @param phoneNo
* @param smsVerificationCode
* @return
*/
@Override
public boolean validRegisterOrResetPasswdVerificationCode(String phoneNo, String smsVerificationCode) {
if (StringUtils.isEmpty(smsVerificationCode) || smsVerificationCode.length() != SMS_VERIFICATION_MAXLEN) {
return false;
}
String key = Constants.REDIS_PREFIX_VERIFICATION_CODE + phoneNo;
String randomCode = stringRedisTemplate.opsForValue().get(key);
String[] arr = randomCode.split(":");
String unqiueId = arr[0];
String code = arr[1];
return confirmSms(smsVerificationCode, unqiueId, code);
}
@Override
public boolean validateFastLoginVerificationCode(String phoneNo, String verificationCode){
String key = SMS_VERIFY_PREFIX + phoneNo;
String randomCode = stringRedisTemplate.opsForValue().get(key);
if(StringUtils.isBlank(randomCode)){
return false;
}
String[] arr = randomCode.split(":");
if(arr.length != 2){
return false;
}
String uniqueId = arr[0];
String code = arr[1];
return confirmSms(verificationCode, uniqueId, code);
}
private boolean confirmSms(String smsVerificationCode, String unqiueId, String code) {
if (StringUtils.equals(code, smsVerificationCode)) {
try {
smsSender.confirmSmsResult("1", unqiueId);
} catch (Exception e) {
LOGGER.info("短信验证像短信中心确认失效");
}
return true;
}
return false;
}
} }
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