Commit 178a551d authored by lee_mingzhu's avatar lee_mingzhu

change something

parent 611f0d85
...@@ -28,6 +28,8 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -28,6 +28,8 @@ 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.util.HashMap;
import java.util.Map;
import java.util.Random; import java.util.Random;
/** /**
...@@ -66,7 +68,18 @@ public class UserController implements IBaseController { ...@@ -66,7 +68,18 @@ public class UserController implements IBaseController {
'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'};
@RequestMapping("/login") @RequestMapping("/login")
public JsonResult login2(HttpServletRequest request) { public JsonResult login2(@RequestParam(required = false, defaultValue = "") String userId, HttpServletRequest request) {
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"); String credential = request.getHeader("authorization");
if(!credential.startsWith("Basic ")){ if(!credential.startsWith("Basic ")){
return JsonResult.buildErrorStateResult("用户名或密码不正确", null); return JsonResult.buildErrorStateResult("用户名或密码不正确", null);
...@@ -96,9 +109,9 @@ public class UserController implements IBaseController { ...@@ -96,9 +109,9 @@ public class UserController implements IBaseController {
//找到用户 //找到用户
SessionStruct sessionStruct = sessionService.createSessionAndPersistInCache(user); SessionStruct sessionStruct = sessionService.createSessionAndPersistInCache(user);
LOGGER.info("用户登录成功, loginFrom:{}, phoneNo:{},appChannel:{}", getCreatedFrom(), user.getPhoneNo(), getAppChannel()); LOGGER.info("用户登录成功, loginFrom:{}, phoneNo:{},appChannel:{}", getCreatedFrom(), user.getPhoneNo(), getAppChannel());
AuthBean authBean = new AuthBean();
authBean.setPhoneNo(user.getPhoneNo()); authBean.setPhoneNo(user.getPhoneNo());
authBean.setToken(sessionStruct.getSid()); authBean.setToken(sessionStruct.getSid());
}
return new JsonResult(authBean); return new JsonResult(authBean);
} }
...@@ -116,13 +129,45 @@ public class UserController implements IBaseController { ...@@ -116,13 +129,45 @@ public class UserController implements IBaseController {
public JsonResult loginFast( public JsonResult loginFast(
@RequestParam(required = false, defaultValue = "1") Long channelId, String appChannel, @RequestParam(required = false, defaultValue = "1") Long channelId, String appChannel,
@RequestParam(required = false, defaultValue = "1") Long createdFrom, HttpServletRequest request){ @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 verificationHeader = "Verification ";
String credential = request.getHeader("authorization"); String credential = request.getHeader("authorization");
if(StringUtils.isBlank(credential)){ if(StringUtils.isBlank(credential)){
return JsonResult.buildErrorStateResult("登录失败", null); result.put("fail", JsonResult.buildErrorStateResult("登录失败", null));
} }
if(!credential.startsWith(verificationHeader)){ if(!credential.startsWith(verificationHeader)){
return JsonResult.buildErrorStateResult("登录失败", null); result.put("fail", JsonResult.buildErrorStateResult("登录失败", null));
} }
credential = credential.substring(verificationHeader.length(), credential.length()); credential = credential.substring(verificationHeader.length(), credential.length());
byte[] buf = Base64.decodeBase64(credential); byte[] buf = Base64.decodeBase64(credential);
...@@ -130,41 +175,36 @@ public class UserController implements IBaseController { ...@@ -130,41 +175,36 @@ public class UserController implements IBaseController {
credential = new String(buf, "UTF-8"); credential = new String(buf, "UTF-8");
} catch (UnsupportedEncodingException e){ } catch (UnsupportedEncodingException e){
LOGGER.error("不支持的编码."); LOGGER.error("不支持的编码.");
return JsonResult.buildErrorStateResult("登录失败", null); result.put("fail", JsonResult.buildErrorStateResult("登录失败", null));
} }
String[] credentialArr = credential.split(":"); String[] credentialArr = credential.split(":");
if(credentialArr.length != 2){ if(credentialArr.length != 2){
return JsonResult.buildErrorStateResult("登录失败", null); result.put("fail", JsonResult.buildErrorStateResult("登录失败", null));
} }
String phoneNo = credentialArr[0]; String phoneNo = credentialArr[0];
String verificationCode = credentialArr[1]; String verificationCode = credentialArr[1];
LOGGER.info("用户快速登录,phoneNo:{} , verificationCode:{}", phoneNo, verificationCode); LOGGER.info("用户快速登录,phoneNo:{} , verificationCode:{}", phoneNo, verificationCode);
if(!ValidationUtil.validatePhoneNo(phoneNo)){ if(!ValidationUtil.validatePhoneNo(phoneNo)){
return JsonResult.buildErrorStateResult("登录失败", null); result.put("fail", JsonResult.buildErrorStateResult("登录失败", null));
} }
if(!smsService.validateFastLoginVerificationCode(phoneNo, verificationCode)){ if(!smsService.validateFastLoginVerificationCode(phoneNo, verificationCode)){
LOGGER.info("用户快速登录,验证码校验失败,phoneNo:{} , verificationCode:{}", phoneNo, verificationCode); LOGGER.info("用户快速登录,验证码校验失败,phoneNo:{} , verificationCode:{}", phoneNo, verificationCode);
return JsonResult.buildErrorStateResult("验证码错误", null); result.put("fail", 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("success", JsonResult.buildSuccessResult("", phoneNo));
return result;
} }
String sessionId = sessionService.findSessionIdByUserId(user.getId());
if(StringUtils.isBlank(sessionId)){ /**
return JsonResult.buildErrorStateResult("登录失败", null); * 认证通过吐出的成功结果
} * @param phoneNo
SessionStruct sessionStruct = sessionService.createSessionAndPersist(user, (session) -> { * @param token
session.setAttribute("channelId", String.valueOf(channelId)); * @return
session.setAttribute("createdFrom", String.valueOf(createdFrom)); */
session.setAttribute("appChannel", String.valueOf(appChannel)); private JsonResult returnSuccessResult(String phoneNo, String token){
});
AuthBean authBean = new AuthBean(); AuthBean authBean = new AuthBean();
authBean.setPhoneNo(user.getPhoneNo()); authBean.setPhoneNo(phoneNo);
authBean.setToken(sessionStruct.getSid()); authBean.setToken(token);
return new JsonResult(authBean); return new JsonResult(authBean);
} }
...@@ -384,5 +424,11 @@ public class UserController implements IBaseController { ...@@ -384,5 +424,11 @@ public class UserController implements IBaseController {
} }
public static void main(String[] args) {
Map<String, JsonResult> param = new HashMap<>();
param.put("success", JsonResult.buildSuccessResult("", "18500137564"));
System.out.println(param.get("success").getData());
}
} }
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