
@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig {
? ? @Bean
? ? public Docket buildDocket() throws Exception {
? ? ? ? ? ? ? ? ? return new Docket(DocumentationType.SWAGGER_2)
????????????????????????????????????????????????.apiInfo(buildApiInf())
????????????????????????????????????????????????.select().apis(RequestHandlerSelectors.basePackage("com.assist.assessment.app"))//controller路徑
? ? ? ? ? ? ? ? //.paths(PathSelectors.any())
? ? ? ? ? ? ? ? .paths(paths())
????????????????.build()
????????????????.securitySchemes(newArrayList(apiKey()))
????????????????.securityContexts(newArrayList(securityContext()));
? ? }
private Predicate?paths() {
????????????return or(regex("/api.*"));
}
private ApiInfo buildApiInf() {
????????????return new ApiInfoBuilder()
????????????????????????????????.title("評(píng)估 app api")
????????????????????????????????.termsOfServiceUrl("http://團(tuán)隊(duì)鏈接")
????????????????????????????????.description("swagger2 doc 接口文檔描述")
????????????????????????????????.contact(new Contact("開(kāi)發(fā)者接口", "http://**.com", "248xx@qq.com"))
????????????????????????????????.build();
}
private ApiKey apiKey() {
????????????????????????????????return new ApiKey("clientSecret", "客戶端密匙 api_key", "header");
}
private SecurityContext securityContext()throws Exception {
????????????????????????????????return SecurityContext.builder()
????????????????????????????????.securityReferences(defaultAuth())
????????????????????????????????.forPaths(regex("/anyPath.*"))
????????????????????????????????.build();
}
List<SecurityReference>?defaultAuth() {
????????????????????????AuthorizationScope authorizationScope
????????????????????????????????= new AuthorizationScope("global", "accessEverything");
? ? ? ????????????????? AuthorizationScope[] authorizationScopes =new AuthorizationScope[1];
? ? ? ????????????????? authorizationScopes[0] = authorizationScope;
? ? ? ? ????????????????return newArrayList(
????????????????????????new SecurityReference("clientSecret", authorizationScopes));
}
@Bean
?SecurityConfiguration security() {
????????????????????????return new SecurityConfiguration(
????????????????????????????"test-app-client-id",
? ? ? ? ? ? ? ????????????? "test-app-client-secret",
? ? ? ? ? ? ? ? ????????????"test-app-realm",
? ? ? ? ? ? ? ? ????????????"test-app",
? ? ? ? ? ? ? ????????????? "apiKey",
? ? ? ? ? ? ? ????????????? ApiKeyVehicle.HEADER,
? ? ? ? ? ? ? ????????????? "api_key",
? ? ? ? ? ? ? ????????????? "," /*scope separator*/);
?}
@Bean
UiConfiguration uiConfig() {
????????????????????????????return new UiConfiguration(
????????????????????????????????????????"validatorUrl",// url
? ? ? ? ? ????????????????????????? ? ? "none",? ? ? // docExpansion? ? ? ? ? => none | list
? ? ? ? ????????????????????????? ? ? ? "alpha",? ? ? // apiSorter? ? ? ? ? ? => alpha
? ? ????????????????????????? ? ? ? ? ? "schema",? ? // defaultModelRendering => schema
? ? ? ? ????????????????????????? ? ? ? UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS,
? ? ? ? ? ????????????????????????? ? ? false,? ? ? ? // enableJsonEditor? ? ? => true | false
? ? ? ? ? ????????????????????????? ? ? true,? ? ? ? // showRequestHeaders? ? => true | false
? ? ? ? ? ? ????????????? ? 60000L);? ? ? // requestTimeout => in milliseconds, defaults to null (uses jquery xh timeout)
? ? }
}


http://localhost:8080/swagger-ui.html#/


