springboot配置http跳转https的过程
作者:乐兰特 时间:2021-12-25 07:31:11
SSL是为网络通信提供安全以及保证数据完整性的的一种安全协议,SSL在网络传输层对网络连接进行加密。
例:cas 的单点登陆就用到了SSL
一、安全证书的生成
1、可以使用jdk自带的证书生成工具,jdk自带一个叫keytool的证书管理工具,可以用它来实现签名的证书。
2、先配置好基本的java环境,ctrl+r 输入cmd ,进入java 的目录
3、例:生成一个别名叫tomcat 的证书 先使用命令进入jdk的bin 这里的密码是123456
keytool -genkey -alias tomcat -keypass 123456 -keyalg RSA -keysize 1024 -validity 365 -keystore D:/keys/tomcat.keystore -storepass 123456
4、获取一个tomcat.keystore的文件,将这个文件放到项目的目录中
二、配置SSL
1、编辑application.properties这个文件
package com.example;
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.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SpringBootHttpsApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootHttpsApplication.class, args);
}
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint constraint = new SecurityConstraint();
constraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
constraint.addCollection(collection);
context.addConstraint(constraint);
}
};
tomcat.addAdditionalTomcatConnectors(httpConnector());
return tomcat;
}
@Bean
public Connector httpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
//Connector监听的http的端口号
connector.setPort(8080);
connector.setSecure(false);
//监听到http的端口号后转向到的https的端口号
connector.setRedirectPort(8443);
return connector;
}
}
三、测试使用
1、查看启动信息
2、访问地址localhost:8080/AmazeUI-2.7.2/login.html 我自定义了一个html网页,它已经转向到了8443端口
3、浏览器的地址栏中显示不安全:因为这个证书是不收信任的,传统一般都企业都是需要购买此证书的
来源:https://www.cnblogs.com/happy1109/p/8444655.html
标签:springboot,http,https
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java的NIO管道用法代码分享
2022-05-01 23:52:53
![](https://img.aspxhome.com/file/2023/7/105917_0s.jpg)
Java解除文件占用即Dom4j操作后实现xml关流
2022-07-05 19:18:35
简单学习Java抽象类要点及实例
2021-10-11 09:08:12
C# 创建高精度定时器的示例
2023-03-09 12:35:58
![](https://img.aspxhome.com/file/2023/4/76434_0s.png)
SpringBoot整合Web开发之Json数据返回的实现
2023-04-27 05:06:51
![](https://img.aspxhome.com/file/2023/9/61639_0s.png)
C#中调用VB中Inputbox类的实现方法
2023-09-06 18:12:57
Java实现RSA加密工具类
2023-08-23 08:41:02
C#简单实现显示中文格式星期几的方法
2021-09-08 12:27:05
C#图片查看器实现方法
2021-06-17 16:50:03
![](https://img.aspxhome.com/file/2023/1/72491_0s.gif)
Java Callable接口实现细节详解
2023-11-10 05:34:26
![](https://img.aspxhome.com/file/2023/5/58995_0s.png)
从java中调用matlab详细介绍
2023-08-01 14:04:17
![](https://img.aspxhome.com/file/2023/0/59870_0s.jpg)
Intellij IDEA中启动多个微服务(开启Run Dashboard管理)
2022-01-11 02:25:00
![](https://img.aspxhome.com/file/2023/8/62708_0s.png)
java中对list分页并显示数据到页面实例代码
2023-05-22 00:39:10
c# 预处理识别硬币的数据集
2022-05-21 17:26:09
Android 单双击实现的方法步骤
2023-04-19 02:19:31
Android带进度的圆形进度条
2023-08-05 21:47:18
![](https://img.aspxhome.com/file/2023/8/86748_0s.jpg)
c#爬虫爬取京东的商品信息
2022-12-03 14:38:11
Java中使用StackWalker和Stream API进行堆栈遍历
2023-04-12 11:29:07
Java对象不使用时赋值null的意义详解
2023-11-25 01:46:20
![](https://img.aspxhome.com/file/2023/4/59884_0s.png)
java不同版本在多线程中使用随机数生成器的实现
2022-01-02 22:57:07