window本机搭建https环境(两种方式)
一、(方式一)通过openssl安装ssl证书搭建https1.下载opensslhttp://slproweb.com/products/Win32OpenSSL.html2.安装之后配置环境变量OPENSSL_HOME…\binPath变量末尾加;%OPENSSL_HOME%3. 创建私钥 openssl genrsa -des3 -out name.key 1024需要记住输入的密码name
·
一、(方式一)通过openssl 安装ssl证书搭建https
1.下载openssl http://slproweb.com/products/Win32OpenSSL.html
2.安装之后配置环境变量 OPENSSL_HOME …\bin Path变量末尾加;%OPENSSL_HOME%
3. 创建私钥 openssl genrsa -des3 -out name.key 1024 需要记住输入的密码 name为自定义的名字
4.创建ssr证书 openssl req -new -key name.key -out name.csr 需要输入一些列信息,最重要的是Common Name表示要使用https访问的域名
5.去除密码 复制name.key重命名为name.copy.key
6.执行openssl rsa -in name.copy.key -out name.key
7.生成crt证书 openssl x509 -req -days 365 -in test.csr -signkey name.key -out name.crt

8.修改nginx的配置文件

同时配置好你的hosts文件

二 (方式二)jks文件生成和验证
1.生成
keytool -genkey -alias jwt -keyalg RSA -keysize 1024 -keystore jwt.jks -validity 365
2. 口令输入123456,其他直接回车,确认填写“是”

3.验证
把生成的jwt.jks拷贝到项目类路径下,编写代码测试
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
@Slf4j
public class JksUtil {
public static void main(String[] args) throws Exception {
PrivateKey privateKey = getPrivateKey("jwt.jks", "123456", "jwt");
log.info("" + privateKey);
PublicKey publicKey = getPublicKey("jwt.jks", "123456", "jwt");
log.info("" + publicKey);
}
private static PrivateKey getPrivateKey(String fileName, String password, String alias) throws KeyStoreException,
IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException {
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(inputStream, "123456".toCharArray());
return (PrivateKey) keyStore.getKey("jwt", "123456".toCharArray());
}
private static PublicKey getPublicKey(String fileName, String password, String alias) throws KeyStoreException,
IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException {
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(inputStream, "123456".toCharArray());
return keyStore.getCertificate("jwt").getPublicKey();
}
}
4.在SpringBoot中配置
将xxx.jks放入resources文件夹下
#配置jks存放位置
server.ssl.key-store=classpath:xxx.jks
#https端口号
server.port=8090
#是否启用SSL证书
server.ssl.enabled=true
#密钥库密码
server.ssl.key-store-password=jks密码
#密钥库类型(JKS类型)
server.ssl.key-store-type=JKS
同时配置http和https都能访问
新加配置
#http端口号
http.port=18092
在SpringBoot启动类中添加
// 获取配置端口
@Value("${http.port}")
private Integer httpPort;
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
// 添加http
tomcat.addAdditionalTomcatConnectors(createStandardConnector());
return tomcat;
}
/**
* 配置http
*
* @return connector
*/
private Connector createStandardConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(httpPort);
return connector;
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)