Commit 77e20b3b authored by zhouqian's avatar zhouqian
parents 47fcd152 eb925cd3
...@@ -8,6 +8,7 @@ import cn.quantgroup.xyqb.model.AuthBean; ...@@ -8,6 +8,7 @@ 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;
...@@ -15,11 +16,13 @@ import cn.quantgroup.xyqb.service.user.IUserService; ...@@ -15,11 +16,13 @@ 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.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;
...@@ -29,7 +32,13 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -29,7 +32,13 @@ 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.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.
...@@ -69,6 +78,8 @@ public class UserController implements IBaseController { ...@@ -69,6 +78,8 @@ 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'};
...@@ -145,11 +156,103 @@ public class UserController implements IBaseController { ...@@ -145,11 +156,103 @@ public class UserController implements IBaseController {
* *
* @return * @return
*/ */
@RequestMapping("/login/fast") /*@RequestMapping("/login/fast")
public JsonResult loginFast(Principal user) { public JsonResult loginFast(Principal user) {
AuthBean bean = new AuthBean(getRequest().getSession().getId(), user); AuthBean bean = new AuthBean(getRequest().getSession().getId(), user);
LOGGER.info("用户快速登录成功, loginFrom:{}, phoneNo:{},appChannel:{}", getCreatedFrom(), user.getName(), getAppChannel()); LOGGER.info("用户快速登录成功, loginFrom:{}, phoneNo:{},appChannel:{}", getCreatedFrom(), user.getName(), getAppChannel());
return new JsonResult(bean); return new JsonResult(bean);
}*/
@RequestMapping("/login/fast")
public JsonResult loginFast(HttpServletRequest request){
String verificationHeader = "Verification ";
String credential = request.getHeader("authorization");
if(StringUtils.isBlank(credential)){
return JsonResult.buildErrorStateResult("登录失败", null);
}
if(!credential.startsWith(verificationHeader)){
return JsonResult.buildErrorStateResult("登录失败", null);
}
credential = credential.substring(verificationHeader.length(), credential.length());
byte[] buf = Base64.decodeBase64(credential);
try {
credential = new String(buf, "UTF-8");
} catch (UnsupportedEncodingException e){
LOGGER.error("不支持的编码.");
return JsonResult.buildErrorStateResult("登录失败", null);
}
String[] credentialArr = credential.split(":");
if(credentialArr.length != 2){
return JsonResult.buildErrorStateResult("登录失败", null);
}
String phoneNo = credentialArr[0];
String verificationCode = credentialArr[1];
if(!matchPhoneNo(phoneNo)){
return JsonResult.buildErrorStateResult("登录失败", null);
}
if(!validateVerificationCode(phoneNo, verificationCode)){
return JsonResult.buildErrorStateResult("登录失败", null);
}
User user = userService.findByPhone(phoneNo);
if(user == null){
return JsonResult.buildErrorStateResult("登录失败", null);
}
String sessionId = sessionService.findSessionIdByUserId(user.getId());
if(StringUtils.isBlank(sessionId)){
return JsonResult.buildErrorStateResult("登录失败", null);
}
SessionStruct sessionStruct = null;
if(sessionId.length() == 36){
String value = sessionService.findSessionValueBySessionId(sessionId);
if(StringUtils.isBlank(value)){
sessionStruct = sessionService.newSession(user);
SessionValue sessionValue = new SessionValue();
sessionValue.setUser(user);
sessionValue.setLastAccessTime(new Timestamp(System.currentTimeMillis()));
sessionValue.setCreatedAt(new Timestamp(System.currentTimeMillis()));
Map<String, String> values = new HashMap();
sessionValue.setValues(values);
sessionService.persistSession(sessionStruct.getSid(), sessionValue);
}
sessionStruct.setSid(sessionId);
}else {
sessionStruct = sessionService.newSession(user);
sessionService.persistSession(sessionStruct.getSid(), sessionService.newSession(user).getValues());
}
AuthBean authBean = new AuthBean();
authBean.setPhoneNo(user.getPhoneNo());
authBean.setToken(sessionStruct.getSid());
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;
}
private boolean matchPhoneNo(String phoneNo){
String patternString = "1\\d{10}";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(phoneNo);
if(matcher.matches()){
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