springfox-swagger2设置全局token避免每次输入

  • 缺点:每次调试接口,都要重新输入Authroization

设置全局token避免每次输入

@Bean  
public Docket createRestApi() {  
  return new Docket(DocumentationType.SWAGGER_2)  
      .genericModelSubstitutes(DeferredResult.class)  
      .useDefaultResponseMessages(false)  
      .forCodeGeneration(true)  
      .apiInfo(apiInfo())  
      .pathMapping("/")  
      .select()  
      .apis(RequestHandlerSelectors.basePackage("com.rivtower.beauta.api.resource")) //  
  .paths(PathSelectors.any())  
      .build()  
      .securitySchemes(securitySchemes())  
      .securityContexts(securityContexts());  
}  
  
private ApiInfo apiInfo() {  
  return new ApiInfoBuilder()  
      .title("Swagger API Docs")  
      .description("SwaggerAPI")  
      .termsOfServiceUrl("https://github.com/springfox/springfox-demos")  
      .version("2.0")  
      .build();  
}  
  
private List<ApiKey> securitySchemes() {  
  return Arrays.asList(  
      new ApiKey("Authorization", "Authorization", "header"));  
}  
  
private List<SecurityContext> securityContexts() {  
  return Arrays.asList(  
      SecurityContext.builder()  
          .securityReferences(defaultAuth())  
          .forPaths(PathSelectors.regex("^(?!auth).*$"))  
          .build()  
  );  
}  
  
List<SecurityReference> defaultAuth() {  
  AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");  
  AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];  
  authorizationScopes[0] = authorizationScope;  
 return Arrays.asList(  
      new SecurityReference("Authorization", authorizationScopes));  
}

效果: