11package 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 ;
74import org .springframework .security .access .ConfigAttribute ;
85import org .springframework .security .access .vote .RoleVoter ;
96import org .springframework .security .core .Authentication ;
107import org .springframework .security .web .FilterInvocation ;
118import org .springframework .stereotype .Component ;
129import 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 ;
1511import top .cadecode .framework .model .vo .SecurityApiVo ;
1612
1713import java .util .Collection ;
18- import java .util .Collections ;
1914import java .util .List ;
20- import java .util .concurrent .ExecutionException ;
21- import java .util .concurrent .TimeUnit ;
2215import java .util .stream .Collectors ;
2316
2417/**
2518 * @author Cade Li
2619 * @date 2021/12/15
2720 * @description 数据库加载权限 api 的投票器
2821 */
22+ @ RequiredArgsConstructor
2923@ Component
3024public 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_" , "" ))
0 commit comments