生成OpenAPI客户端时如何接受自签名证书?

时间:2018-06-25 09:43:49

标签: ssl ssl-certificate self-signed swagger-codegen openapi

我有一个OpenAPI 2.0记录的休息服务。该服务通过自签名ssl证书进行保护,该证书无法替换,因为很有可能该服务将在生产环境中的localhost上运行。

我使用swagger-codegen-maven-plugin从OpenAPi规范生成一个客户端。客户端似乎可以工作,但是拒绝自签名证书。

基础的http库是okhttp,它可以接受自签名证书,但是由于这种情况,我需要能够随时重新生成客户端,因此无法在生成的文件中加入必要的更改客户。

如何使okhttp接受我的证书?


pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.playground</groupId>
  <artifactId>api-client</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <gson-fire.version>1.8.0</gson-fire.version>
    <swagger-core.version>1.5.15</swagger-core.version>
    <okhttp.version>2.7.5</okhttp.version>
    <gson.version>2.8.1</gson.version>
    <threetenbp.version>1.3.5</threetenbp.version>
    <junit-version>4.12</junit-version>
    <swagger-codegen-maven-plugin.version>2.3.1</swagger-codegen-maven-plugin.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.playground</groupId>
      <artifactId>playground-api</artifactId>
      <version>0.1.1-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>
    <!-- Tag: Swagger SDK Dependencies -->
    <dependency>
      <groupId>io.swagger</groupId>
      <artifactId>swagger-annotations</artifactId>
      <version>${swagger-core.version}</version>
    </dependency>
    <dependency>
      <groupId>com.squareup.okhttp</groupId>
      <artifactId>okhttp</artifactId>
      <version>${okhttp.version}</version>
    </dependency>
    <dependency>
      <groupId>com.squareup.okhttp</groupId>
      <artifactId>logging-interceptor</artifactId>
      <version>${okhttp.version}</version>
    </dependency>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>${gson.version}</version>
    </dependency>
    <dependency>
      <groupId>io.gsonfire</groupId>
      <artifactId>gson-fire</artifactId>
      <version>${gson-fire.version}</version>
    </dependency>
    <dependency>
      <groupId>org.threeten</groupId>
      <artifactId>threetenbp</artifactId>
      <version>${threetenbp.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit-version}</version>
      <scope>test</scope>
    </dependency>
    <!-- End: Swagger SDK Dependencies -->
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-codegen-maven-plugin</artifactId>
        <version>${swagger-codegen-maven-plugin.version}</version>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>${project.basedir}/src/main/resources/spec.json</inputSpec>
              <language>java</language>
              <generateApiTests>false</generateApiTests>
              <configOptions>
                <sourceFolder>src/gen/java/main</sourceFolder>
              </configOptions>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

0 个答案:

没有答案