Swagger添加全局Header和Basic Auth参数

package com.daimler.esbapi.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.swagger.annotations.Api;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.BasicAuth;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.service.SecurityScheme;
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;

@Configuration
@EnableSwagger2
public class Swagger2Config{
	
	@Bean
    public Docket authApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("Normal")
                .select() 
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts())
                .pathMapping("/")
                .apiInfo(apiInfo());
    }
	
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("App title")
				.description("App description")
				.version("1.0")
				.build();
	}
	
	private List<SecurityScheme> securitySchemes() {
		List<SecurityScheme> res = new ArrayList<>();
		res.add(new BasicAuth("basicAuth"));
		res.add(new ApiKey("name1", "keyname1", "header"));
		res.add(new ApiKey("name2", "keyname2", "header"));
		res.add(new ApiKey("name3", "keyname3", "header"));
		return res;
    }

    private List<SecurityContext> securityContexts() {
    	List<SecurityContext> res = new ArrayList<>();
    	res.add(SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("/.*"))
                        .build());
    	return res;
    }
    
    private List<SecurityReference> defaultAuth() {
    	List<SecurityReference> res = new ArrayList<>();
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        res.add(new SecurityReference("basicAuth", authorizationScopes));
        res.add(new SecurityReference("name1", authorizationScopes));
        res.add(new SecurityReference("name2", authorizationScopes));
        res.add(new SecurityReference("name3", authorizationScopes));
        return res;
    }

}

你可能感兴趣的:(Swagger添加全局Header和Basic Auth参数)