gpt4 book ai didi

java - CXF Swagger2Feature 添加安全定义

转载 作者:行者123 更新时间:2023-11-30 10:37:59 25 4
gpt4 key购买 nike

我想使用 org.apache.cxf.jaxrs.swagger.Swagger2Feature 将安全定义添加到我的其余服务中。但是我看不到任何相关方法或任何有关如何执行此操作的资源。下面是我想使用 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'

最佳答案

我遇到了同样的问题,但找不到适合 CXF 及其 api 的解决方案。我的解决方案如下,创建一个扩展 CXF 的 Swagger2Feature 的类以覆盖 addSwaggerResource 方法,以绑定(bind)安全定义:

/** Name of the security definition */
public static final String SECURITY_NAME = "Bearer";

/** Extends the Swagger2Feature to use the security definition of Swagger */
@Provider(value = Provider.Type.Feature, scope = Provider.Scope.Server)
public class ExtendedSwagger2Feature extends Swagger2Feature {
@Override
protected void addSwaggerResource(Server server, Bus bus) {
super.addSwaggerResource(server, bus);

BeanConfig config = (BeanConfig) ScannerFactory.getScanner();
Swagger swagger = config.getSwagger();
swagger.securityDefinition(SECURITY_NAME, new ApiKeyAuthDefinition("authorization", In.HEADER));
}
}

然后,由于 Swagger 实例在被 swagger api 加载后已被修改,您应该在 servlet 的上下文中“重新注册”它(据我浏览时的理解 Swagger 的代码)。查看 io.swagger.jaxrs.config.SwaggerContextService。为此,我必须在我的 servlet 上下文中创建一个新的 ServletContextInitializer:

return servletContext -> {
BeanConfig scanner = (BeanConfig) ScannerFactory.getScanner();
Swagger swagger = scanner.getSwagger();
servletContext.setAttribute("swagger", swagger);
};

将先前使用安全定义修改过的 Swagger 配置置于上下文中,允许 swagger api 正确考虑它。没有这个,我们扩展的 Swagger2Feature 将无法工作。

通过这些更改,我能够得到一个您期望的 swagger.yaml 文件,尤其是以下部分:

securityDefinitions:
Bearer:
type: apiKey
name: Authorization
in: header

我在 Spring Boot 应用程序中使用此解决方案,这是我完整的 swagger 配置类,以防它对某人有所帮助:

package my.package.configuration;

import io.swagger.config.ScannerFactory;
import io.swagger.core.filter.AbstractSpecFilter;
import io.swagger.jaxrs.config.BeanConfig;
import io.swagger.model.ApiDescription;
import io.swagger.models.Operation;
import io.swagger.models.Swagger;
import io.swagger.models.auth.ApiKeyAuthDefinition;
import io.swagger.models.auth.In;
import org.apache.cxf.Bus;
import org.apache.cxf.annotations.Provider;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.swagger.Swagger2Feature;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import java.util.List;
import java.util.Map;

/**
* Configuration of the Swagger API to enable it with CXF.
*/
@Configuration
public class SwaggerConfiguration {

/** Name of the security definition */
public static final String SECURITY_NAME = "Bearer";

@Bean
public Swagger2Feature swagger() {
Swagger2Feature feature = new ExtendedSwagger2Feature();
// Do your stuff with the configuration
return feature;
}

/**
* Register a custom {@link ServletContextInitializer} in the cxf servlet to expose the custom {@link Swagger2Feature}
* otherwise the security definition added in the {@link ExtendedSwagger2Feature#addSwaggerResource} will not be
* used by the swagger api because the original hook occurs during the super call.
*
* @see io.swagger.jaxrs.config.SwaggerContextService
* @see org.apache.cxf.jaxrs.spring.SpringComponentScanServer
*
* @return a new instance of the {@link ServletContextInitializer}
*/
@Bean
@DependsOn("jaxRsServer")
public ServletContextInitializer initializer() {
return servletContext -> {
BeanConfig scanner = (BeanConfig) ScannerFactory.getScanner();
Swagger swagger = scanner.getSwagger();
servletContext.setAttribute("swagger", swagger);
};
}

/**
* Extension of the {@link Swagger2Feature} because the one provided by CXF doesn't allow to use
* feature of the Swagger API such as the security definition. This feature use the {@link ApiKeyAuthDefinition}
* to transport the authorization header required by the application.
*/
@Provider(value = Provider.Type.Feature, scope = Provider.Scope.Server)
public static class ExtendedSwagger2Feature extends Swagger2Feature {
@Override
protected void addSwaggerResource(Server server, Bus bus) {
super.addSwaggerResource(server, bus);

BeanConfig config = (BeanConfig) ScannerFactory.getScanner();
Swagger swagger = config.getSwagger();
swagger.securityDefinition(SECURITY_NAME, new ApiKeyAuthDefinition("authorization", In.HEADER));
}
}
}

关于java - CXF Swagger2Feature 添加安全定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39916418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com