Commit f1524c3a authored by zhouqian's avatar zhouqian

session管理

parent d892c3da
......@@ -26,6 +26,12 @@
</properties>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
......
......@@ -46,7 +46,8 @@ public interface Constants {
}
interface Session{
String USER_SESSIONID_CACHE = "user-sessionvalue:cache:";
String USER_SESSION_CACHE = "user:session:";
String USER_SESSION_ID_CACHE = "userid-sessionvalue:cache:";
Long ONE_DAY = 24 * 60 * 60L;
}
......
......@@ -99,11 +99,8 @@ public class UserController implements IBaseController {
throw new UserNotExistException("用户未找到");
}
}
String sessionId = sessionService.findSessionIdByUserId(user.getId());
if(StringUtils.isBlank(sessionId)){
return JsonResult.buildErrorStateResult("登录失败", null);
}
return createSession(channelId, createdFrom, appChannel, user);
// return createSession(channelId, createdFrom, appChannel, user);
}
/**
......@@ -376,6 +373,18 @@ public class UserController implements IBaseController {
private JsonResult createSession(Long channelId, Long createdFrom, String appChannel, User user) {
AuthBean authBean = new AuthBean();
//找到用户
String sessionId = sessionService.findSessionIdByUserId(user.getId());
if(StringUtils.isNotEmpty(sessionId)) {
SessionStruct sessionStruct = sessionService.findSessionBySessionId(sessionId);
sessionStruct.setAttribute("channelId", String.valueOf(channelId));
sessionStruct.setAttribute("createdFrom", String.valueOf(createdFrom));
sessionStruct.setAttribute("appChannel", String.valueOf(appChannel));
authBean.setPhoneNo(user.getPhoneNo());
authBean.setToken(sessionStruct.getSid());
sessionService.persistSession(sessionStruct.getSid(), sessionStruct.getValues());
LOGGER.info("用户登录成功, loginFrom:{}, phoneNo:{},appChannel:{}", createdFrom, user.getPhoneNo(), appChannel);
return new JsonResult(authBean);
}
SessionStruct sessionStruct = sessionService.createSessionAndPersist(user, (session) -> {
session.setAttribute("channelId", String.valueOf(channelId));
session.setAttribute("createdFrom", String.valueOf(createdFrom));
......
......@@ -68,7 +68,7 @@ public class RequestFilter implements Filter {
private boolean isMatch(String path) {
for (String pattern : ALLOWED_PATTERNS) {
if (matcher.match(path, pattern)) {
if (matcher.match(pattern, path)) {
return true;
}
}
......
......@@ -17,4 +17,6 @@ public interface ISessionService {
String findSessionValueBySessionId(String sessionId);
SessionStruct newSession(User user);
void persistSession(String token, SessionValue sessionValue);
SessionStruct findSessionBySessionId(String sessionId);
}
......@@ -17,6 +17,7 @@ import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
*
......@@ -62,12 +63,12 @@ public class SessionServiceImpl implements ISessionService{
@Override
public String findSessionIdByUserId(Long userId) {
return stringRedisTemplate.opsForValue().get(Constants.Session.USER_SESSIONID_CACHE + userId.toString());
return stringRedisTemplate.opsForValue().get(Constants.Session.USER_SESSION_ID_CACHE + userId.toString());
}
@Override
public String findSessionValueBySessionId(String sessionId){
String result = stringRedisTemplate.opsForValue().get(Constants.Session.USER_SESSIONID_CACHE + sessionId);
String result = stringRedisTemplate.opsForValue().get(Constants.Session.USER_SESSION_CACHE + sessionId);
return StringUtils.defaultString(result, "");
}
......@@ -91,6 +92,32 @@ public class SessionServiceImpl implements ISessionService{
Timestamp current = new Timestamp(System.currentTimeMillis());
sessionValue.setLastAccessTime(current);
String json = JSON.toJSONString(sessionValue);
stringRedisTemplate.opsForValue().set(Constants.Session.USER_SESSIONID_CACHE + token, json, Constants.Session.ONE_DAY);
stringRedisTemplate.opsForValue().set(Constants.Session.USER_SESSION_CACHE + token, json,
Constants.Session.ONE_DAY, TimeUnit.SECONDS);
stringRedisTemplate.opsForValue().set(Constants.Session.USER_SESSION_ID_CACHE +
sessionValue.getUser().getId().toString(), token, Constants.Session.ONE_DAY, TimeUnit.SECONDS);
}
@Override
public SessionStruct findSessionBySessionId(String sessionId) {
String sessionValue = findSessionValueBySessionId(sessionId);
if(StringUtils.isEmpty(sessionValue)) {
return null;
}
try {
SessionValue value = JSON.parseObject(sessionValue, SessionValue.class);
if (null == value) {
return null;
}
SessionStruct struct = new SessionStruct();
struct.setSid(sessionId);
struct.setValues(value);
return struct;
} catch (Exception ex) {
return null;
}
}
}
......@@ -76,7 +76,7 @@ public class SmsServiceImpl implements ISmsService {
@Override
public boolean validateFastLoginVerificationCode(String phoneNo, String verificationCode){
String key = SMS_VERIFY_PREFIX + phoneNo;
String key = Constants.REDIS_PREFIX_VERIFICATION_CODE + phoneNo;
String randomCode = stringRedisTemplate.opsForValue().get(key);
if(StringUtils.isBlank(randomCode)){
return false;
......
......@@ -31,7 +31,7 @@ public class XyqbSessionContextHolder {
if (token == null || token.length() != 36) {
return null;
}
String result = redisTemplate.opsForValue().get(Constants.Session.USER_SESSIONID_CACHE + token);
String result = redisTemplate.opsForValue().get(Constants.Session.USER_SESSION_CACHE + token);
if (StringUtils.isEmpty(result)) {
return 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