2015-09-03 14 views
12
を宣言する

はどのようにして闊歩2.0アノテーションを使用して基本認証を定義し、それが闊歩のUIに表示されています。闊歩2.0基本認証スキーマ

私が持っているリソースの場合:

https://github.com/swagger-api/swagger-core/wiki/Annotations#authorization-authorizationscope

をそして、あなたはあなたがあなたのAPIでサポートする認証方式を宣言して設定したら、それは」言う、:

@ApiOperation(value = "Return list of categories", response=Category.class, responseContainer="List", httpMethod="GET", authorizations = {@Authorization(value="basicAuth")}) 
public Response getCategories(); 

私はここに見えましたあなたは、リソースまたは特定の操作」に必要です。しかし、私は宣言し、認証スキームを設定する場所について語って何かを見つけることができないの認可スキームに注意することは、これらのアノテーションを使用することができます。

更新:

私は、スキーマを宣言する方法についてのコードを見つけましたが、私はまだUIでの認証スキーマに関する情報は表示されません。私が迷っているものが分からない

@SwaggerDefinition 
public class MyApiDefinition implements ReaderListener { 
    public static final String BASIC_AUTH_SCHEME = "basicAuth"; 

    @Override 
    public void beforeScan(Reader reader, Swagger swagger) { 
    } 

    @Override 
    public void afterScan(Reader reader, Swagger swagger) { 
     BasicAuthDefinition basicAuthDefinition = new BasicAuthDefinition(); 
     swagger.addSecurityDefinition(BASIC_AUTH_SCHEME, basicAuthDefinition); 
    } 
} 
+0

この問題を解決できましたか? –

答えて

2

私はこれも苦労しました。私の場合、swagger-maven-pluginを使用しました。

<securityDefinitions> 
    <securityDefinition> 
    <name>basicAuth</name> 
    <type>basic</type> 
    </securityDefinition> 
</securityDefinitions> 

その後、私はこのように私のリソースにそれを追加することができました:これ、私はMavenプラグインの中にこれを追加解決するために

@Api(value = "My REST Interface", authorizations = {@Authorization(value="basicAuth")}) 

を生成したJSONは、各エンドポイントのセキュリティ要素を含んでいました:

"security":[{ 
    "basicAuth" : [] 
}] 

とセキュリティの定義:

"securityDefinitions" : { 
    "basicAuth" : { 
     "type" : "basic" 
    } 
    } 

私は、これは同様に他の人に役立ちます願っています。

5

だ、あなたは最初のセキュリティの一つとして、基本認証を定義する必要がありますあなたの設定でDocketを設定するときは、次のようになります。

List<SecurityScheme> schemeList = new ArrayList<>(); 
schemeList.add(new BasicAuth("basicAuth")); 

return new 
    Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo) 
            .securitySchemes(schemeList) 
            ... 

次に、Springfo xアノテーションを使用して、認証を要求する操作のBasic Authを設定します。

@ApiOperation(value = "Return list of categories", response=Category.class, responseContainer="List", httpMethod="GET", authorizations = {@Authorization(value="basicAuth")}) 
public Response getCategories();