java net 配置局域网受信任的https

安装 Chocolatey(Windows 包管理器

Windows PowerShell ISE 安装Chocolatey

ini 复制代码
Set-ExecutionPolicy Bypass -Scope Process -Force;
\[System.Net.ServicePointManager\]::SecurityProtocol =
\[System.Net.ServicePointManager\]::SecurityProtocol -bor 3072; iex
((New-Object
System.Net.WebClient).DownloadString(\'https://community.chocolatey.org/install.ps1\'))

验证安装

css 复制代码
choco \--version

使用 choco 安装 mkcert

[在管理员 PowerShell 中运行:]{.mark}

复制代码
choco install mkcert

验证 mkcert 是否正常

复制代码
mkcert -install

然后为你的局域网 IP 生成证书(例如 192.168.5.31):

复制代码
mkcert 192.168.5.31

这时候会得到2个文件

vbnet 复制代码
192.168.5.31.pem
192.168.5.31-key.pem

asp.net core 启动设置证书

使用openssl 将文件转成 pfx

bash 复制代码
openssl pkcs12 -export -out server.pfx -inkey 192.168.5.31-key.pem -in
192.168.5.31.pem -name \"192.168.5.31\" -password pass:yourpassword123

代码配置

ini 复制代码
// 配置 Kestrel 使用 HTTPS 证书

builder.WebHost.ConfigureKestrel(serverOptions =>
{
    var pfxPath = Path.Combine(builder.Environment.ContentRootPath, "certs", "server.pfx");
    var pfxPassword = "yourpassword123"; 
    if (File.Exists(pfxPath) )
    {
        serverOptions.Listen(new IPEndPoint(IPAddress.Parse("192.168.5.31"),5018),options =>
        {
            options.UseHttps(pfxPath, pfxPassword);
        });
    }
    else
    {
        Console.WriteLine("⚠️ 证书未找到,使用开发证书(浏览器可能仍报错)");
        serverOptions.ListenAnyIP(5001, options =>
        {
            options.UseHttps(); // 使用 ASP.NET Core 开发证书
        });
}
});

配置文件配置

json 复制代码
{
  "Kestrel": {
    "Certificates": {
      "Default": {
        "Path": "certs/server.pfx",
        "Password": "yourpassword123"
      }
    },
    "Endpoints": {
      "Https": {
        "Url": "https://192.168.5.31:5018",
        "Certificate": {
          "Path": "certs/server.pfx",
          "Password": "yourpassword123"
        }
      },
      "Http": {
        "Url": "http://192.168.5.31:5000"
      }
    }
  }
}

java启动设置证书

java 支持 .p12 格式(比 .jks 更现代),推荐使用。

objectivec 复制代码
openssl pkcs12 -export \
  -out server.p12 \
  -inkey 192.168.5.31-key.pem \
  -in 192.168.5.31.pem \
  -name "tomcat" \
  -password pass:changeit

将 server.p12 放入 Java 项目

css 复制代码
your-spring-boot-app/
├── src/
│ └── main/
│ ├── resources/
│ │ └── server.p12 ← 放这里
│ └── java/
│ └── com/example/demo/DemoApplication.java
├── application.yml

配置 application.yml(Spring Boot)

yaml 复制代码
server:
  port: 8443
  ssl:
    enabled: true
    key-store-type: PKCS12
    key-store: classpath:server.p12
    key-store-password: changeit
    key-alias: tomcat
    # client-auth: want/need(双向认证可选)

启动 Spring Boot 应用

arduino 复制代码
mvn spring-boot:run
\# 或
java -jar your-app.jar
相关推荐
uzong6 小时前
Mermaid: AI 时代画图的魔法工具
后端·架构
q***69777 小时前
Spring Boot与MyBatis
spring boot·后端·mybatis
IUGEI8 小时前
synchronized的工作机制是怎样的?深入解析synchronized底层原理
java·开发语言·后端·c#
间彧8 小时前
GraalVM Native Image:跨平台能力与编译模式深度解析
后端
间彧8 小时前
GraalVM Native Image 与传统 JVM 内存管理:云原生时代的技术选型指南
后端
r***12388 小时前
SpringBoot最佳实践之 - 使用AOP记录操作日志
java·spring boot·后端
b***74888 小时前
前端GraphQL案例
前端·后端·graphql
LSL666_9 小时前
SpringBoot自动配置类
java·spring boot·后端·自动配置类
q***78379 小时前
Spring Boot 3.X:Unable to connect to Redis错误记录
spring boot·redis·后端
t***26599 小时前
SpringBoot + vue 管理系统
vue.js·spring boot·后端