|
|
|
@ -24,11 +24,12 @@ import java.util.*;
|
|
|
|
|
* 1、在每个微服务的配置文件中添加参数:security.annotation.enabled: false 关闭系统默认的通过注解方式鉴权,默认开启
|
|
|
|
|
* 2、在每个微服务的配置文件中添加参数:pathPrefix: 值为网关中微服务匹配的路由地址前缀,例如: /auth
|
|
|
|
|
* 3、在网关配置文件中添加参数:security.gateway.enabled: true 启用网关统一鉴权,默认关闭
|
|
|
|
|
*
|
|
|
|
|
* <p>
|
|
|
|
|
* 通过反射扫描所有控制器,缓存所有控制器的映射路径以及对应的权限注解,缓存到redis,方便网关鉴权
|
|
|
|
|
*/
|
|
|
|
|
@ConditionalOnProperty(prefix = "security.annotation", name = "enabled", havingValue = "false")
|
|
|
|
|
public class PathPermissionMappingConfig {
|
|
|
|
|
public class PathPermissionMappingConfig
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 微服务在网关配置中predicates中的Path前缀,例如: /system
|
|
|
|
|
*/
|
|
|
|
@ -36,7 +37,8 @@ public class PathPermissionMappingConfig {
|
|
|
|
|
private String pathPrefix;
|
|
|
|
|
|
|
|
|
|
@PostConstruct
|
|
|
|
|
public PathPermissionMappingConfig execute() {
|
|
|
|
|
public PathPermissionMappingConfig execute()
|
|
|
|
|
{
|
|
|
|
|
RedisService redisService = SpringUtils.getBean(RedisService.class);
|
|
|
|
|
RequestMappingHandlerMapping bean = SpringUtils.getBean("requestMappingHandlerMapping");
|
|
|
|
|
Map<RequestMappingInfo, HandlerMethod> handlerMethods = bean.getHandlerMethods();
|
|
|
|
@ -45,7 +47,8 @@ public class PathPermissionMappingConfig {
|
|
|
|
|
*/
|
|
|
|
|
Map<String, String> pathPermsMap = new TreeMap<>();
|
|
|
|
|
|
|
|
|
|
handlerMethods.forEach((k, v) -> {
|
|
|
|
|
handlerMethods.forEach((k, v) ->
|
|
|
|
|
{
|
|
|
|
|
RequiresRoles requiresRoles = v.getMethodAnnotation(RequiresRoles.class);
|
|
|
|
|
RequiresPermissions requiresPermissions = v.getMethodAnnotation(RequiresPermissions.class);
|
|
|
|
|
|
|
|
|
@ -54,21 +57,27 @@ public class PathPermissionMappingConfig {
|
|
|
|
|
/**
|
|
|
|
|
* @RequestMapping注解
|
|
|
|
|
*/
|
|
|
|
|
if(methods.isEmpty()) {
|
|
|
|
|
if (methods.isEmpty())
|
|
|
|
|
{
|
|
|
|
|
methods = new HashSet<>();
|
|
|
|
|
methods.addAll(Arrays.asList(RequestMethod.GET, RequestMethod.POST));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(requiresPermissions == null && requiresRoles == null) {
|
|
|
|
|
if (requiresPermissions == null && requiresRoles == null)
|
|
|
|
|
{
|
|
|
|
|
addPathPermsMap(SecurityConstants.ROLE_ANON, pathPermsMap, methods, patternValues);
|
|
|
|
|
}
|
|
|
|
|
if(requiresPermissions != null) {
|
|
|
|
|
for (String perms : requiresPermissions.value()) {
|
|
|
|
|
if (requiresPermissions != null)
|
|
|
|
|
{
|
|
|
|
|
for (String perms : requiresPermissions.value())
|
|
|
|
|
{
|
|
|
|
|
addPathPermsMap(perms, pathPermsMap, methods, patternValues);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(requiresRoles != null) {
|
|
|
|
|
for (String role : requiresRoles.value()) {
|
|
|
|
|
if (requiresRoles != null)
|
|
|
|
|
{
|
|
|
|
|
for (String role : requiresRoles.value())
|
|
|
|
|
{
|
|
|
|
|
addPathPermsMap(SecurityConstants.ROLE_PREFIX + role, pathPermsMap, methods, patternValues);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -80,14 +89,18 @@ public class PathPermissionMappingConfig {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 一个path对应多个perms
|
|
|
|
|
*
|
|
|
|
|
* @param perms
|
|
|
|
|
* @param pathPermsMap
|
|
|
|
|
* @param methods
|
|
|
|
|
* @param patternValues
|
|
|
|
|
*/
|
|
|
|
|
private void addPathPermsMap(String perms, Map<String, String> pathPermsMap, Set<RequestMethod> methods, Set<String> patternValues) {
|
|
|
|
|
for (RequestMethod method : methods) {
|
|
|
|
|
for (String patternValue : patternValues) {
|
|
|
|
|
private void addPathPermsMap(String perms, Map<String, String> pathPermsMap, Set<RequestMethod> methods, Set<String> patternValues)
|
|
|
|
|
{
|
|
|
|
|
for (RequestMethod method : methods)
|
|
|
|
|
{
|
|
|
|
|
for (String patternValue : patternValues)
|
|
|
|
|
{
|
|
|
|
|
String key = pathPrefix + patternValue + "_" + method.name();
|
|
|
|
|
pathPermsMap.put(key, perms);
|
|
|
|
|
}
|
|
|
|
|