提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.security.core.service; |
H |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
|
4 |
import cn.hutool.core.collection.ListUtil; |
|
5 |
import cn.hutool.core.util.ArrayUtil; |
|
6 |
import cn.hutool.core.util.HashUtil; |
|
7 |
import cn.hutool.core.util.ObjectUtil; |
|
8 |
import com.iailab.framework.common.core.KeyValue; |
|
9 |
import com.iailab.framework.common.pojo.CommonResult; |
|
10 |
import com.iailab.framework.common.util.cache.CacheUtils; |
|
11 |
import com.iailab.framework.security.core.LoginUser; |
|
12 |
import com.iailab.framework.security.core.util.SecurityFrameworkUtils; |
|
13 |
import com.iailab.module.system.api.permission.PermissionApi; |
|
14 |
import com.google.common.cache.CacheLoader; |
|
15 |
import com.google.common.cache.LoadingCache; |
|
16 |
import lombok.AllArgsConstructor; |
|
17 |
import lombok.SneakyThrows; |
|
18 |
|
|
19 |
import java.time.Duration; |
|
20 |
import java.util.Arrays; |
|
21 |
import java.util.List; |
|
22 |
|
|
23 |
import static com.iailab.framework.common.util.cache.CacheUtils.buildAsyncReloadingCache; |
|
24 |
import static com.iailab.framework.common.util.cache.CacheUtils.buildCache; |
|
25 |
import static com.iailab.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
|
26 |
|
|
27 |
/** |
|
28 |
* 默认的 {@link SecurityFrameworkService} 实现类 |
|
29 |
* |
|
30 |
* @author iailab |
|
31 |
*/ |
|
32 |
@AllArgsConstructor |
|
33 |
public class SecurityFrameworkServiceImpl implements SecurityFrameworkService { |
|
34 |
|
|
35 |
private final PermissionApi permissionApi; |
|
36 |
|
|
37 |
/** |
|
38 |
* 针对 {@link #hasAnyRoles(String...)} 的缓存 |
|
39 |
*/ |
|
40 |
private final LoadingCache<KeyValue<Long, List<String>>, Boolean> hasAnyRolesCache = buildCache( |
|
41 |
Duration.ofMinutes(1L), // 过期时间 1 分钟 |
|
42 |
new CacheLoader<KeyValue<Long, List<String>>, Boolean>() { |
|
43 |
|
|
44 |
@Override |
|
45 |
public Boolean load(KeyValue<Long, List<String>> key) { |
|
46 |
return permissionApi.hasAnyRoles(key.getKey(), key.getValue().toArray(new String[0])).getCheckedData(); |
|
47 |
} |
|
48 |
|
|
49 |
}); |
|
50 |
|
|
51 |
/** |
|
52 |
* 针对 {@link #hasAnyPermissions(String...)} 的缓存 |
|
53 |
*/ |
|
54 |
private final LoadingCache<KeyValue<Long, List<String>>, Boolean> hasAnyPermissionsCache = buildCache( |
|
55 |
Duration.ofMinutes(1L), // 过期时间 1 分钟 |
|
56 |
new CacheLoader<KeyValue<Long, List<String>>, Boolean>() { |
|
57 |
|
|
58 |
@Override |
|
59 |
public Boolean load(KeyValue<Long, List<String>> key) { |
|
60 |
return permissionApi.hasAnyPermissions(key.getKey(), key.getValue().toArray(new String[0])).getCheckedData(); |
|
61 |
} |
|
62 |
|
|
63 |
}); |
|
64 |
|
|
65 |
@Override |
|
66 |
public boolean hasPermission(String permission) { |
|
67 |
return hasAnyPermissions(permission); |
|
68 |
} |
|
69 |
|
|
70 |
@Override |
|
71 |
@SneakyThrows |
|
72 |
public boolean hasAnyPermissions(String... permissions) { |
|
73 |
return hasAnyPermissionsCache.get(new KeyValue<>(getLoginUserId(), Arrays.asList(permissions))); |
|
74 |
} |
|
75 |
|
|
76 |
@Override |
|
77 |
public boolean hasRole(String role) { |
|
78 |
return hasAnyRoles(role); |
|
79 |
} |
|
80 |
|
|
81 |
@Override |
|
82 |
@SneakyThrows |
|
83 |
public boolean hasAnyRoles(String... roles) { |
|
84 |
return hasAnyRolesCache.get(new KeyValue<>(getLoginUserId(), Arrays.asList(roles))); |
|
85 |
} |
|
86 |
|
|
87 |
@Override |
|
88 |
public boolean hasScope(String scope) { |
|
89 |
return hasAnyScopes(scope); |
|
90 |
} |
|
91 |
|
|
92 |
@Override |
|
93 |
public boolean hasAnyScopes(String... scope) { |
|
94 |
LoginUser user = SecurityFrameworkUtils.getLoginUser(); |
|
95 |
if (user == null) { |
|
96 |
return false; |
|
97 |
} |
|
98 |
return CollUtil.containsAny(user.getScopes(), Arrays.asList(scope)); |
|
99 |
} |
|
100 |
|
|
101 |
} |