Commit 2a78d659 authored by zhouqian's avatar zhouqian

refactor

parent 270c84d0
...@@ -60,59 +60,20 @@ public class UserController implements IBaseController { ...@@ -60,59 +60,20 @@ public class UserController implements IBaseController {
@Autowired @Autowired
private IUserDetailService userDetailService; private IUserDetailService userDetailService;
/*@Autowired
private IUserDetailRepository userDetailRepository;*/
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'};
@RequestMapping("/login") @RequestMapping("/login")
public JsonResult login2(@RequestParam(required = false, defaultValue = "") String userId, HttpServletRequest request) { public JsonResult login(
AuthBean authBean = new AuthBean(); @RequestParam(required = false, defaultValue = "1") Long channelId, String appChannel,
if(!StringUtils.isEmpty(userId)){ @RequestParam(required = false, defaultValue = "1") Long createdFrom,
//查询用户,存在则保存用户session信息,userId为uuid @RequestParam(required = false, defaultValue = "") String userId, HttpServletRequest request) {
User user = userService.findByUuidInDb(userId); if(!StringUtils.isEmpty(userId) && userId.length() > 10) {
//用户信息存在,更新session中的最后访问时间,重新写入缓存. return loginWithUserId(channelId, appChannel, createdFrom, userId);
if(null != user){ } else {
SessionStruct sessionStruct = sessionService.createSessionAndPersistInCache(user); return loginWithHttpBasic(channelId, appChannel, createdFrom, request);
authBean.setPhoneNo(user.getPhoneNo());
authBean.setToken(sessionStruct.getSid());
}
}else {
String credential = request.getHeader("authorization");
if(!credential.startsWith("Basic ")){
return JsonResult.buildErrorStateResult("用户名或密码不正确", null);
}
credential = credential.substring("Basic ".length(), credential.length());
byte[] buf = Base64.decodeBase64(credential);
String bufStr = "";
try {
bufStr = new String(buf, "UTF-8");
} catch (UnsupportedEncodingException e){
LOGGER.error("不支持的编码: ", e);
}
String[] credentialArr = bufStr.split(":");
if(credentialArr.length != 2){
return JsonResult.buildErrorStateResult("用户名或密码不正确.", null);
}
String userName = credentialArr[0];
String pass = credentialArr[1];
User user = userService.findByPhoneWithCache(userName);
if(user == null){
return JsonResult.buildErrorStateResult("用户名或密码不正确", null);
}
//验证密码
if (!validatePassword(pass, user.getPassword())) {
return JsonResult.buildErrorStateResult("用户名或密码不正确", null);
}
//找到用户
SessionStruct sessionStruct = sessionService.createSessionAndPersistInCache(user);
LOGGER.info("用户登录成功, loginFrom:{}, phoneNo:{},appChannel:{}", getCreatedFrom(), user.getPhoneNo(), getAppChannel());
authBean.setPhoneNo(user.getPhoneNo());
authBean.setToken(sessionStruct.getSid());
} }
return new JsonResult(authBean);
} }
@RequestMapping("/test") @RequestMapping("/test")
...@@ -121,10 +82,6 @@ public class UserController implements IBaseController { ...@@ -121,10 +82,6 @@ public class UserController implements IBaseController {
return JsonResult.buildSuccessResult("", getCurrentUser()); return JsonResult.buildSuccessResult("", getCurrentUser());
} }
private boolean validatePassword(String paramPass, String targetPassword){
return StringUtils.defaultString(targetPassword, "").equals(PasswordUtil.MD5(paramPass.toLowerCase() + pwdSalt));
}
@RequestMapping("/login/fast") @RequestMapping("/login/fast")
public JsonResult loginFast( public JsonResult loginFast(
@RequestParam(required = false, defaultValue = "1") Long channelId, String appChannel, @RequestParam(required = false, defaultValue = "1") Long channelId, String appChannel,
...@@ -146,12 +103,7 @@ public class UserController implements IBaseController { ...@@ -146,12 +103,7 @@ public class UserController implements IBaseController {
if(StringUtils.isBlank(sessionId)){ if(StringUtils.isBlank(sessionId)){
return JsonResult.buildErrorStateResult("登录失败", null); return JsonResult.buildErrorStateResult("登录失败", null);
} }
SessionStruct sessionStruct = sessionService.createSessionAndPersist(user, (session) -> { return createSession(channelId, createdFrom, appChannel, user);
session.setAttribute("channelId", String.valueOf(channelId));
session.setAttribute("createdFrom", String.valueOf(createdFrom));
session.setAttribute("appChannel", String.valueOf(appChannel));
});
return returnSuccessResult(phoneNo, sessionStruct.getSid());
} }
/** /**
...@@ -160,7 +112,7 @@ public class UserController implements IBaseController { ...@@ -160,7 +112,7 @@ public class UserController implements IBaseController {
* @return * @return
*/ */
private Map<String, JsonResult> getHeaderParam(HttpServletRequest request){ private Map<String, JsonResult> getHeaderParam(HttpServletRequest request){
Map<String, JsonResult> result = new HashMap(); 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)){
...@@ -195,19 +147,6 @@ public class UserController implements IBaseController { ...@@ -195,19 +147,6 @@ public class UserController implements IBaseController {
return result; return result;
} }
/**
* 认证通过吐出的成功结果
* @param phoneNo
* @param token
* @return
*/
private JsonResult returnSuccessResult(String phoneNo, String token){
AuthBean authBean = new AuthBean();
authBean.setPhoneNo(phoneNo);
authBean.setToken(token);
return new JsonResult(authBean);
}
/** /**
* 用户快速注册 * 用户快速注册
* *
...@@ -425,4 +364,72 @@ public class UserController implements IBaseController { ...@@ -425,4 +364,72 @@ public class UserController implements IBaseController {
} }
private JsonResult loginWithHttpBasic( Long channelId, String appChannel, Long createdFrom, HttpServletRequest request) {
User user = verificateUserNameAndPassword(request);
if (user == null) {
return JsonResult.buildErrorStateResult("用户名或密码不正确", null);
} else {
return createSession(channelId, createdFrom, appChannel, user);
}
}
private JsonResult createSession(Long channelId, Long createdFrom, String appChannel, User user) {
AuthBean authBean = new AuthBean();
//找到用户
SessionStruct sessionStruct = sessionService.createSessionAndPersist(user, (session) -> {
session.setAttribute("channelId", String.valueOf(channelId));
session.setAttribute("createdFrom", String.valueOf(createdFrom));
session.setAttribute("appChannel", String.valueOf(appChannel));
});
authBean.setPhoneNo(user.getPhoneNo());
authBean.setToken(sessionStruct.getSid());
LOGGER.info("用户登录成功, loginFrom:{}, phoneNo:{},appChannel:{}", createdFrom, user.getPhoneNo(), appChannel);
return new JsonResult(authBean);
}
private User verificateUserNameAndPassword(HttpServletRequest request) {
String credential = request.getHeader("authorization");
if(!credential.startsWith("Basic ")){
return null;
}
credential = credential.substring("Basic ".length(), credential.length());
byte[] buf = Base64.decodeBase64(credential);
String bufStr = "";
try {
bufStr = new String(buf, "UTF-8");
} catch (UnsupportedEncodingException e){
LOGGER.error("不支持的编码: ", e);
}
String[] credentialArr = bufStr.split(":");
if(credentialArr.length != 2){
return null;
}
String userName = credentialArr[0];
String pass = credentialArr[1];
User user = userService.findByPhoneWithCache(userName);
if(user == null){
return null;
}
//验证密码
if (!validatePassword(pass, user.getPassword())) {
return null;
}
return user;
}
private boolean validatePassword(String paramPass, String targetPassword){
return StringUtils.defaultString(targetPassword, "").equals(PasswordUtil.MD5(paramPass.toLowerCase() + pwdSalt));
}
private JsonResult loginWithUserId(Long channelId, String appChannel, Long createdFrom, String userId) {
//查询用户,存在则保存用户session信息,userId为uuid
User user = userService.findByUuidInDb(userId);
//用户信息存在,更新session中的最后访问时间,重新写入缓存.
if(null != user) {
return createSession(channelId, createdFrom, appChannel, user);
} else {
return JsonResult.buildErrorStateResult("登录失败", null);
}
}
} }
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