2017-12-04 9 views
0

私のエンドポイントの認証がすべてのOAuth2であるので、私は、プロジェクトをspringbootするswagger2を統合するので、私は次のコードのように、すべてのメソッドにAuthorizationヘッダーを追加する必要があります。Swagger2あまりにも多くの同じ注釈

@ApiImplicitParams({  
    @ApiImplicitParam(paramType="header",name="Authorization",dataType="String",required=true, value="Bearer {access-token}") 
}) 

これらを同じアノテーションが多すぎる場合、それらをリファクタリングするいくつかの方法がありますか?

Swagger2ドケットコード:

@Bean 
public Docket createRestApi() { 
    return new Docket(DocumentationType.SWAGGER_2) 
      .apiInfo(apiInfo()) 
      .ignoredParameterTypes(TokenInfo.class, HttpServletRequest.class, HttpServletResponse.class) 
      .select() 
      .apis(RequestHandlerSelectors.basePackage("com.xxx.yyy.resource")) 
      .paths(PathSelectors.any()) 
      .build(); 
} 
+0

あなたは 'swagger-ui'から' access_token'を通してあなたのAPIをテストしたいですか? –

+0

はい、私はswagger-uiページですべてのエンドポイントをテストするためにaccess_tokenが必要です。実際には、私のテストだけであれば、私はそれを追加する必要はありませんが、テストチームはそれを望みます。 –

+0

あなたはあなたのdocket設定コードを提供できますか? –

答えて

2

私は解決策を見つけるために数日を過ごします。ここでは、access_tokenのドケット設定を提供します。グローバルパラメータを設定すると、すべてのエンドポイントを含む共通パラメータ(access_token)を指定できるので、すべてのエンドポイントに@ApiImplicitParamsを含める必要はありません。次のような可能性のあるドケットの設定(後でapi infoのためにあなた自身のものを変更することができます)。

@Bean 
public Docket api() { 
    return new Docket(DocumentationType.SWAGGER_2) 
      .select() 
      .apis(RequestHandlerSelectors.basePackage("com.xxx.yyy.resource")) 
      .paths(PathSelectors.any()) 
      .build() 
      .globalOperationParameters(commonParameters()) 
      .apiInfo(apiInfo()) 
      .ignoredParameterTypes(TokenInfo.class, HttpServletRequest.class, HttpServletResponse.class) 
      .securityContexts(Lists.newArrayList(securityContext())) 
      .securitySchemes(Lists.newArrayList(apiKey())); 
} 


private List<Parameter> commonParameters() { 
    List<Parameter> parameters = new ArrayList<Parameter>(); 
    parameters.add(new ParameterBuilder() 
      .name("access_token") 
      .description("token for authorization") 
      .modelRef(new ModelRef("string")) 
      .parameterType("query") 
      .required(true) 
      .build()); 

    return parameters; 
} 

private ApiInfo apiInfo() { 
    ApiInfo apiInfo = new ApiInfo(
      "My REST API", 
      "Some custom description of API.", 
      "API TOS", 
      "Terms of service", 
      "[email protected]", 
      "License of API", 
      "API license URL"); 
    return apiInfo; 
} 

private SecurityContext securityContext() { 
    return SecurityContext.builder() 
      .securityReferences(defaultAuth()) 
      .forPaths(PathSelectors.any()) 
      .build(); 
} 

List<SecurityReference> defaultAuth() { 
    AuthorizationScope authorizationScope 
      = new AuthorizationScope("global", "accessEverything"); 
    AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; 
    authorizationScopes[0] = authorizationScope; 
    return Lists.newArrayList(
      new SecurityReference("AUTHORIZATION", authorizationScopes)); 
} 

private ApiKey apiKey() { 
    return new ApiKey("AUTHORIZATION", "access_token", "header"); 
} 

コードを貼り、swagger-uiから試してみてください。ステータスを教えてください。

+0

うまく動作します、ありがとうございます。 –

+0

あなたはまたstackoverflowの仲間のユーザーのための答えを受け入れる必要があります。 @Liuguanghua –

+1

どのようにそれを行うには?私は受け入れボタンを見つけることができません。 –

関連する問題