Commit ff894e42 authored by 常学朋's avatar 常学朋

Merge branch 'feature-202509191033-userSession' into 'master'

Feature 202509191033 user session

See merge request !126
parents c2ef13e0 2011ee1c
...@@ -6,6 +6,8 @@ package cn.quantgroup.xyqb; ...@@ -6,6 +6,8 @@ package cn.quantgroup.xyqb;
*/ */
public interface Constants { public interface Constants {
String BIAN_LI_KA_BA_TOKEN_PREFIX = "jr-";
// zero fill with 4 chars... // zero fill with 4 chars...
String ZERO_FILL_TEMPLATE = "%04d"; String ZERO_FILL_TEMPLATE = "%04d";
String IMAGE_CAPTCHA_KEY = "img_captcha:"; String IMAGE_CAPTCHA_KEY = "img_captcha:";
......
...@@ -620,6 +620,13 @@ public class UserController implements IBaseController { ...@@ -620,6 +620,13 @@ public class UserController implements IBaseController {
log.info("当前token对应的用户非当前租户,userId:{},用户tenantId:{},入参tenantId:{},token:{}", user.getId(), user.getTenantId(), tenantId, token); log.info("当前token对应的用户非当前租户,userId:{},用户tenantId:{},入参tenantId:{},token:{}", user.getId(), user.getTenantId(), tenantId, token);
return JsonResult.buildSuccessResult(null, tokenExchange); return JsonResult.buildSuccessResult(null, tokenExchange);
} }
if(Objects.nonNull(user) && Objects.nonNull(user.getId())){
User userDb = userService.findById(user.getId(), tenantId);
if(Objects.isNull(userDb)){
log.error("当前token对应的用户不存在,userId:{},用户tenantId:{},入参tenantId:{},token:{}", user.getId(), user.getTenantId(), tenantId, token);
return JsonResult.buildSuccessResult(null, tokenExchange);
}
}
tokenExchange.setLoginProperties(JSONObject.toJSONString(sessionStruct.getValues().getLoginProperties())); tokenExchange.setLoginProperties(JSONObject.toJSONString(sessionStruct.getValues().getLoginProperties()));
tokenExchange.setUserId(user.getId()); tokenExchange.setUserId(user.getId());
......
...@@ -54,5 +54,6 @@ public interface IUserRepository extends JpaRepository<User, Long>, JpaSpecifica ...@@ -54,5 +54,6 @@ public interface IUserRepository extends JpaRepository<User, Long>, JpaSpecifica
* *
* @param userId 用户id * @param userId 用户id
*/ */
@Transactional
void deleteByIdAndTenantId(Long userId,Integer tenantId); void deleteByIdAndTenantId(Long userId,Integer tenantId);
} }
...@@ -3,6 +3,7 @@ package cn.quantgroup.xyqb.repository; ...@@ -3,6 +3,7 @@ package cn.quantgroup.xyqb.repository;
import cn.quantgroup.xyqb.entity.UserTag; import cn.quantgroup.xyqb.entity.UserTag;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.transaction.annotation.Transactional;
import java.util.List; import java.util.List;
...@@ -11,5 +12,6 @@ public interface IUserTagRepository extends JpaRepository<UserTag, Long>, JpaSpe ...@@ -11,5 +12,6 @@ public interface IUserTagRepository extends JpaRepository<UserTag, Long>, JpaSpe
List<UserTag> findByUserIdInAndTenantId(List<Long> userIds, Integer tenantId); List<UserTag> findByUserIdInAndTenantId(List<Long> userIds, Integer tenantId);
@Transactional
void deleteByUserIdAndTenantId(Long userId, Integer tenantId); void deleteByUserIdAndTenantId(Long userId, Integer tenantId);
} }
...@@ -15,6 +15,7 @@ import cn.quantgroup.xyqb.service.http.IHttpService; ...@@ -15,6 +15,7 @@ import cn.quantgroup.xyqb.service.http.IHttpService;
import cn.quantgroup.xyqb.service.session.ISessionService; import cn.quantgroup.xyqb.service.session.ISessionService;
import cn.quantgroup.xyqb.service.session.aspect.UserBtRegisterFill; import cn.quantgroup.xyqb.service.session.aspect.UserBtRegisterFill;
import cn.quantgroup.xyqb.service.user.ILoginRecordService; import cn.quantgroup.xyqb.service.user.ILoginRecordService;
import cn.quantgroup.xyqb.service.user.IUserService;
import cn.quantgroup.xyqb.util.MqUtils; import cn.quantgroup.xyqb.util.MqUtils;
import cn.quantgroup.xyqb.util.TenantUtil; import cn.quantgroup.xyqb.util.TenantUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
...@@ -48,6 +49,8 @@ public class SessionServiceImpl implements ISessionService { ...@@ -48,6 +49,8 @@ public class SessionServiceImpl implements ISessionService {
@Resource @Resource
private ILoginRecordService loginRecordService; private ILoginRecordService loginRecordService;
@Resource
private IUserService userService;
@Value("${token.prefix}") @Value("${token.prefix}")
private String prefix; private String prefix;
...@@ -209,6 +212,11 @@ public class SessionServiceImpl implements ISessionService { ...@@ -209,6 +212,11 @@ public class SessionServiceImpl implements ISessionService {
@Override @Override
public void persistSession(String token, SessionValue sessionValue, Long time, Integer tenantId) { public void persistSession(String token, SessionValue sessionValue, Long time, Integer tenantId) {
//
if(token.startsWith(Constants.BIAN_LI_KA_BA_TOKEN_PREFIX)){
log.info("token={},命中不能续期逻辑 id={}",token,JSON.toJSONString(sessionValue));
return;
}
Timestamp current = new Timestamp(System.currentTimeMillis()); Timestamp current = new Timestamp(System.currentTimeMillis());
if (sessionValue == null) { if (sessionValue == null) {
sessionValue = new SessionValue(); sessionValue = new SessionValue();
...@@ -235,6 +243,16 @@ public class SessionServiceImpl implements ISessionService { ...@@ -235,6 +243,16 @@ public class SessionServiceImpl implements ISessionService {
stringRedisTemplate.opsForValue().set(key, json, stringRedisTemplate.opsForValue().set(key, json,
time, TimeUnit.SECONDS); time, TimeUnit.SECONDS);
if (sessionValue.getUser() != null) { if (sessionValue.getUser() != null) {
User user = sessionValue.getUser();
// 只缓存存在的用户
if(Objects.nonNull(user.getId())){
User userDb = userService.findById(user.getId(), tenantId);
if(Objects.isNull(userDb)){
log.error("当前token对应的用户不存在,userId:{},用户tenantId:{},入参tenantId:{},token:{}", user.getId(), user.getTenantId(), tenantId, token);
}else{
log.info("[当前token对应的用户存在],userId:{},用户tenantId:{},入参tenantId:{},token:{}", user.getId(), user.getTenantId(), tenantId, token);
}
}
String generateLoginPropertiesKey = generateLoginPropertiesKey(sessionValue.getUser().getId(), sessionValue.getLoginProperties(), tenantId); String generateLoginPropertiesKey = generateLoginPropertiesKey(sessionValue.getUser().getId(), sessionValue.getLoginProperties(), tenantId);
stringRedisTemplate.opsForValue().set(generateLoginPropertiesKey, token, time, TimeUnit.SECONDS); stringRedisTemplate.opsForValue().set(generateLoginPropertiesKey, token, time, TimeUnit.SECONDS);
log.info("[Session生命期延续],token:{},有效期:[24Hour]", token); log.info("[Session生命期延续],token:{},有效期:[24Hour]", token);
......
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