✨ Introducing new features. close #3598 【新特性】终端管理支持图形化配置登录是否重新生成令牌,控制是否允许同一账号是否可以多处

This commit is contained in:
qraddx committed 2022-02-05 17:10:33 +08:00
1 parent 048132d912
commit 2d2a995a83
4 files changed
+46 -5

No files matched your search

@@ -175,6 +175,11 @@ public interface SecurityConstants {
*/
String CLIENT_ID = "client_id";
/**
* 客户端唯一令牌
*/
String CLIENT_RECREATE = "recreate_flag";
/**
* 用户ID字段
*/
@@ -1,5 +1,7 @@
package com.pig4cloud.pigx.common.security.service;
import cn.hutool.core.map.MapUtil;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
@@ -15,6 +17,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
@@ -56,14 +59,37 @@ public class PigxCustomTokenServices implements AuthorizationServerTokenServices
OAuth2AccessToken existingAccessToken = tokenStore.getAccessToken(authentication);
OAuth2RefreshToken refreshToken = null;
// 根据clientId 查询客户端信息,判断客户端是否是每次生成新的令牌 recreate
String clientId = authentication.getOAuth2Request().getClientId();
ClientDetails clientDetails = clientDetailsService.loadClientByClientId(clientId);
Map<String, Object> extMap = clientDetails.getAdditionalInformation();
Boolean recreate = MapUtil.getBool(extMap, SecurityConstants.CLIENT_RECREATE, Boolean.TRUE);
// 若已产生token , 过期时删除相关token,执行下边的重新生成逻辑
if (existingAccessToken != null) {
if (existingAccessToken != null && recreate) {
tokenStore.removeAccessToken(existingAccessToken);
if (existingAccessToken.getRefreshToken() != null) {
refreshToken = existingAccessToken.getRefreshToken();
tokenStore.removeRefreshToken(refreshToken);
}
}
else if (existingAccessToken != null) {
if (existingAccessToken.isExpired()) {
if (existingAccessToken.getRefreshToken() != null) {
refreshToken = existingAccessToken.getRefreshToken();
// The token store could remove the refresh token when the
// access token is removed, but we want to
// be sure...
tokenStore.removeRefreshToken(refreshToken);
}
tokenStore.removeAccessToken(existingAccessToken);
}
else {
// Re-store the access token in case the authentication has changed
tokenStore.storeAccessToken(existingAccessToken, authentication);
return existingAccessToken;
}
}
if (refreshToken == null) {
refreshToken = createRefreshToken(authentication);
@@ -22,4 +22,9 @@ public class SysOauthClientDetailsDTO extends SysOauthClientDetails {
*/
private String encFlag;
/**
* 令牌是否重新生成
*/
private String recreateFlag;
}
@@ -31,6 +31,7 @@ import com.pig4cloud.pigx.admin.mapper.SysOauthClientDetailsMapper;
import com.pig4cloud.pigx.admin.service.SysOauthClientDetailsService;
import com.pig4cloud.pigx.common.core.constant.CacheConstants;
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.core.util.SpringContextHolder;
import lombok.RequiredArgsConstructor;
@@ -56,8 +57,6 @@ import java.util.stream.Collectors;
public class SysOauthClientDetailsServiceImpl extends ServiceImpl<SysOauthClientDetailsMapper, SysOauthClientDetails>
implements SysOauthClientDetailsService {
private final RedisTemplate redisTemplate;
/**
* 通过ID删除客户端
* @param clientId
@@ -90,7 +89,9 @@ public class SysOauthClientDetailsServiceImpl extends ServiceImpl<SysOauthClient
String information = clientDetailsDTO.getAdditionalInformation();
JSONObject informationObj = JSONUtil.parseObj(information)
.set(CommonConstants.CAPTCHA_FLAG, clientDetailsDTO.getCaptchaFlag())
.set(CommonConstants.ENC_FLAG, clientDetailsDTO.getEncFlag());
.set(CommonConstants.ENC_FLAG, clientDetailsDTO.getEncFlag())
.set(SecurityConstants.CLIENT_RECREATE, clientDetailsDTO.getRecreateFlag());
clientDetails.setAdditionalInformation(informationObj.toString());
// 更新数据库
@@ -115,7 +116,8 @@ public class SysOauthClientDetailsServiceImpl extends ServiceImpl<SysOauthClient
// 获取扩展信息,插入开关相关
String information = clientDetailsDTO.getAdditionalInformation();
JSONUtil.parseObj(information).set(CommonConstants.CAPTCHA_FLAG, clientDetailsDTO.getCaptchaFlag())
.set(CommonConstants.ENC_FLAG, clientDetailsDTO.getEncFlag());
.set(CommonConstants.ENC_FLAG, clientDetailsDTO.getEncFlag())
.set(SecurityConstants.CLIENT_RECREATE, clientDetailsDTO.getRecreateFlag());
// 插入数据
this.baseMapper.insert(clientDetails);
@@ -139,10 +141,13 @@ public class SysOauthClientDetailsServiceImpl extends ServiceImpl<SysOauthClient
String information = details.getAdditionalInformation();
String captchaFlag = JSONUtil.parseObj(information).getStr(CommonConstants.CAPTCHA_FLAG);
String encFlag = JSONUtil.parseObj(information).getStr(CommonConstants.ENC_FLAG);
String recreateFlag = JSONUtil.parseObj(information).getStr(SecurityConstants.CLIENT_RECREATE);
SysOauthClientDetailsDTO dto = new SysOauthClientDetailsDTO();
BeanUtils.copyProperties(details, dto);
dto.setCaptchaFlag(captchaFlag);
dto.setEncFlag(encFlag);
dto.setRecreateFlag(recreateFlag);
return dto;
}).collect(Collectors.toList());