我想使用org.apache.cxf.jaxrs.swagger.Swagger2Feature将安全定义添加到我的rest服务中。然而,我看不到任何相关的方法或资源如何做到这一点。下面是我想用swagger2feature生成的swagger文档。我该怎么做呢?
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'
发布于 2017-03-10 03:10:08
我没有使用Spring Boot,但我复制了@Naoj的方法。(谢谢!)
对于那些没有使用Spring Boot的用户,您可以在CXF servlet之后加载的启动servlet中完成此操作。如果在获取Swagger实例时只修改它,也可以避免扩展类。
所以在web.xml中:
<servlet>
<servlet-name>SwaggerServlet</servlet-name>
<servlet-class>my.package.configuration.SwaggerStartupServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
然后是servlet代码:
/** 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);
}
https://stackoverflow.com/questions/39916418
复制相似问题