diff --git a/pigx-common/pigx-common-swagger/src/main/java/com/pig4cloud/pigx/common/swagger/config/SwaggerAutoConfiguration.java b/pigx-common/pigx-common-swagger/src/main/java/com/pig4cloud/pigx/common/swagger/config/SwaggerAutoConfiguration.java index 39eb33cf8..60cd4ea48 100644 --- a/pigx-common/pigx-common-swagger/src/main/java/com/pig4cloud/pigx/common/swagger/config/SwaggerAutoConfiguration.java +++ b/pigx-common/pigx-common-swagger/src/main/java/com/pig4cloud/pigx/common/swagger/config/SwaggerAutoConfiguration.java @@ -85,34 +85,39 @@ public class SwaggerAutoConfiguration { .pathMapping("/"); } + /** + * 配置默认的全局鉴权策略的开关,通过正则表达式进行匹配;默认匹配所有URL + * @return + */ private SecurityContext securityContext() { - return SecurityContext.builder().securityReferences(defaultAuth()) - .forPaths(PathSelectors.ant(BASE_PATH)) + return SecurityContext.builder() + .securityReferences(defaultAuth()) + .forPaths(PathSelectors.regex(swaggerProperties().getAuthorization().getAuthRegex())) .build(); } + /** + * 默认的全局鉴权策略 + * + * @return + */ private List defaultAuth() { - - final AuthorizationScope[] authorizationScopes = new AuthorizationScope[3]; - authorizationScopes[0] = new AuthorizationScope("read", "read all"); - authorizationScopes[1] = new AuthorizationScope("trust", "trust all"); - authorizationScopes[2] = new AuthorizationScope("write", "write all"); - - return Collections.singletonList(new SecurityReference("pigX OAuth", authorizationScopes)); + ArrayList authorizationScopeList = new ArrayList<>(); + swaggerProperties().getAuthorization().getAuthorizationScopeList().forEach(authorizationScope->authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(),authorizationScope.getDescription()))); + AuthorizationScope[] authorizationScopes = new AuthorizationScope[authorizationScopeList.size()]; + return Collections.singletonList(SecurityReference.builder() + .reference(swaggerProperties().getAuthorization().getName()) + .scopes(authorizationScopeList.toArray(authorizationScopes)) + .build()); } private OAuth securitySchema() { - ArrayList authorizationScopeList = new ArrayList(); - authorizationScopeList.add(new AuthorizationScope("server", "server all")); - authorizationScopeList.add(new AuthorizationScope("read", "read all")); - authorizationScopeList.add(new AuthorizationScope("write", "access all")); - - ArrayList grantTypes = new ArrayList(); - GrantType creGrant = new ResourceOwnerPasswordCredentialsGrant("http://localhost:9999/auth/oauth/token"); - - grantTypes.add(creGrant); - return new OAuth("pigX OAuth", authorizationScopeList, grantTypes); + ArrayList authorizationScopeList = new ArrayList<>(); + swaggerProperties().getAuthorization().getAuthorizationScopeList().forEach(authorizationScope->authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(),authorizationScope.getDescription()))); + ArrayList grantTypes = new ArrayList<>(); + swaggerProperties().getAuthorization().getTokenUrlList().forEach(tokenUrl->grantTypes.add(new ResourceOwnerPasswordCredentialsGrant(tokenUrl))); + return new OAuth(swaggerProperties().getAuthorization().getName(), authorizationScopeList, grantTypes); } private ApiInfo apiInfo(SwaggerProperties swaggerProperties) { diff --git a/pigx-common/pigx-common-swagger/src/main/java/com/pig4cloud/pigx/common/swagger/config/SwaggerProperties.java b/pigx-common/pigx-common-swagger/src/main/java/com/pig4cloud/pigx/common/swagger/config/SwaggerProperties.java index 56e0edf9a..2a0a738d3 100644 --- a/pigx-common/pigx-common-swagger/src/main/java/com/pig4cloud/pigx/common/swagger/config/SwaggerProperties.java +++ b/pigx-common/pigx-common-swagger/src/main/java/com/pig4cloud/pigx/common/swagger/config/SwaggerProperties.java @@ -19,7 +19,6 @@ package com.pig4cloud.pigx.common.swagger.config; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.util.StringUtils; import java.util.ArrayList; import java.util.List; @@ -48,37 +47,41 @@ public class SwaggerProperties { /** * 标题 **/ - private String title = "PigX Swagger API"; + private String title = ""; /** * 描述 **/ - private String description = "https://gitee.com/log4j/pig/wikis"; + private String description = ""; /** * 版本 **/ - private String version = "2.0"; + private String version = ""; /** * 许可证 **/ - private String license = "Powered By PigX"; + private String license = ""; /** * 许可证URL **/ - private String licenseUrl = "https://gitee.com/log4j/pig/wikis"; + private String licenseUrl = ""; /** * 服务条款URL **/ - private String termsOfServiceUrl = "https://gitee.wang/pig/pigx"; + private String termsOfServiceUrl = ""; - /** - * 忽略的参数类型 - **/ - private List> ignoredParameterTypes = new ArrayList<>(); /** * host信息 **/ private String host = ""; + /** + * 联系人信息 + */ private Contact contact = new Contact(); + /** + * 全局统一鉴权配置 + **/ + private Authorization authorization = new Authorization(); + @Data @NoArgsConstructor public static class Contact { @@ -86,15 +89,53 @@ public class SwaggerProperties { /** * 联系人 **/ - private String name = "冷冷"; + private String name = ""; /** * 联系人url **/ - private String url = "https://gitee.wang/pig/pigx"; + private String url = ""; /** * 联系人email **/ - private String email = "wangiegie@gmail.com"; + private String email = ""; + + } + + @Data + @NoArgsConstructor + public static class Authorization { + + /** + * 鉴权策略ID,需要和SecurityReferences ID保持一致 + */ + private String name = ""; + + /** + * 需要开启鉴权URL的正则 + */ + private String authRegex = "^.*$"; + + /** + * 鉴权作用域列表 + */ + private List authorizationScopeList = new ArrayList<>(); + + private List tokenUrlList =new ArrayList<>(); + } + + @Data + @NoArgsConstructor + public static class AuthorizationScope { + + /** + * 作用域名称 + */ + private String scope = ""; + + /** + * 作用域描述 + */ + private String description = ""; } } diff --git a/pigx-config/src/main/resources/config/application-dev.yml b/pigx-config/src/main/resources/config/application-dev.yml index 08c1ca9e1..364e7494c 100644 --- a/pigx-config/src/main/resources/config/application-dev.yml +++ b/pigx-config/src/main/resources/config/application-dev.yml @@ -47,9 +47,21 @@ ribbon: #swagger公共信息 swagger: + title: 'PigX Swagger API' description: '全宇宙最牛逼的Spring Cloud微服务开发脚手架' version: '1.3.0' + license: 'Powered By PigX' + licenseUrl: 'https://gitee.com/log4j/pig/wikis' + terms-of-service-url: 'https://gitee.wang/pig/pigx' contact: name: '冷冷' email: 'wangiegie@gmail.com' url: 'https://gitee.wang/pig/pigx' + authorization: + name: 'pigX OAuth' + auth-regex: '^.*$' + authorization-scope-list: + - scope: 'server' + description: 'server all' + token-url-list: + - 'http://localhost:9999/auth/oauth/token' diff --git a/pigx-config/src/main/resources/config/pigx-upms-dev.yml b/pigx-config/src/main/resources/config/pigx-upms-dev.yml index 06fa8b4ab..98df2c4bc 100644 --- a/pigx-config/src/main/resources/config/pigx-upms-dev.yml +++ b/pigx-config/src/main/resources/config/pigx-upms-dev.yml @@ -40,6 +40,14 @@ mybatis-plus: cache-enabled: true # swagger相关配置 swagger: + authorization: + authorization-scope-list: + - scope: 'server' + description: 'server all' + - scope: 'read' + description: 'read all' + - scope: 'write' + description: 'access all' exclude-path: - /actuator/** - /error