Commit f1524c3a authored by zhouqian's avatar zhouqian

session管理

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