Commit 5be2ba6b authored by xiaoguang.xu's avatar xiaoguang.xu

15 天token 有效期

parent bbe7f477
......@@ -172,7 +172,7 @@ public interface Constants {
String USER_SESSION_CACHE = "user:session:";
String USER_SESSION_ID_CACHE = "userid-sessionvalue:cache:";
String USER_SESSION_KEY_SET = "userid-keys:set:";
Long ONE_DAY = 24 * 60 * 60L;
Long SESSION_VALID_TIME = 15 * 24 * 60 * 60L;
}
interface UserAvatar {
......
......@@ -66,8 +66,11 @@ public class UserApiController {
@ApiOperation(notes = "检查token是否有效,如果有效,可选择是否延续生命期(延续后有效期24Hour)", value = "Check token and then prolong session", nickname = "checkToken")
@IpValidator
@RequestMapping(value = "/valid/{token}", method = RequestMethod.POST)
public JsonResult checkToken(@ApiParam(value = "sid,session的id", required = true) @PathVariable("token") String token,
@ApiParam(value = "是否延续生命期,可选参数,默认为: false - 不延续") @RequestParam(name = "prolong", required = false, defaultValue = "false") Boolean prolong) {
public JsonResult checkToken(@ApiParam(value = "sid,session的id", required = true)
@PathVariable("token") String token,
@ApiParam(value = "是否延续生命期,可选参数,默认为: false - 不延续")
@RequestParam(name = "prolong", required = false, defaultValue = "false") Boolean prolong,
@RequestParam(name = "prolongTime", required = false, defaultValue = "86400") Long prolongTime) {
if (Objects.isNull(token) || !ValidationUtil.validateToken(token)) {
return JsonResult.buildErrorStateResult("token invalid", token);
}
......@@ -87,7 +90,7 @@ public class UserApiController {
} else {
/* 延续session生命期 */
try {
sessionService.persistSession(sessionStruct.getSid(), sessionStruct.getValues());
sessionService.persistSession(sessionStruct.getSid(), sessionStruct.getValues(), prolongTime);
log.info("延续token:[{}]生命期,result:[{}]", token, true);
} finally {
XyqbSessionContextHolder.releaseSession();
......
......@@ -27,6 +27,7 @@ public interface ISessionService {
SessionStruct createSessionAndPersist(User user, LoginProperties loginProperties);
void persistSession(String token, SessionValue sessionValue);
void persistSession(String token, SessionValue sessionValue,Long time);
void deleteByUserId(long userId);
......
......@@ -136,16 +136,20 @@ public class SessionServiceImpl implements ISessionService {
@Override
@UserBtRegisterFill
public void persistSession(String token, SessionValue sessionValue) {
persistSession(token, sessionValue, Constants.Session.SESSION_VALID_TIME);
}
@Override
public void persistSession(String token, SessionValue sessionValue, Long time) {
Timestamp current = new Timestamp(System.currentTimeMillis());
sessionValue.setLastAccessTime(current);
String json = JSON.toJSONString(sessionValue);
stringRedisTemplate.opsForValue().set(Constants.Session.USER_SESSION_CACHE + token, json,
Constants.Session.ONE_DAY, TimeUnit.SECONDS);
time, TimeUnit.SECONDS);
String key = generateLoginPropertiesKey(sessionValue.getUser().getId(), sessionValue.getLoginProperties());
stringRedisTemplate.opsForValue().set(key, token, Constants.Session.ONE_DAY, TimeUnit.SECONDS);
stringRedisTemplate.opsForValue().set(key, token, time, TimeUnit.SECONDS);
log.info("[Session生命期延续],token:{},有效期:[24Hour]", token);
setUserIdTokenKeys(sessionValue.getUser().getId(), key);
}
/**
......@@ -159,7 +163,7 @@ public class SessionServiceImpl implements ISessionService {
String setKey = getUserSessionSetKey(userId);
try {
stringRedisTemplate.opsForSet().add(setKey, key);
stringRedisTemplate.expire(setKey, Constants.Session.ONE_DAY, TimeUnit.SECONDS);
stringRedisTemplate.expire(setKey, Constants.Session.SESSION_VALID_TIME, TimeUnit.SECONDS);
} catch (Exception e) {
log.error("存储用户注销件失败,userId:{},Exception:{}", userId, e);
}
......
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