|
|
@@ -13,6 +13,8 @@ import com.ruoyi.dz.domain.DzCards;
|
|
|
import com.ruoyi.dz.service.IDzCardsService;
|
|
|
import com.ruoyi.dz.service.impl.DzCardsServiceImpl;
|
|
|
import com.ruoyi.framework.web.service.TokenService;
|
|
|
+import com.ruoyi.system.domain.ZuserToken;
|
|
|
+import com.ruoyi.system.service.IZuserTokenService;
|
|
|
import com.ruoyi.system.service.ShortMessageService;
|
|
|
import com.ruoyi.web.controller.dz.DzCardsController;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
@@ -45,9 +47,8 @@ import com.ruoyi.system.service.ISysConfigService;
|
|
|
import com.ruoyi.system.service.ISysUserService;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 登录校验方法
|
|
|
@@ -77,6 +78,10 @@ public class SysLoginService
|
|
|
@Autowired
|
|
|
private IDzCardsService cardsService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private IZuserTokenService zuserTokenService;
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 登录验证
|
|
|
* @param mobile
|
|
|
@@ -162,13 +167,115 @@ public class SysLoginService
|
|
|
}
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
|
|
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
|
|
- recordLoginInfo(loginUser.getUserId());
|
|
|
+ Long userId = loginUser.getUserId();
|
|
|
+ recordLoginInfo(userId);
|
|
|
// 生成token
|
|
|
AjaxResult ajax = AjaxResult.success();
|
|
|
- ajax.put(Constants.TOKEN, tokenService.createToken(loginUser));
|
|
|
+
|
|
|
+ //单点登录
|
|
|
+ resetTokensByUserIds(Arrays.asList(userId),true);
|
|
|
+
|
|
|
+ // 生成token
|
|
|
+ String token = tokenService.createToken(loginUser);
|
|
|
+
|
|
|
+ ZuserToken userToken = new ZuserToken();
|
|
|
+ userToken.setUserName(StringUtils.isEmpty(type)?username:(type+":"+username));
|
|
|
+ userToken.setUserId(userId);
|
|
|
+ userToken.setToken(token);
|
|
|
+ userToken.setUpdateTime(new Date());
|
|
|
+ zuserTokenService.insertZuserToken(userToken);
|
|
|
+
|
|
|
+ ajax.put(Constants.TOKEN, token);
|
|
|
+
|
|
|
return ajax;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 此处不使用resetTokens是为了与志愿保持一致
|
|
|
+ * 重置指定用户的登陆信息,强制重新登陆系统
|
|
|
+ * @param userIds
|
|
|
+ * @param isSso:适配志愿中锁分时也要清空用户token信息,此时设置为false,直接清空用户的token,设置为true就表示走单点登录的逻辑
|
|
|
+ * @param isSetSso:后台配置的单点登录配置,设置为true时启用单点登录,为false时不启用单点登录
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public AjaxResult resetTokensByUserIds(List<Long> userIds,Boolean isSso) {
|
|
|
+ if (isSso){
|
|
|
+ String isSetSso = configService.selectConfigByKey( "is.set.sso");
|
|
|
+ if (StringUtils.isBlank(isSetSso)||!Boolean.valueOf(isSetSso)){
|
|
|
+ //参数配置的单点登录为false,此时不设置单点登录(将userids设置为空就不走清空流程),不需要清空用户已登录的token
|
|
|
+ userIds = new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ Set<String> excludeUserIdSet=new HashSet<>();
|
|
|
+ String ssoWhiteListUserName = configService.selectConfigByKey("sso.white.list");
|
|
|
+ if(StringUtils.isNotBlank(ssoWhiteListUserName)){
|
|
|
+ Arrays.asList(ssoWhiteListUserName.split(",")).forEach(username->{
|
|
|
+ excludeUserIdSet.add(username);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (CollectionUtils.isNotEmpty(excludeUserIdSet)){
|
|
|
+ List<Long> excludeUserIds = userService.selectUsers(null,null,excludeUserIdSet).stream().map(SysUser::getUserId).collect(Collectors.toList());
|
|
|
+ //userIds减去excludeUserIds
|
|
|
+ // 将排除的用户ID转化为Set,以便高效查找
|
|
|
+ Set<Long> excludeSet = new HashSet<>(excludeUserIds);
|
|
|
+
|
|
|
+ // 使用Stream API过滤掉排除的用户ID
|
|
|
+ userIds = userIds.stream().filter(userId -> !excludeSet.contains(userId)).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return resetTokensByUserIds(userIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 重置指定用户的登陆信息,强制重新登陆系统
|
|
|
+ * @param startUserList
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public AjaxResult resetTokensByUserIds(List<Long> userIds) {
|
|
|
+ if(CollectionUtils.isEmpty(userIds)){
|
|
|
+ return AjaxResult.success();
|
|
|
+ }
|
|
|
+ ZuserToken zuserToken = new ZuserToken();
|
|
|
+ zuserToken.setUserIds(userIds);
|
|
|
+ List<ZuserToken> zuserTokenList = zuserTokenService.selectTopZuserTokenList(zuserToken);
|
|
|
+ for (ZuserToken zt : zuserTokenList) {
|
|
|
+ if(StringUtils.isNotNull(zt)){
|
|
|
+ tokenService.removeUserToken(zt.getToken());
|
|
|
+// zuserTokenService.deleteZuserTokenById(zt.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<Long> tokenIdList = zuserTokenList.stream().map(ZuserToken::getId).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
|
|
|
+ Long[] tokenIds = tokenIdList.toArray(new Long[tokenIdList.size()]);
|
|
|
+ if(tokenIds.length>0){
|
|
|
+ zuserTokenService.deleteZuserTokenByIds(tokenIds);
|
|
|
+ }
|
|
|
+ return AjaxResult.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ public AjaxResult resetTokens(List<SysUser> startUserList) {
|
|
|
+ List<Long> userIds = startUserList.stream().map(SysUser::getUserId).collect(Collectors.toList());
|
|
|
+ return resetTokensByUserIds(userIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 重置指定用户的登陆信息,强制重新登陆系统
|
|
|
+ * @param userName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public AjaxResult resetTokens(String userName) {
|
|
|
+ ZuserToken zuserToken = new ZuserToken();
|
|
|
+ zuserToken.setUserName(userName);
|
|
|
+ List<ZuserToken> zuserTokenList = zuserTokenService.selectTopZuserTokenList(zuserToken);
|
|
|
+ for (ZuserToken zt : zuserTokenList) {
|
|
|
+ if(StringUtils.isNotNull(zt)){
|
|
|
+ tokenService.removeUserToken(zt.getToken());
|
|
|
+ zuserTokenService.deleteZuserTokenById(zt.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return AjaxResult.success();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 内部自动登陆,不记录日志
|
|
|
* @param username
|