Skip to content

Commit 01bba6f

Browse files
committed
feat: 基于 springboot cache 组件修改 api 和 role 的缓存策略
1 parent fdb6fb3 commit 01bba6f

5 files changed

Lines changed: 56 additions & 40 deletions

File tree

simple-framework/src/main/java/top/cadecode/framework/model/mapper/SecurityApiMapper.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ public interface SecurityApiMapper {
1616
/**
1717
* 查询所有接口及其角色
1818
*
19-
* @return url
19+
* @return api 列表
2020
*/
21-
List<SecurityApiVo> listSecurityApiVo();
22-
21+
List<SecurityApiVo> listSecurityApiVos();
2322
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package top.cadecode.framework.model.service;
2+
3+
import top.cadecode.framework.model.vo.SecurityApiVo;
4+
5+
import java.util.List;
6+
7+
/**
8+
* @author Cade Li
9+
* @date 2022/1/5
10+
* @description spring security api service 接口
11+
*/
12+
public interface SecurityApiService {
13+
14+
/**
15+
* 查询所有接口及其角色
16+
*
17+
* @return api 列表
18+
*/
19+
List<SecurityApiVo> listSecurityApiVos();
20+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package top.cadecode.framework.model.service.impl;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.springframework.cache.annotation.Cacheable;
5+
import org.springframework.stereotype.Service;
6+
import top.cadecode.framework.model.mapper.SecurityApiMapper;
7+
import top.cadecode.framework.model.service.SecurityApiService;
8+
import top.cadecode.framework.model.vo.SecurityApiVo;
9+
10+
import java.util.List;
11+
12+
/**
13+
* @author Cade Li
14+
* @date 2022/1/5
15+
* @description spring security api service 实现
16+
*/
17+
@RequiredArgsConstructor
18+
@Service
19+
public class SecurityApiServiceImpl implements SecurityApiService {
20+
21+
private final SecurityApiMapper securityApiMapper;
22+
23+
@Cacheable(cacheNames = "apiRoleCache", cacheManager = "securityCacheManager")
24+
@Override
25+
public List<SecurityApiVo> listSecurityApiVos() {
26+
return securityApiMapper.listSecurityApiVos();
27+
}
28+
}

simple-framework/src/main/java/top/cadecode/framework/security/voter/DbRoleVoter.java

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,32 @@
11
package top.cadecode.framework.security.voter;
22

3-
import com.google.common.cache.CacheBuilder;
4-
import com.google.common.cache.CacheLoader;
5-
import com.google.common.cache.LoadingCache;
6-
import org.springframework.beans.factory.annotation.Autowired;
3+
import lombok.RequiredArgsConstructor;
74
import org.springframework.security.access.ConfigAttribute;
85
import org.springframework.security.access.vote.RoleVoter;
96
import org.springframework.security.core.Authentication;
107
import org.springframework.security.web.FilterInvocation;
118
import org.springframework.stereotype.Component;
129
import org.springframework.util.AntPathMatcher;
13-
import top.cadecode.common.util.TokenUtil;
14-
import top.cadecode.framework.model.mapper.SecurityApiMapper;
10+
import top.cadecode.framework.model.service.SecurityApiService;
1511
import top.cadecode.framework.model.vo.SecurityApiVo;
1612

1713
import java.util.Collection;
18-
import java.util.Collections;
1914
import java.util.List;
20-
import java.util.concurrent.ExecutionException;
21-
import java.util.concurrent.TimeUnit;
2215
import java.util.stream.Collectors;
2316

2417
/**
2518
* @author Cade Li
2619
* @date 2021/12/15
2720
* @description 数据库加载权限 api 的投票器
2821
*/
22+
@RequiredArgsConstructor
2923
@Component
3024
public class DbRoleVoter extends RoleVoter {
3125

3226
// ant 匹配器
3327
private final AntPathMatcher antPathMatcher = new AntPathMatcher();
34-
// api role 关系缓存
35-
private final LoadingCache<String, List<SecurityApiVo>> apiRoleCache;
3628

37-
@Autowired
38-
public DbRoleVoter(TokenUtil tokenUtil, SecurityApiMapper securityApiMapper) {
39-
this.apiRoleCache = CacheBuilder.newBuilder()
40-
.refreshAfterWrite(tokenUtil.getExpiration(), TimeUnit.SECONDS)
41-
.build(new CacheLoader<String, List<SecurityApiVo>>() {
42-
@Override
43-
public List<SecurityApiVo> load(String key) {
44-
return securityApiMapper.listSecurityApiVo();
45-
}
46-
});
47-
}
48-
49-
/**
50-
* 获取 api role 的关系缓存
51-
*
52-
* @return api role 关系列表
53-
*/
54-
public List<SecurityApiVo> getDbRoleCache() {
55-
try {
56-
return apiRoleCache.get("");
57-
} catch (ExecutionException e) {
58-
return Collections.emptyList();
59-
}
60-
}
29+
private final SecurityApiService securityApiService;
6130

6231
@Override
6332
public int vote(Authentication authentication, Object object, Collection<ConfigAttribute> attributes) {
@@ -68,7 +37,7 @@ public int vote(Authentication authentication, Object object, Collection<ConfigA
6837
FilterInvocation fi = (FilterInvocation) object;
6938
String requestUrl = fi.getRequestUrl();
7039
// 获取 api role 的关系列表
71-
List<SecurityApiVo> securityApiVos = this.getDbRoleCache();
40+
List<SecurityApiVo> securityApiVos = securityApiService.listSecurityApiVos();
7241
// 获取用户角色
7342
List<String> roles = authentication.getAuthorities().stream()
7443
.map(authority -> authority.getAuthority().replace("ROLE_", ""))

simple-framework/src/main/resources/mapper/SecurityApiMapper.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<result column="code" />
1010
</collection>
1111
</resultMap>
12-
<select id="listSecurityApiVo" resultMap="SecurityApiVoMap">
12+
<select id="listSecurityApiVos" resultMap="SecurityApiVoMap">
1313
SELECT sa.id, sa.url, sa.description, sr.code
1414
FROM security_api sa
1515
LEFT JOIN security_role_api sra ON sra.api_id = sa.id

0 commit comments

Comments
 (0)