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

Swagger UI添加Curl自定义参数及自签名证书HTTPS请求失败问题

Solutions for Your Swagger UI & Curl Issues

Let's break down your problems one by one and solve them step by step:


1. Fixing SSL Certificate Errors (Both Swagger UI and Manual Curl)

The root cause of both errors is the self-signed SSL certificate on your localhost:8088 server—neither curl nor browsers trust self-signed certificates by default.

Manual Curl Error (curl: (60) SSL certificate problem)

You have two practical options here:

  • Temporary development fix: Add the -k (or --insecure) flag to skip SSL certificate verification. Your updated command will look like this:
    curl -X POST "https://localhost:8088/Authenticate" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"username\":\"user\", \"password\": \"user\"}" -k
    
  • Permanent fix: Import your self-signed certificate into your system's trusted root store, or specify the certificate file directly in curl:
    curl -X POST "https://localhost:8088/Authenticate" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"username\":\"user\", \"password\": \"user\"}" --cacert /path/to/your/self-signed-cert.pem
    

Swagger UI Error (TypeError: Failed to fetch)

This occurs because your browser blocks requests to servers with untrusted SSL certificates. Try one of these fixes:

  • Chrome quick fix: Enable the Allow insecure localhost flag by visiting chrome://flags/#allow-insecure-localhost, setting it to "Enabled", and restarting your browser.
  • Import certificate to browser: Export your self-signed certificate from the server, then import it into your browser's trusted root certificate store (steps vary by browser—look for "Manage certificates" in settings).
  • Simplify development: If possible, switch your server to use HTTP instead of HTTPS during development to avoid certificate-related headaches entirely.

2. Adding Custom Curl Parameters to Swagger UI

The implementation depends on how you're serving Swagger UI:

For Spring Boot + Springfox (Swagger 2)

Create a custom CurlCommandConverter to modify the generated curl command:

import springfox.documentation.swagger2.web.CurlCommandConverter;
import springfox.documentation.swagger2.web.RequestContext;
import org.springframework.stereotype.Component;

@Component
public class CustomCurlConverter extends CurlCommandConverter {
    @Override
    public String convert(RequestContext context) {
        String originalCurl = super.convert(context);
        // Add your custom parameter (e.g., -k for insecure connections)
        return originalCurl + " -k";
    }
}

For Spring Boot + SpringDoc (OpenAPI 3)

Implement a CurlOperationCustomizer to inject custom options into the generated curl command:

import org.springdoc.core.customizers.CurlOperationCustomizer;
import org.springframework.stereotype.Component;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.servers.Server;
import org.springdoc.core.service.CurlOperation;

@Component
public class CustomCurlCustomizer implements CurlOperationCustomizer {
    @Override
    public CurlOperation customize(CurlOperation curlOperation, Operation operation, Server server) {
        // Add custom flags (e.g., --insecure)
        curlOperation.getOptions().add("--insecure");
        // You can also append custom headers or other parameters here
        return curlOperation;
    }
}

For Static Swagger UI Deployment (e.g., using swagger-ui-dist)

Modify the Swagger UI initialization script to include curlOptions:

window.ui = SwaggerUIBundle({
  url: "/openapi.json",
  dom_id: '#swagger-ui',
  layout: "StandaloneLayout",
  // Add custom curl parameters
  curlOptions: {
    insecure: true, // Equivalent to the -k/--insecure flag
    // Add other custom options as needed
  }
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:23:25