You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改swagger-codegen-maven-plugin生成的ApiClient默认协议为TLSv1.2?

如何修改swagger-codegen-maven-plugin生成的ApiClient默认协议为TLSv1.2

问题描述

我使用v3.0.33版本的swagger-codegen-maven插件,Java 11环境,想要将生成的ApiClient默认协议从TLS改为TLSv1.2。尝试过在Maven命令行添加-Dhttps.protocols=TLSv1.2参数但未生效,查阅插件官方文档也没找到相关配置项,想问是否可以通过pom.xml中的属性实现该修改?

生成的ApiClient代码片段

...
private void applySslSettings() {
        try {
            TrustManager[] trustManagers = null;
            HostnameVerifier hostnameVerifier = null;
            if (!verifyingSsl) {
                TrustManager trustAll = new X509TrustManager() {
                    @Override
                    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
                    @Override
                    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
                    @Override
                    public X509Certificate[] getAcceptedIssuers() { return null; }
                };
                SSLContext sslContext = SSLContext.getInstance("TLS");
                trustManagers = new TrustManager[]{ trustAll };
    ...

当前pom.xml插件配置

<plugin>
                <groupId>io.swagger.codegen.v3</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/admin.yaml</inputSpec>
                            <output>${generation.path}</output>
                            <language>java</language>
                            <addCompileSourceRoot>false</addCompileSourceRoot>
                            <generateApiTests>false</generateApiTests>
                            <generateApiDocumentation>false</generateApiDocumentation>
                            <generateModelTests>false</generateModelTests>
                            <generateModelDocumentation>false</generateModelDocumentation>
                            <configOptions>
                                <dateLibrary>joda</dateLibrary>
                                <modelPackage>${codegen.model}</modelPackage>
                                <apiPackage>${codegen.api}</apiPackage>
                                <configPackage>${codegen.configuration}</configPackage>
                                <basePackage>${codegen.base}</basePackage>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
   </plugin>

解决方案

方法1:自定义模板(从根源修改生成逻辑)

swagger-codegen支持通过自定义模板覆盖默认生成代码,步骤如下:

  1. 把官方Java模板中的ApiClient.mustache文件复制到你项目的src/main/resources/swagger-templates/java目录下;
  2. 打开该模板文件,找到SSLContext.getInstance("TLS")这一行,将"TLS"替换为"TLSv1.2";
  3. 在pom.xml的插件配置里添加templateDirectory参数,指定自定义模板路径:
<configuration>
    <!-- 保留原有所有配置 -->
    <templateDirectory>${project.basedir}/src/main/resources/swagger-templates/java</templateDirectory>
</configuration>

重新执行Maven生成命令,生成的ApiClient就会默认使用TLSv1.2协议。

方法2:生成后代码层面修改(无需维护模板)

如果不想自定义模板,可以在生成代码后通过以下方式修改:

  • 编写ApiClient的子类,重写applySslSettings方法,将协议改为TLSv1.2;
  • 或者在初始化ApiClient时手动替换SSL上下文:
ApiClient apiClient = new ApiClient();
// 初始化TLSv1.2上下文
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null, apiClient.getTrustManagers(), new SecureRandom());
// 设置到ApiClient
apiClient.setSslContext(sslContext);

补充说明

命令行的-Dhttps.protocols=TLSv1.2是JVM级别的HTTPS协议配置,但swagger-codegen生成的ApiClient是硬编码指定SSL协议,所以这个参数无法影响生成的代码逻辑,只会作用于JVM默认的HTTPS客户端,因此对你的场景不生效。

内容的提问来源于stack exchange,提问作者favisan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 00:27:20