增加 SecurityFrameworkServiceImpl 的缓存,提升性能
This commit is contained in:
parent
5c04c9eeaf
commit
0045602e6b
@ -1,5 +1,6 @@
|
|||||||
package cn.iocoder.yudao.framework.common.core;
|
package cn.iocoder.yudao.framework.common.core;
|
||||||
|
|
||||||
|
import com.google.common.base.Objects;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
@ -57,6 +57,12 @@
|
|||||||
<artifactId>yudao-module-system-api</artifactId> <!-- 需要使用它,进行 Token 的校验 -->
|
<artifactId>yudao-module-system-api</artifactId> <!-- 需要使用它,进行 Token 的校验 -->
|
||||||
<version>${revision}</version>
|
<version>${revision}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 工具类相关 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.guava</groupId>
|
||||||
|
<artifactId>guava</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -1,13 +1,24 @@
|
|||||||
package cn.iocoder.yudao.framework.security.core.service;
|
package cn.iocoder.yudao.framework.security.core.service;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.core.collection.ListUtil;
|
||||||
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
|
import cn.hutool.core.util.HashUtil;
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import cn.iocoder.yudao.framework.common.core.KeyValue;
|
||||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||||
|
import cn.iocoder.yudao.framework.common.util.cache.CacheUtils;
|
||||||
import cn.iocoder.yudao.framework.security.core.LoginUser;
|
import cn.iocoder.yudao.framework.security.core.LoginUser;
|
||||||
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
|
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
|
||||||
import cn.iocoder.yudao.module.system.api.permission.PermissionApi;
|
import cn.iocoder.yudao.module.system.api.permission.PermissionApi;
|
||||||
|
import com.google.common.cache.CacheLoader;
|
||||||
|
import com.google.common.cache.LoadingCache;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
|
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
|
||||||
|
|
||||||
@ -21,16 +32,45 @@ public class SecurityFrameworkServiceImpl implements SecurityFrameworkService {
|
|||||||
|
|
||||||
private final PermissionApi permissionApi;
|
private final PermissionApi permissionApi;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 针对 {@link #hasAnyRoles(String...)} 的缓存
|
||||||
|
*/
|
||||||
|
private final LoadingCache<KeyValue<Long, List<String>>, Boolean> hasAnyRolesCache = CacheUtils.buildAsyncReloadingCache(
|
||||||
|
Duration.ofMinutes(1L), // 过期时间 1 分钟
|
||||||
|
new CacheLoader<KeyValue<Long, List<String>>, Boolean>() {
|
||||||
|
@Override
|
||||||
|
public Boolean load(KeyValue<Long, List<String>> key) {
|
||||||
|
CommonResult<Boolean> hasAnyRolesResult = permissionApi.hasAnyRoles(key.getKey(),
|
||||||
|
key.getValue().toArray(new String[0]));
|
||||||
|
hasAnyRolesResult.checkError();
|
||||||
|
return hasAnyRolesResult.getData();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 针对 {@link #hasAnyPermissions(String...)} 的缓存
|
||||||
|
*/
|
||||||
|
private final LoadingCache<KeyValue<Long, List<String>>, Boolean> hasAnyPermissionsCache = CacheUtils.buildAsyncReloadingCache(
|
||||||
|
Duration.ofMinutes(1L), // 过期时间 1 分钟
|
||||||
|
new CacheLoader<KeyValue<Long, List<String>>, Boolean>() {
|
||||||
|
@Override
|
||||||
|
public Boolean load(KeyValue<Long, List<String>> key) {
|
||||||
|
CommonResult<Boolean> hasAnyPermissionsResult = permissionApi.hasAnyPermissions(key.getKey(),
|
||||||
|
key.getValue().toArray(new String[0]));
|
||||||
|
hasAnyPermissionsResult.checkError();
|
||||||
|
return hasAnyPermissionsResult.getData();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasPermission(String permission) {
|
public boolean hasPermission(String permission) {
|
||||||
return hasAnyPermissions(permission);
|
return hasAnyPermissions(permission);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@SneakyThrows
|
||||||
public boolean hasAnyPermissions(String... permissions) {
|
public boolean hasAnyPermissions(String... permissions) {
|
||||||
CommonResult<Boolean> hasAnyPermissionsResult = permissionApi.hasAnyPermissions(getLoginUserId(), permissions);
|
return hasAnyPermissionsCache.get(new KeyValue<>(getLoginUserId(), Arrays.asList(permissions)));
|
||||||
hasAnyPermissionsResult.checkError();
|
|
||||||
return hasAnyPermissionsResult.getData();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -39,10 +79,9 @@ public class SecurityFrameworkServiceImpl implements SecurityFrameworkService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@SneakyThrows
|
||||||
public boolean hasAnyRoles(String... roles) {
|
public boolean hasAnyRoles(String... roles) {
|
||||||
CommonResult<Boolean> hasAnyRolesResult = permissionApi.hasAnyRoles(getLoginUserId(), roles);
|
return hasAnyRolesCache.get(new KeyValue<>(getLoginUserId(), Arrays.asList(roles)));
|
||||||
hasAnyRolesResult.checkError();
|
|
||||||
return hasAnyRolesResult.getData();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -31,7 +31,7 @@ tenant-id: {{adminTenentId}}
|
|||||||
roleId=14
|
roleId=14
|
||||||
|
|
||||||
### /role/get 成功
|
### /role/get 成功
|
||||||
GET {{baseUrl}}/system/role/get?id=100
|
GET {{systemBaseUrl}}/system/role/get?id=100
|
||||||
Content-Type: application/x-www-form-urlencoded
|
Content-Type: application/x-www-form-urlencoded
|
||||||
Authorization: Bearer {{token}}
|
Authorization: Bearer {{token}}
|
||||||
tenant-id: {{adminTenentId}}
|
tenant-id: {{adminTenentId}}
|
||||||
|
Loading…
Reference in New Issue
Block a user