Commit 270c84d0 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/controller/internal/user/UserController.java
parents 9a6bbb72 178a551d
......@@ -28,6 +28,8 @@ import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
/**
......@@ -67,11 +69,19 @@ public class UserController implements IBaseController {
@RequestMapping("/login")
public JsonResult login2(@RequestParam(required = false, defaultValue = "") String userId, HttpServletRequest request) {
if (userId != null && userId.length() > 10) {
userService.findByUuidInDb()
} else {
AuthBean authBean = new AuthBean();
if(!StringUtils.isEmpty(userId)){
//查询用户,存在则保存用户session信息,userId为uuid
User user = userService.findByUuidInDb(userId);
//用户信息存在,更新session中的最后访问时间,重新写入缓存.
if(null != user){
SessionStruct sessionStruct = sessionService.createSessionAndPersistInCache(user);
authBean.setPhoneNo(user.getPhoneNo());
authBean.setToken(sessionStruct.getSid());
}
}else {
String credential = request.getHeader("authorization");
if (!credential.startsWith("Basic ")) {
if(!credential.startsWith("Basic ")){
return JsonResult.buildErrorStateResult("用户名或密码不正确", null);
}
credential = credential.substring("Basic ".length(), credential.length());
......@@ -79,17 +89,17 @@ public class UserController implements IBaseController {
String bufStr = "";
try {
bufStr = new String(buf, "UTF-8");
} catch (UnsupportedEncodingException e) {
} catch (UnsupportedEncodingException e){
LOGGER.error("不支持的编码: ", e);
}
String[] credentialArr = bufStr.split(":");
if (credentialArr.length != 2) {
if(credentialArr.length != 2){
return JsonResult.buildErrorStateResult("用户名或密码不正确.", null);
}
String userName = credentialArr[0];
String pass = credentialArr[1];
User user = userService.findByPhoneWithCache(userName);
if (user == null) {
if(user == null){
return JsonResult.buildErrorStateResult("用户名或密码不正确", null);
}
//验证密码
......@@ -99,11 +109,10 @@ public class UserController implements IBaseController {
//找到用户
SessionStruct sessionStruct = sessionService.createSessionAndPersistInCache(user);
LOGGER.info("用户登录成功, loginFrom:{}, phoneNo:{},appChannel:{}", getCreatedFrom(), user.getPhoneNo(), getAppChannel());
AuthBean authBean = new AuthBean();
authBean.setPhoneNo(user.getPhoneNo());
authBean.setToken(sessionStruct.getSid());
return new JsonResult(authBean);
}
return new JsonResult(authBean);
}
@RequestMapping("/test")
......@@ -120,13 +129,45 @@ public class UserController implements IBaseController {
public JsonResult loginFast(
@RequestParam(required = false, defaultValue = "1") Long channelId, String appChannel,
@RequestParam(required = false, defaultValue = "1") Long createdFrom, HttpServletRequest request){
Map<String, JsonResult> validMap = getHeaderParam(request);
if(null != validMap.get("fail")){
return validMap.get("fail");
}
JsonResult successResult = validMap.get("success");
String phoneNo = successResult.getData().toString();
User user = userService.findByPhoneWithCache(phoneNo);
if(user == null){
user = registerFastWhenLogin(phoneNo, channelId, createdFrom, appChannel);
if (user == null) {
throw new UserNotExistException("用户未找到");
}
}
String sessionId = sessionService.findSessionIdByUserId(user.getId());
if(StringUtils.isBlank(sessionId)){
return JsonResult.buildErrorStateResult("登录失败", null);
}
SessionStruct sessionStruct = sessionService.createSessionAndPersist(user, (session) -> {
session.setAttribute("channelId", String.valueOf(channelId));
session.setAttribute("createdFrom", String.valueOf(createdFrom));
session.setAttribute("appChannel", String.valueOf(appChannel));
});
return returnSuccessResult(phoneNo, sessionStruct.getSid());
}
/**
* 快速登录验证
* @param request
* @return
*/
private Map<String, JsonResult> getHeaderParam(HttpServletRequest request){
Map<String, JsonResult> result = new HashMap();
String verificationHeader = "Verification ";
String credential = request.getHeader("authorization");
if(StringUtils.isBlank(credential)){
return JsonResult.buildErrorStateResult("登录失败", null);
result.put("fail", JsonResult.buildErrorStateResult("登录失败", null));
}
if(!credential.startsWith(verificationHeader)){
return JsonResult.buildErrorStateResult("登录失败", null);
result.put("fail", JsonResult.buildErrorStateResult("登录失败", null));
}
credential = credential.substring(verificationHeader.length(), credential.length());
byte[] buf = Base64.decodeBase64(credential);
......@@ -134,41 +175,36 @@ public class UserController implements IBaseController {
credential = new String(buf, "UTF-8");
} catch (UnsupportedEncodingException e){
LOGGER.error("不支持的编码.");
return JsonResult.buildErrorStateResult("登录失败", null);
result.put("fail", JsonResult.buildErrorStateResult("登录失败", null));
}
String[] credentialArr = credential.split(":");
if(credentialArr.length != 2){
return JsonResult.buildErrorStateResult("登录失败", null);
result.put("fail", JsonResult.buildErrorStateResult("登录失败", null));
}
String phoneNo = credentialArr[0];
String verificationCode = credentialArr[1];
LOGGER.info("用户快速登录,phoneNo:{} , verificationCode:{}", phoneNo, verificationCode);
if(!ValidationUtil.validatePhoneNo(phoneNo)){
return JsonResult.buildErrorStateResult("登录失败", null);
result.put("fail", JsonResult.buildErrorStateResult("登录失败", null));
}
if(!smsService.validateFastLoginVerificationCode(phoneNo, verificationCode)){
LOGGER.info("用户快速登录,验证码校验失败,phoneNo:{} , verificationCode:{}", phoneNo, verificationCode);
return JsonResult.buildErrorStateResult("验证码错误", null);
}
User user = userService.findByPhoneWithCache(phoneNo);
if(user == null){
user = registerFastWhenLogin(phoneNo, channelId, createdFrom, appChannel);
if (user == null) {
throw new UserNotExistException("用户未找到");
}
result.put("fail", JsonResult.buildErrorStateResult("验证码错误", null));
}
String sessionId = sessionService.findSessionIdByUserId(user.getId());
if(StringUtils.isBlank(sessionId)){
return JsonResult.buildErrorStateResult("登录失败", null);
}
SessionStruct sessionStruct = sessionService.createSessionAndPersist(user, (session) -> {
session.setAttribute("channelId", String.valueOf(channelId));
session.setAttribute("createdFrom", String.valueOf(createdFrom));
session.setAttribute("appChannel", String.valueOf(appChannel));
});
result.put("success", JsonResult.buildSuccessResult("", phoneNo));
return result;
}
/**
* 认证通过吐出的成功结果
* @param phoneNo
* @param token
* @return
*/
private JsonResult returnSuccessResult(String phoneNo, String token){
AuthBean authBean = new AuthBean();
authBean.setPhoneNo(user.getPhoneNo());
authBean.setToken(sessionStruct.getSid());
authBean.setPhoneNo(phoneNo);
authBean.setToken(token);
return new JsonResult(authBean);
}
......
......@@ -25,9 +25,6 @@ public class Channel implements Serializable {
@Column(name = "name")
private String name;
@Column(name = "strategy_name")
private String strategyName;
public Long getId() {
return id;
}
......@@ -52,13 +49,6 @@ public class Channel implements Serializable {
this.name = name;
}
public String getStrategyName() {
return strategyName;
}
public void setStrategyName(String strategyName) {
this.strategyName = strategyName;
}
@Override
public String toString() {
......@@ -66,7 +56,6 @@ public class Channel implements Serializable {
"channelCode='" + channelCode + '\'' +
", id=" + id +
", name='" + name + '\'' +
", strategyName='" + strategyName + '\'' +
'}';
}
}
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