优化配置参数的名字和日志输出级别

This commit is contained in:
天天向上 2022-11-29 17:16:14 +08:00
parent 47993ef573
commit e36b3642da
3 changed files with 14 additions and 11 deletions

View File

@ -21,7 +21,7 @@ import java.lang.reflect.Method;
*/ */
@Aspect @Aspect
@Component @Component
@ConditionalOnProperty(prefix = "security.aspect", name = "enabled", havingValue = "true", matchIfMissing = true) @ConditionalOnProperty(prefix = "security.annotation", name = "enabled", havingValue = "true", matchIfMissing = true)
public class PreAuthorizeAspect public class PreAuthorizeAspect
{ {
/** /**

View File

@ -19,18 +19,21 @@ import java.util.*;
/** /**
* =====================================网关鉴权使用说明======================================= * =====================================网关鉴权使用说明=======================================
* 场景 * 场景
* 微服务部署在内网确定安全无需在每个微服务都实现一次权限控制的逻辑可以在网关层面实现统一鉴权 * 微服务部署在内网确定安全无需在每个微服务都实现鉴权的逻辑可以在网关层面实现统一鉴权
* 使用方式 * 使用方式
* 1在每个微服务的配置文件中添加参数security.aspect.enabled: false 关闭系统默认的通过注解方式鉴权默认开启 * 1在每个微服务的配置文件中添加参数security.annotation.enabled: false 关闭系统默认的通过注解方式鉴权默认开启
* 2在每个微服务的配置文件中添加参数routePrefix: 值为网关中微服务匹配的路由地址例如: /auth * 2在每个微服务的配置文件中添加参数pathPrefix: 值为网关中微服务匹配的路由地址前缀例如: /auth
* 3在网关配置文件中添加参数security.gateway.enabled: true 启用网关统一鉴权默认关闭 * 3在网关配置文件中添加参数security.gateway.enabled: true 启用网关统一鉴权默认关闭
* *
* 通过反射扫描所有控制器缓存所有控制器的映射路径以及对应的权限注解缓存到redis方便网关鉴权 * 通过反射扫描所有控制器缓存所有控制器的映射路径以及对应的权限注解缓存到redis方便网关鉴权
*/ */
@ConditionalOnProperty(prefix = "security.gateway", name = "enabled", havingValue = "true") @ConditionalOnProperty(prefix = "security.annotation", name = "enabled", havingValue = "false")
public class PathPermissionMappingConfig { public class PathPermissionMappingConfig {
@Value("${routePrefix}") /**
private String routePrefix; * 微服务在网关配置中predicates中的Path前缀例如 /system
*/
@Value("${pathPrefix}")
private String pathPrefix;
@PostConstruct @PostConstruct
public PathPermissionMappingConfig execute() { public PathPermissionMappingConfig execute() {
@ -85,7 +88,7 @@ public class PathPermissionMappingConfig {
private void addPathPermsMap(String perms, Map<String, String> pathPermsMap, Set<RequestMethod> methods, Set<String> patternValues) { private void addPathPermsMap(String perms, Map<String, String> pathPermsMap, Set<RequestMethod> methods, Set<String> patternValues) {
for (RequestMethod method : methods) { for (RequestMethod method : methods) {
for (String patternValue : patternValues) { for (String patternValue : patternValues) {
String key = routePrefix + patternValue + "_" + method.name(); String key = pathPrefix + patternValue + "_" + method.name();
pathPermsMap.put(key, perms); pathPermsMap.put(key, perms);
} }
} }

View File

@ -133,21 +133,21 @@ public class AuthFilter implements GlobalFilter, Ordered
if(!rolePerms.isEmpty()) { if(!rolePerms.isEmpty()) {
if(rolePerms.contains(SecurityConstants.ROLE_ANON)) { if(rolePerms.contains(SecurityConstants.ROLE_ANON)) {
log.info("允许访问公共权限:{}{}", api, rolePerms); log.debug("允许访问公共权限:{}{}", api, rolePerms);
return true; return true;
} }
rolePerms = rolePerms.stream().map(item -> item.substring(SecurityConstants.ROLE_PREFIX.length())).collect(Collectors.toSet()); rolePerms = rolePerms.stream().map(item -> item.substring(SecurityConstants.ROLE_PREFIX.length())).collect(Collectors.toSet());
// 求交集 // 求交集
rolePerms.retainAll(roles); rolePerms.retainAll(roles);
if(!rolePerms.isEmpty()) { if(!rolePerms.isEmpty()) {
log.info("允许访问角色权限:{} {}", api, rolePerms); log.debug("允许访问角色权限:{} {}", api, rolePerms);
return true; return true;
} }
} }
// 求交集 // 求交集
matchedPerms.retainAll(permissions); matchedPerms.retainAll(permissions);
if(!matchedPerms.isEmpty()) { if(!matchedPerms.isEmpty()) {
log.info("允许访问资源权限:{}{}", api, matchedPerms); log.debug("允许访问资源权限:{}{}", api, matchedPerms);
return true; return true;
} }
} }