Commit c9a51659 authored by zhouqian's avatar zhouqian

abc

parent 4b5125ba
......@@ -14,10 +14,10 @@ import cn.quantgroup.xyqb.repository.IUserDetailRepository;
import cn.quantgroup.xyqb.service.session.ISessionService;
import cn.quantgroup.xyqb.service.sms.ISmsService;
import cn.quantgroup.xyqb.service.user.IUserService;
import cn.quantgroup.xyqb.util.PasswordUtil;
import cn.quantgroup.xyqb.util.ValidationUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.apache.tomcat.util.security.MD5Encoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -101,15 +101,20 @@ public class UserController implements IBaseController {
return JsonResult.buildErrorStateResult("用户名或密码不正确", null);
}
//验证密码
validatePassword(pass, user.getPassword());
if (!validatePassword(pass, user.getPassword())) {
return JsonResult.buildErrorStateResult("用户名或密码不正确", null);
}
//找到用户
SessionStruct sessionStruct = sessionService.createSession(user);
return JsonResult.buildSuccessResult("success", sessionStruct);
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);
}
private boolean validatePassword(String paramPass, String findPass){
String hashPass = MD5Encoder.encode((paramPass.toLowerCase() + pwdSalt).getBytes());
return hashPass == findPass;
private boolean validatePassword(String paramPass, String targetPassword){
return StringUtils.defaultString(targetPassword, "").equals(PasswordUtil.MD5(paramPass.toLowerCase() + pwdSalt));
}
/**
......@@ -118,18 +123,18 @@ public class UserController implements IBaseController {
* @param user
* @return
*/
@RequestMapping("/login")
public JsonResult login(Principal user) {
if (user == null) {
throw new NullUserException();
}
AuthBean bean = new AuthBean(getRequest().getSession().getId(), user);
LOGGER.info("用户登录成功, loginFrom:{}, phoneNo:{},appChannel:{}", getCreatedFrom(), user.getName(), getAppChannel());
return new JsonResult(bean);
}
// @RequestMapping("/login")
// public JsonResult login(Principal user) {
// if (user == null) {
// throw new NullUserException();
// }
//
// AuthBean bean = new AuthBean(getRequest().getSession().getId(), user);
//
// LOGGER.info("用户登录成功, loginFrom:{}, phoneNo:{},appChannel:{}", getCreatedFrom(), user.getName(), getAppChannel());
//
// return new JsonResult(bean);
// }
/**
......
......@@ -11,7 +11,7 @@ import cn.quantgroup.xyqb.model.session.SessionValue;
public interface ISessionService {
SessionStruct createSession(User user);
String findSessionIdByUserId(Long userId);
JsonResult findSessionValueBySessionId(String sessionId);
String findSessionValueBySessionId(String sessionId);
SessionStruct newSession(User user);
JsonResult PersistSession(String token, SessionValue sessionValue);
void PersistSession(String token, SessionValue sessionValue);
}
......@@ -6,9 +6,11 @@ import cn.quantgroup.xyqb.model.JsonResult;
import cn.quantgroup.xyqb.model.session.SessionStruct;
import cn.quantgroup.xyqb.model.session.SessionValue;
import cn.quantgroup.xyqb.service.session.ISessionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.sql.Timestamp;
import java.util.HashMap;
......@@ -16,11 +18,13 @@ import java.util.Map;
import java.util.UUID;
/**
*
* Created by 11 on 2016/12/28.
*/
@Service
public class SessionServiceImpl implements ISessionService{
public final int oneDay = 24 * 60 * 60;
private static final Long ONE_DAY = 24 * 60 * 60L;
@Autowired
@Qualifier("stringRedisTemplate")
......@@ -31,11 +35,10 @@ public class SessionServiceImpl implements ISessionService{
SessionStruct sessionStruct;
//获取sessionid
String sessionId = findSessionIdByUserId(user.getId());
if(sessionId.length() == 36){
if(StringUtils.length(sessionId) == 36){
sessionStruct = new SessionStruct();
//TODO 不确定
JsonResult result = findSessionValueBySessionId(sessionId);
if(result != null){
String sessionValue = findSessionValueBySessionId(sessionId);
if(StringUtils.isEmpty(sessionValue)) {
sessionStruct = newSession(user);
PersistSession(sessionStruct.getSid(), sessionStruct.getValues());
}
......@@ -54,12 +57,9 @@ public class SessionServiceImpl implements ISessionService{
}
@Override
public JsonResult findSessionValueBySessionId(String sessionId){
public String findSessionValueBySessionId(String sessionId){
String result = stringRedisTemplate.opsForValue().get("userid-sessionvalue:cache:" + sessionId);
if(result.length() == 0){
return JsonResult.buildErrorStateResult("wrong session Id", null, 0001L);
}
return null;
return StringUtils.defaultString(result, "");
}
@Override
......@@ -77,11 +77,12 @@ public class SessionServiceImpl implements ISessionService{
return sessionStruct;
}
public JsonResult PersistSession(String token, SessionValue sessionValue){
@Override
public void PersistSession(String token, SessionValue sessionValue){
Timestamp current = new Timestamp(System.currentTimeMillis());
sessionValue.setLastAccessTime(current);
// todo: session转换
String json = Constants.GSON.toJson(sessionValue);
stringRedisTemplate.opsForValue().set("userid-sessionvalue:cache:" + token, json, oneDay);
return JsonResult.buildSuccessResult("持久化session success.", null);
stringRedisTemplate.opsForValue().set("userid-sessionvalue:cache:" + token, json, ONE_DAY);
}
}
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