首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >CXF Swagger2Feature添加securityDefinitions

CXF Swagger2Feature添加securityDefinitions
EN

Stack Overflow用户
提问于 2016-10-07 19:43:21
回答 1查看 1.6K关注 0票数 2

我想使用org.apache.cxf.jaxrs.swagger.Swagger2Feature将安全定义添加到我的rest服务中。然而,我看不到任何相关的方法或资源如何做到这一点。下面是我想用swagger2feature生成的swagger文档。我该怎么做呢?

代码语言:javascript
复制
swagger: '2.0'
info:
  version: 1.0.0
  title: Based on "Basic Auth Example"
  description: >
    An example for how to use Auth with Swagger.

host: basic-auth-server.herokuapp.com
schemes:
  - http
  - https
securityDefinitions:
  Bearer:
    type: apiKey
    name: Authorization
    in: header
paths:
  /:
    get:
      security:
        - Bearer: []
      responses:
        '200':
          description: 'Will send `Authenticated`'
        '403': 
          description: 'You do not have necessary permissions for the resource'
EN

回答 1

Stack Overflow用户

发布于 2017-03-10 03:10:08

我没有使用Spring Boot,但我复制了@Naoj的方法。(谢谢!)

对于那些没有使用Spring Boot的用户,您可以在CXF servlet之后加载的启动servlet中完成此操作。如果在获取Swagger实例时只修改它,也可以避免扩展类。

所以在web.xml中:

代码语言:javascript
复制
<servlet>
    <servlet-name>SwaggerServlet</servlet-name>
    <servlet-class>my.package.configuration.SwaggerStartupServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>

然后是servlet代码:

代码语言:javascript
复制
/** Name of the security definition */
public static final String SECURITY_NAME = "Bearer";

@Override
public void init(final ServletConfig config) throws ServletException
{
    BeanConfig scanner = (BeanConfig) ScannerFactory.getScanner();
    Swagger swagger = scanner.getSwagger();
    swagger.securityDefinition(SECURITY_NAME, new ApiKeyAuthDefinition("Authorization", In.HEADER));
    config.getServletContext().setAttribute("swagger", swagger);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39916418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档