♻️ 重构代码:提供更多的swagger参数配置

This commit is contained in:
lishangbu committed 2018-07-27 14:49:44 +08:00
1 parent 8ce8ba60b7
commit b9275daf1c
4 files changed
+99 -33

No files matched your search

@@ -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<SecurityReference> 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<AuthorizationScope> 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<AuthorizationScope> authorizationScopeList = new ArrayList<>();
swaggerProperties().getAuthorization().getAuthorizationScopeList().forEach(authorizationScope->authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(),authorizationScope.getDescription())));
ArrayList<GrantType> 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) {
@@ -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<Class<?>> 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<AuthorizationScope> authorizationScopeList = new ArrayList<>();
private List<String> tokenUrlList =new ArrayList<>();
}
@Data
@NoArgsConstructor
public static class AuthorizationScope {
/**
* 作用域名称
*/
private String scope = "";
/**
* 作用域描述
*/
private String description = "";
}
}
@@ -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'
@@ -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