Spring Boot开启https
来源:互联网 发布:做淘宝赚钱吗怎么做 编辑:程序博客网 时间:2024/06/13 17:09
原文:https://github.com/x113773/testall/issues/1
1. 第一步就是用JDK的keytool工具来创建一个密钥存储(keystore)
`keytool -keystore mykeys.jks -genkey -alias tomcat -keyalg RSA`
记住输入的Enter keystore password(该项目为letmein),剩下的一路回车,直到:
` Is CN=Unknown, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown correct?`
输入yes
2. 把证书添加到项目中
[`src/main/resources/mykeys.jks`](https://github.com/x113773/testall/blob/master/src/main/resources/mykeys.jks)
3. 修改配置文件application.properties,加入如下配置
```
server.port= 8443
server.ssl.key-store= classpath:mykeys.jks
server.ssl.key-store-password= letmein
server.ssl.key-password= letmein
```
4. 配置用户访问http自动跳转到https
[HttpsConfiguration.java](https://github.com/x113773/testall/blob/master/src/main/java/com/ansel/testall/config/HttpsConfiguration.java)
```
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HttpsConfiguration {
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(){
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(httpConnector());
return tomcat;
}
@Bean
public Connector httpConnector(){
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080);//表示用8080端口来供http访问
connector.setSecure(false);
connector.setRedirectPort(8443);//自动重定向到8443端口
return connector;
}
}
- Spring Boot开启https
- spring boot 启用https
- Spring Boot 实现 HTTPS
- Spring boot 使用HTTPS
- Enable HTTPS in Spring Boot
- Spring Boot中启动HTTPS
- Spring Boot中启动HTTPS
- spring boot 采用https启动
- spring boot 配置 https|ssl
- spring-boot 开启Gzip解决办法
- spring boot 开启声明事物
- Spring Boot工程支持HTTP和HTTPS
- 让spring boot支持https和http
- 【spring boot】配置ssl证书实现https
- spring boot设置http https端口
- spring boot 配置ssl证书实现https
- spring-boot 添加http自动转向https
- spring boot 配置https和SSL
- CodeWars:Sum of odd numbers
- HTTP协议
- 信息系统集成有以下几个显著特点
- 线程同步
- android5.1 user版本4G模块
- Spring Boot开启https
- 数据结构:二叉查找树(BST)&平衡二叉树(AVL)
- unflatten_device_tree
- 中国最牛的5位【IT界大佬】30岁在干嘛?
- C++中动态内存管理
- 参展马丁靴
- ChemBioDraw Ultra 15.1破解版下载附教程
- Maven的安装教程
- 将数组A中的内容和数组B中的内容进行交换(数组一样大)