使用 Jersey 2 和 Spring 引导向注释驱动的 swagger.json 添加授权
Adding authorization to Annotation-driven swagger.json with Jersey 2 and Spring Boot
我正在尝试将基本身份验证添加到 Swagger UI 以用于使用 Spring Boot 构建的带有 Swagger 注释的 Jersey 2.0 Web 服务。我正在使用:
- Spring 引导 1.5.4
- spring-启动球衣
- 招摇 UI 3.0.4
- (Maven 包)swagger-jersey2-jaxrs 1.5.13
我能够使用以下 JerseyConfig 和我的资源上的 Swagger 注释生成一个 swagger.json 文件。 This article was immensely helpful in getting this far.
@Component
public class JerseyConfiguration extends ResourceConfig {
private static Log logger = LogFactory.getLog(JerseyConfiguration.class);
@Value("${spring.jersey.application-path:/}")
private String apiPath;
public JerseyConfiguration() {
registerEndpoints();
configureSwagger();
}
private void registerEndpoints() {
register(MyEndpoints.class);
// Generate Jersey WADL at /<Jersey's servlet path>/application.wadl
register(WadlResource.class);
// Lets us get to static content like swagger
property(ServletProperties.FILTER_STATIC_CONTENT_REGEX, "((/swagger/.*)|(.*\.html))");
}
/**
* Configure the Swagger documentation for this API.
*/
private void configureSwagger() {
// Creates file at localhost:port/swagger.json
this.register(ApiListingResource.class);
this.register(SwaggerSerializers.class);
BeanConfig config = new BeanConfig();
config.setConfigId("example-jersey-app");
config.setTitle("Spring Boot + Jersey + Swagger");
config.setVersion("2");
config.setContact("Me <me@example.com>");
config.setSchemes(new String[] {"http", "https"});
config.setResourcePackage("com.example.api");
config.setBasePath(this.apiPath);
config.setPrettyPrint(true);
config.setScan(true);
}
}
现在我希望能够使用基本身份验证从 Swagger UI 连接到这些服务。我已经在 Spring 中对其进行了配置,并且可以使用它来对站点进行身份验证,但不能从 Swagger UI 中进行身份验证。
不幸的是,Spring Boot examples currently on the Swagger sample site 的 none 包含 Jersey 和身份验证,而 Jersey 示例的 none 使用 Spring Boot 和 @SpringBootApplication
就像我一样在我的项目中使用。
如何让 Basic Auth 显示在 Swagger 中 UI?
我可以通过将 ServletConfigAware
添加到 JerseyConfiguration
来实现它。然后我可以使用 Swagger Bootstrap.java examples.
中使用的相同样式的 Swagger 配置
@Component
public class JerseyConfiguration extends ResourceConfig implements ServletConfigAware{
private ServletConfig servletConfig;
// ... this is all unchanged ...
/**
* Configure the Swagger documentation for this API.
*/
private void configureSwagger() {
// Creates file at localhost:port/swagger.json
this.register(ApiListingResource.class);
this.register(SwaggerSerializers.class);
BeanConfig config = new BeanConfig();
// ... this is all unchanged ...
config.setScan(true);
Swagger swagger = new Swagger();
swagger.securityDefinition("basicAuth", new BasicAuthDefinition());
new SwaggerContextService().withServletConfig(servletConfig).updateSwagger(swagger);
}
@Override
public void setServletConfig(ServletConfig servletConfig) {
logger.info("Setting ServletConfig");
this.servletConfig = servletConfig;
}
}
进行这些更改并向我的端点添加如下注释后:
@Api(value = "/api", description = "My super API",
authorizations = {@Authorization(value="basicAuth")})
@Path("api")
@Component
public class MyApi {
我看到了以下变化:
添加到我的 swagger.json:
"securityDefinitions":{"basicAuth":{"type":"basic"}}
...
"security":[{"basicAuth":[]}]}}
此外,在 Swagger UI 中,一个新的绿色按钮出现在与方案下拉列表相同的行中,上面写着 "Authorize",上面有一个打开的锁。如果我点击它,会出现一个弹出窗口,我可以在其中输入用户名和密码。现在,当我使用 Swagger UI "Try It" 功能时,这些凭证会发送到 API。
我正在尝试将基本身份验证添加到 Swagger UI 以用于使用 Spring Boot 构建的带有 Swagger 注释的 Jersey 2.0 Web 服务。我正在使用:
- Spring 引导 1.5.4
- spring-启动球衣
- 招摇 UI 3.0.4
- (Maven 包)swagger-jersey2-jaxrs 1.5.13
我能够使用以下 JerseyConfig 和我的资源上的 Swagger 注释生成一个 swagger.json 文件。 This article was immensely helpful in getting this far.
@Component
public class JerseyConfiguration extends ResourceConfig {
private static Log logger = LogFactory.getLog(JerseyConfiguration.class);
@Value("${spring.jersey.application-path:/}")
private String apiPath;
public JerseyConfiguration() {
registerEndpoints();
configureSwagger();
}
private void registerEndpoints() {
register(MyEndpoints.class);
// Generate Jersey WADL at /<Jersey's servlet path>/application.wadl
register(WadlResource.class);
// Lets us get to static content like swagger
property(ServletProperties.FILTER_STATIC_CONTENT_REGEX, "((/swagger/.*)|(.*\.html))");
}
/**
* Configure the Swagger documentation for this API.
*/
private void configureSwagger() {
// Creates file at localhost:port/swagger.json
this.register(ApiListingResource.class);
this.register(SwaggerSerializers.class);
BeanConfig config = new BeanConfig();
config.setConfigId("example-jersey-app");
config.setTitle("Spring Boot + Jersey + Swagger");
config.setVersion("2");
config.setContact("Me <me@example.com>");
config.setSchemes(new String[] {"http", "https"});
config.setResourcePackage("com.example.api");
config.setBasePath(this.apiPath);
config.setPrettyPrint(true);
config.setScan(true);
}
}
现在我希望能够使用基本身份验证从 Swagger UI 连接到这些服务。我已经在 Spring 中对其进行了配置,并且可以使用它来对站点进行身份验证,但不能从 Swagger UI 中进行身份验证。
不幸的是,Spring Boot examples currently on the Swagger sample site 的 none 包含 Jersey 和身份验证,而 Jersey 示例的 none 使用 Spring Boot 和 @SpringBootApplication
就像我一样在我的项目中使用。
如何让 Basic Auth 显示在 Swagger 中 UI?
我可以通过将 ServletConfigAware
添加到 JerseyConfiguration
来实现它。然后我可以使用 Swagger Bootstrap.java examples.
@Component
public class JerseyConfiguration extends ResourceConfig implements ServletConfigAware{
private ServletConfig servletConfig;
// ... this is all unchanged ...
/**
* Configure the Swagger documentation for this API.
*/
private void configureSwagger() {
// Creates file at localhost:port/swagger.json
this.register(ApiListingResource.class);
this.register(SwaggerSerializers.class);
BeanConfig config = new BeanConfig();
// ... this is all unchanged ...
config.setScan(true);
Swagger swagger = new Swagger();
swagger.securityDefinition("basicAuth", new BasicAuthDefinition());
new SwaggerContextService().withServletConfig(servletConfig).updateSwagger(swagger);
}
@Override
public void setServletConfig(ServletConfig servletConfig) {
logger.info("Setting ServletConfig");
this.servletConfig = servletConfig;
}
}
进行这些更改并向我的端点添加如下注释后:
@Api(value = "/api", description = "My super API",
authorizations = {@Authorization(value="basicAuth")})
@Path("api")
@Component
public class MyApi {
我看到了以下变化:
添加到我的 swagger.json:
"securityDefinitions":{"basicAuth":{"type":"basic"}}
...
"security":[{"basicAuth":[]}]}}
此外,在 Swagger UI 中,一个新的绿色按钮出现在与方案下拉列表相同的行中,上面写着 "Authorize",上面有一个打开的锁。如果我点击它,会出现一个弹出窗口,我可以在其中输入用户名和密码。现在,当我使用 Swagger UI "Try It" 功能时,这些凭证会发送到 API。