生成自己签名的证书 ps 你的姓名就是你之后的域名最好是写成一样的
1.通过jdk工具生成keystore(jks)文件
keytool -genkey -keyalg RSA -alias selfsigned -keystore my.keystore -validity 360 -keysize 2048
2.转换为p12
keytool -importkeystore -srckeystore my.keystore -destkeystore keystore.p12 -deststoretype PKCS12
3.使用openssl命令导出.crt
openssl pkcs12 -in keystore.p12 -nokeys -out my.crt
4.使用openssl命令导出.key
openssl pkcs12 -in keystore.p12 -nocerts -nodes -out my.key
项目结构图Springboot搭建的https请求服务
application.properties
server.port=443
server.ssl.key-store=C:\\Users\\logincat\\IdeaProjects\\test\\src\\main\\resources\\my.keystore
server.ssl.key-alias=selfsigned
server.ssl.enabled=true
server.ssl.key-store-password=123456
server.ssl.key-store-type=JKS
build.gradle
buildscript {
//存储一个属于gradle的变量,整个工程都能用,可通过gradle.ext.springBootVersion使用
ext {
springBootVersion = '2.1.2.RELEASE'
}
/*配置仓库地址,从而找到外部依赖
按照你在文件中(build.gradle)仓库的顺序寻找所需依赖(如jar文件),
如果在某个仓库中找到了,那么将不再其它仓库中寻找
*/
repositories {
//mavenLocal()本地库,local repository(${user.home}/.m2/repository)
mavenCentral()//maven的中央仓库
//阿里云Maven远程仓库
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}
/*配置springboot插件加载
*/
dependencies {
// classpath 声明说明了在执行其余的脚本时,ClassLoader 可以使用这些依赖项
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
//使用以下插件
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'//jvm版本要求
// 定义仓库
repositories {
maven{url 'http://maven.aliyun.com/nexus/content/groups/public/'}
maven{url 'https://mvnrepository.com/'}
mavenCentral()
}
// 定义依赖:声明项目中需要哪些依赖
dependencies {
compile 'org.springframework.boot:spring-boot-starter'
compile('org.springframework.boot:spring-boot-starter-web')//引入web模块,springmvc
compile 'org.springframework.boot:spring-boot-starter-test'
compile 'org.springframework.boot:spring-boot-starter-freemarker'
}
go项目结构
go
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
)
func main() {
pool := x509.NewCertPool()
//添加信任的证书
aCrt, err := ioutil.ReadFile("my.crt")
if err != nil {
fmt.Println("读取文件失败:", err)
return
}
pool.AppendCertsFromPEM(aCrt)
proxy := httputil.NewSingleHostReverseProxy(&url.URL{
Scheme: "https",
Host: "wang",
Path: "/test",
})
//下面是跳过验证
//proxy.Transport = &http.Transport{
// TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
//}
proxy.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: pool,
},
}
fmt.Println("程序开始")
http.ListenAndServe("127.0.0.1:8081", proxy)
//https监听
//http.ListenAndServeTLS(":8081",".crt", "server.key",proxy)
}
网友评论