swagger2配置token(将token放在请求头中)

swagger2配置token(将token放在请求头中)

@Configuration
@EnableSwagger2
public class Swagger2 {
	// http://localhost:8080/swagger-ui.html	原访问路径
	// http://localhost:8080/doc.html   		友好体验版的访问路径

	//swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
	@Bean
	public Docket createRestApi() {
		List<Parameter> parameters = new ArrayList<>();
		parameters.add(new ParameterBuilder()
                .name("token")
                .description("认证token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build());
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.select()
				.apis(RequestHandlerSelectors.basePackage("com.fuju.app.controller"))
				.paths(PathSelectors.any())
				.build()
				.securitySchemes(securitySchemes())
				.securityContexts(securityContexts())
				.globalOperationParameters(parameters);
	}
	
	private List<ApiKey> securitySchemes() {
		List<ApiKey> apiKeyList = new ArrayList<>();
		apiKeyList.add(new ApiKey("Authorization", "Authorization", "header"));
		return apiKeyList;
	}


	//构建 api文档的详细信息函数,注意这里的注解引用的是哪个
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				//页面标题
				.title("拉钩网")
				//创建人 
				.contact(new Contact("拉钩网接口地址", "", null))
				// 描述
				.description("专为   拉钩教育提供的在线文档,可根据各个api接口的规范来调用各种接口~~")
				//版本号
				.version("1.0")
				.termsOfServiceUrl("http://localhost:8080/parkmanager/swagger-ui.html")
				.build();
	}
	
	private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("^(?!app).*$")).build());
        return securityContexts;
    }
 
    private 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("Authorization", authorizationScopes));
        return securityReferences;
    }
	
}

效果图

swagger2配置token(将token放在请求头中)_第1张图片

你可能感兴趣的:(java,接口)