refactor(sa-token): 优化 sa-token 细节

This commit is contained in:
冷冷 2024-12-06 22:22:30 +08:00
parent dc73679935
commit 62461e0717
3 changed files with 11 additions and 8 deletions

View File

@ -5,9 +5,11 @@ import cn.dev33.satoken.oauth2.data.model.loader.SaClientModel;
import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pig.admin.api.entity.SysOauthClientDetails;
import com.pig4cloud.pig.admin.api.feign.RemoteClientDetailsService;
import com.pig4cloud.pig.common.core.constant.CacheConstants;
import com.pig4cloud.pig.common.core.util.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.Arrays;
@ -33,6 +35,7 @@ public class SaOAuth2ClientDataLoaderImpl implements SaOAuth2DataLoader {
* @return ClientModel
*/
@Override
@Cacheable(value = CacheConstants.CLIENT_DETAILS_KEY, key = "#clientId", unless = "#result == null")
public SaClientModel getClientModel(String clientId) {
R<SysOauthClientDetails> clientDetailsById = remoteClientDetailsService.getClientDetailsById(clientId);

View File

@ -176,6 +176,14 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- sa-token version-->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-bom</artifactId>
<version>${sa-token.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--web 模块-->
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -44,10 +44,6 @@ public class SecurityUtils {
*/
public PigUser getUser() {
Object loginId = StpUtil.getLoginId();
if (ObjectUtil.isNull(loginId)) {
return null;
}
String username = loginId.toString();
Cache cache = SpringUtil.getBean(CacheManager.class).getCache(CacheConstants.USER_DETAILS);
PigUser pigUser = getCachedPigUser(cache, username);
@ -65,10 +61,6 @@ public class SecurityUtils {
*/
public List<Long> getRoles() {
Object loginId = StpUtil.getLoginId();
if (ObjectUtil.isNull(loginId)) {
return null;
}
String username = loginId.toString();
Cache cache = SpringUtil.getBean(CacheManager.class).getCache(CacheConstants.USER_DETAILS);
List<Long> roles = getCachedRoles(cache, username);