私は同じ問題に直面していましたが、CXFとそのAPIに適したソリューションが見つかりませんでした。私のソリューションは、セキュリティ定義をバインドするために、addSwaggerResourceメソッドをオーバーライドするために、CXFのSwagger2Featureを拡張するクラスを作成し、以下のとおりである:
/** 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 APIによってロードされた場合、サーブレットのコンテキストでそれを「再登録」する必要があります(私がswaggerのコードをブラウズしたときにわかるように)。 io.swagger.jaxrs.config.SwaggerContextServiceをご覧ください。これを行うには、私は私のサーブレットコンテキストに新しいServletContextInitializerを作成する必要がありました:
return servletContext -> {
BeanConfig scanner = (BeanConfig) ScannerFactory.getScanner();
Swagger swagger = scanner.getSwagger();
servletContext.setAttribute("swagger", swagger);
};
闊歩設定は、以前にセキュリティ定義を変更し、コンテキストに置く闊歩APIが正しく考慮に入れ、それを取ることができます。これがなければ、拡張Swagger2Featureは動作しません。この変更により
、私はあなたが期待している1、特に以下の一環としてswagger.yamlファイルを取得することができた:
securityDefinitions:
Bearer:
type: apiKey
name: Authorization
in: header
私はここで、春のブートアプリケーションでこのソリューションをされて使用しています私の完全な闊歩の設定クラス、ケースにはそれが誰か助け:
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));
}
}
}