0

我是开发 REST Web 服务的新手,现在,我正在尝试通过以下网址添加身份验证:http: //developers-blog.helloreverb.com/enabling-oauth-with-swagger/

不幸的是,我被困在第一步,当它说要编辑资源列表时,我相信这是 api-docs,对吗?那么,如果它由服务生成并且没有在任何地方存储为文件,我应该如何编辑它?

我的 Swagger 版本是 1.2

4

2 回答 2

1

您提供的链接指的是 Swagger 1.2,但最新版本是Swagger 2.0

作为起点,您可以考虑使用editor.swagger.io并查看 petstore 示例,其中包含与身份验证相关的设置

于 2015-07-24T01:23:25.537 回答
0

查看来自 1.3.12 标签的宠物商店示例,这是生成 Swagger 1.2 的最后一个版本 - https://github.com/swagger-api/swagger-core/tree/v1.3.12/samples/java-jaxrs .

具体来说,您需要将定义添加到您的 Bootstrap 类中:

public class Bootstrap extends HttpServlet {
  static {
    // do any additional initialization here, such as set your base path programmatically as such:
    // ConfigFactory.config().setBasePath("http://www.foo.com/");

    ApiInfo info = new ApiInfo(
      "Swagger Sample App",                             /* title */
      "This is a sample server Petstore server.  You can find out more about Swagger " + 
      "at <a href=\"http://swagger.io\">http://swagger.io</a> or on irc.freenode.net, #swagger.  For this sample, " + 
      "you can use the api key \"special-key\" to test the authorization filters", 
      "http://helloreverb.com/terms/",                  /* TOS URL */
      "apiteam@wordnik.com",                            /* Contact */
      "Apache 2.0",                                     /* license */
      "http://www.apache.org/licenses/LICENSE-2.0.html" /* license URL */
    );

    List<AuthorizationScope> scopes = new ArrayList<AuthorizationScope>();
    scopes.add(new AuthorizationScope("email", "Access to your email address"));
    scopes.add(new AuthorizationScope("pets", "Access to your pets"));

    List<GrantType> grantTypes = new ArrayList<GrantType>();

    ImplicitGrant implicitGrant = new ImplicitGrant(
      new LoginEndpoint("http://petstore.swagger.wordnik.com/oauth/dialog"), 
      "access_code");

    grantTypes.add(implicitGrant);

    AuthorizationType oauth = new OAuthBuilder().scopes(scopes).grantTypes(grantTypes).build();

    ConfigFactory.config().addAuthorization(oauth);
    ConfigFactory.config().setApiInfo(info);
  }
}
于 2015-08-03T21:29:48.310 回答