2017-03-06 121 views
3

我正在使用swagger来记录我的Java REST API。 X-Auth-Token应该在每个API(除了一个)的头部中发送。 我想在授权的宠物商店V2中有这样的按钮。 可以在这里找到:http://petstore.swagger.io/Swagger批注以获得授权按钮

我明白它是在jason \ yaml文件中定义的,由swagger生成。 确切的说是在YAML做过这样的:

securityDefinitions: 
    petstore_auth: 
    type: "oauth2" 
    authorizationUrl: "http://petstore.swagger.io/oauth/dialog" 
    flow: "implicit" 
    scopes: 
     write:pets: "modify pets in your account" 
     read:pets: "read your pets" 
    api_key: 
    type: "apiKey" 
    name: "api_key" 
    in: "header" 

我所有的招摇的文档我的注释做了。但是我找不到这个按钮的注释。 你能帮我找到这个注解吗?

谢谢!

+0

曾经找到一个解决方案:

所以最后我通过调用Ajax调用取回一个令牌,并调用此方法做了一种自动授权? –

+0

通过在@SwaggerDefinition中添加字段。我会在星期天更新答案 – sosolo

回答

3

我用:

@SwaggerDefinition(securityDefinition = @SecurityDefinition(apiKeyAuthDefinitions = {@ApiKeyAuthDefinition(key = "X-Auth-Token", 
    in = ApiKeyAuthDefinition.ApiKeyLocation.HEADER, name = "X-Auth-Token")})) 

然后我得到了在扬鞭-UI的 “授权” 按钮。 我检查了它做了什么 - 当`self = window.swaggerUi;'时,它调用方法self.api.clientAuthorizations.add

​​
+0

@tisbar帮助? – sosolo

+0

不知道@tisbar,但你肯定帮了我。为我节省了无数日子,谢谢! –