5

I have a rest service that is documented with OpenAPI 2.0. It is secured via a self-signed ssl-certificate, which cannot be replaced, because it is highly likely, the service will run on localhost in production.

I generate a client from the OpenAPi specification with the swagger-codegen-maven-plugin. The client seems to work, but it rejects the self-signed certificate.

The underlying http library is okhttp, which is able to accept self-signed certificates, but due to the circumstance, I need to be able to regenerate my client anytime, I cannot incorporate the necessary changes in the generated client.

How can I make okhttp accept my certificate?


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>
tgr
  • 3,557
  • 4
  • 33
  • 63
  • If the problem is with the client, then you should probably post your application that uses the client. It is possible to replace the HTTP client in the ApiClient created by the OpenApi codegen. You can create an okHttp client of your own configured to accept self-signed certificates and give that to the ApiClient object. – Shalom Crown Nov 09 '19 at 16:28

0 Answers0