|
@@ -5,40 +5,137 @@ package com.mooctest.crowd.site.configuration;
|
|
|
* @date 2020-08-11 10:09
|
|
|
*/
|
|
|
|
|
|
-import io.swagger.annotations.Api;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import springfox.documentation.builders.ApiInfoBuilder;
|
|
|
import springfox.documentation.builders.PathSelectors;
|
|
|
import springfox.documentation.builders.RequestHandlerSelectors;
|
|
|
-import springfox.documentation.service.ApiInfo;
|
|
|
+import springfox.documentation.service.*;
|
|
|
import springfox.documentation.spi.DocumentationType;
|
|
|
+import springfox.documentation.spi.service.contexts.SecurityContext;
|
|
|
import springfox.documentation.spring.web.plugins.Docket;
|
|
|
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
/**
|
|
|
* Swagger使用的配置文件
|
|
|
*/
|
|
|
@Configuration
|
|
|
@EnableSwagger2
|
|
|
public class Swagger2Configuration {
|
|
|
+// @Bean
|
|
|
+// public Docket createRestApi(){
|
|
|
+// return new Docket(DocumentationType.SWAGGER_2)
|
|
|
+// .apiInfo(apiInfo())
|
|
|
+// .select()
|
|
|
+// .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
|
|
|
+// .paths(PathSelectors.any())
|
|
|
+// .build();
|
|
|
+// }
|
|
|
+//
|
|
|
+// //基本信息的配置,信息会在api文档上显示
|
|
|
+// private ApiInfo apiInfo(){
|
|
|
+// return new ApiInfoBuilder()
|
|
|
+// .title("群智平台后端接口文档")
|
|
|
+// .description("群智平台后端接口的文档")
|
|
|
+// .termsOfServiceUrl("http://localhost:8080/api/hello")
|
|
|
+// .version("1.0")
|
|
|
+// .build();
|
|
|
+// }
|
|
|
+
|
|
|
+// @Bean
|
|
|
+// public Docket customDocket(){
|
|
|
+// ParameterBuilder ticketPar = new ParameterBuilder();
|
|
|
+// List<Parameter> pars = new ArrayList<Parameter>();
|
|
|
+// ticketPar.name("Cookie").description("认证cookie")
|
|
|
+// .modelRef(new ModelRef("string")).parameterType("header")
|
|
|
+// .required(false).build(); //header中的ticket参数非必填,传空也可以
|
|
|
+// pars.add(ticketPar.build()); //根据每个方法名也知道当前方法在设置什么参数
|
|
|
+//
|
|
|
+// return new Docket(DocumentationType.SWAGGER_2)
|
|
|
+// .select()
|
|
|
+// .apis(RequestHandlerSelectors.any())
|
|
|
+// .build()
|
|
|
+// .globalOperationParameters(pars)
|
|
|
+// .apiInfo(apiInfo());
|
|
|
+// }
|
|
|
+//
|
|
|
+// //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
|
|
|
+// private ApiInfo apiInfo() {
|
|
|
+// return new ApiInfoBuilder()
|
|
|
+// //页面标题
|
|
|
+// .title("demo")
|
|
|
+// //创建人
|
|
|
+// .contact(new Contact("demo", "", ""))
|
|
|
+// //版本号
|
|
|
+// .version("1.0")
|
|
|
+// //描述
|
|
|
+// .description("demo REST API")
|
|
|
+// .build();
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /* Docket,一个代表了一组API,没有配置组名则为默认组 */
|
|
|
@Bean
|
|
|
- public Docket createRestApi(){
|
|
|
+ public Docket createRestApi() {
|
|
|
return new Docket(DocumentationType.SWAGGER_2)
|
|
|
+ .groupName("task")
|
|
|
.apiInfo(apiInfo())
|
|
|
.select()
|
|
|
- .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
|
|
|
+ .apis(RequestHandlerSelectors.basePackage("com.mooctest.crowd.site.controller"))
|
|
|
.paths(PathSelectors.any())
|
|
|
- .build();
|
|
|
+ .build()
|
|
|
+ /* 设置安全模式,swagger可以设置访问token */
|
|
|
+ .securitySchemes(securitySchemes())
|
|
|
+ .securityContexts(securityContexts());
|
|
|
}
|
|
|
|
|
|
- //基本信息的配置,信息会在api文档上显示
|
|
|
- private ApiInfo apiInfo(){
|
|
|
+ /* API信息 */
|
|
|
+ private ApiInfo apiInfo() {
|
|
|
+
|
|
|
+ /* 联系人 */
|
|
|
+ Contact contact = new Contact("ChaoGuo","https://github.com/crayon-shin-chan","1021172957@qq.com");
|
|
|
return new ApiInfoBuilder()
|
|
|
- .title("群智平台后端接口文档")
|
|
|
- .description("群智平台后端接口的文档")
|
|
|
- .termsOfServiceUrl("http://localhost:8080/api/hello")
|
|
|
+ /* API标题 */
|
|
|
+ .title("群智平台API文档")
|
|
|
+ /* API描述 */
|
|
|
+ .description("简单优雅的restful风格")
|
|
|
+ /* 版本 */
|
|
|
.version("1.0")
|
|
|
+ /* 许可 */
|
|
|
+ .license("Apache License")
|
|
|
+ .contact(contact)
|
|
|
.build();
|
|
|
}
|
|
|
+
|
|
|
+ /* 安全模式,这里指定token通过Authorization头请求头传递 */
|
|
|
+ private List<ApiKey> securitySchemes() {
|
|
|
+ List<ApiKey> apiKeyList= new ArrayList<>();
|
|
|
+ apiKeyList.add(new ApiKey("Cookie", "apiKey", "header"));
|
|
|
+ return apiKeyList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* 安全上下文 */
|
|
|
+ private List<SecurityContext> securityContexts() {
|
|
|
+ List<SecurityContext> securityContexts=new ArrayList<>();
|
|
|
+ securityContexts.add(
|
|
|
+ SecurityContext.builder()
|
|
|
+ .securityReferences(defaultAuth())
|
|
|
+ .forPaths(PathSelectors.any())
|
|
|
+ .build());
|
|
|
+ return securityContexts;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* 默认的安全引用 */
|
|
|
+ List<SecurityReference> defaultAuth() {
|
|
|
+ AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
|
|
|
+ AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
|
|
|
+ authorizationScopes[0] = authorizationScope;
|
|
|
+ List<SecurityReference> securityReferences= new ArrayList<>();
|
|
|
+ securityReferences.add(new SecurityReference("Cookie", authorizationScopes));
|
|
|
+ return securityReferences;
|
|
|
+ }
|
|
|
}
|