Https系列之三:让服务器同时支持http、https,基于spring boot
Https系列会在下面几篇文章中分别作介绍:
一:https的简单介绍及SSL证书的生成
二:https的SSL证书在服务器端的部署,基于tomcat,spring boot
三:让服务器同时支持http、https,基于spring boot
四:https的SSL证书在Android端基于okhttp,Retrofit的使用
所有文章会优先在:
微信公众号“颜家大少”中发布
转载请标明出处
前面已介绍了:”https在服务器端的部署,基于tomcat,spring boot“
但我们会发现一个问题,只能用https登录我们的网站,而不能用http
假设我们的网站名为:www.my.com
如果是之前的http,我们只需在浏览器中输入:my.com
浏览器就会自动登录到:http:// www.my.com
但部署了https后,发现在浏览器中输入:my.com,返回的结果是:无法访问此网站
这对用户来说,体验是非常不好的。
好吧,那我们试试一些比较有名的网站,如阿里云。
在浏览器中输入:aliyun.com
就能自动跳转到:https: //www.aliyun.com
那我们能不能在部署了https后,在输入:my.com
自动跳转到https对应的: https:// www.my.com
或
依然跳转到:http:// www.my.com ?
答案是,上面两种方法都可以的,任君选择
下面介绍的就是以上要求基于spring boot的实现
直接上代码:
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.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
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 ServerMain implements CommandLineRunner{
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
@Override
protected void postProcessContext(Context context) {
//Due to CONFIDENTIAL and /*, this will cause Tomcat to redirect every request to HTTPS.
//You can configure multiple patterns and multiple constraints if you need more control over what is and is not redirected.
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");
//Set the scheme that will be assigned to requests received through this connector
//@param scheme The new scheme
connector.setScheme("http");
//Set the port number on which we listen for requests.
// @param port The new port number
connector.setPort(80);
//Set the secure connection flag that will be assigned to requests received through this connector.
//@param secure The new secure connection flag
//if connector.setSecure(true),the http use the http and https use the https;else if connector.setSecure(false),the http redirect to https;
connector.setSecure(false);
//redirectPort The redirect port number (non-SSL to SSL)
connector.setRedirectPort(443);
return connector;
}
public static void main(String[] args) throws Exception {
SpringApplication.run(ServerMain.class, args);
}
@Override
public void run(String... arg0) throws Exception {
// TODO Auto-generated method stub
}
}
其中,下面代码的作用是把此EmbeddedServletContainerFactory 注入到web容器中
@Bean
public EmbeddedServletContainerFactory servletContainer()
然后,用下面的代码拦截所有的/*请求
@Override
protected void postProcessContext(Context context) {
..........
constraint.setUserConstraint("CONFIDENTIAL");
collection.addPattern("/*");
.............
}
并把其关联到下面的httpConnector中
@Bean
public Connector httpConnector()
最后,在public Connector httpConnector()中,
把http设为默认的80端口,并把http的请求跳转到443的https端口
其中443是https的默认端口,也可以设为其它的值,但要和resources/application.properties的内容对应
如下:
server.port=443
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=123456
server.ssl.keyStoreType=PKCS12
server.ssl.keyAlias:tomcat
运行服务器,会看到打印如下:
其中会看到TomcatEmbeddedServletContainer,和同时开启的两个端口:443 (https) 80 (http)
TomcatEmbeddedServletContainer : Tomcat started on port(s): 443 (https) 80 (http)
Ok,那现在试试输入:my.com,就会发现浏览器会直接跳到:https:// www.my.com了
到此,这件事情就算是大功告成了。
但此时有同学可能会提出特殊的要求:
他的https只是为了某某的要求而使用的,比如说要接入什么什么的一定要填的是https的地址
而他的网站根本就不需要https这种安全级别的,另外,他觉得http的访问速度可能会快点,你知到有些同学是有这种洁癖的 :p
也就是说:
输入:my.com,跳到: http:// www.my.com
输入:https:// www.my.com,跳到:https:// www.my.com
要实现此要求,其实很简单,只需要把:
@Bean
public Connector httpConnector() {
........
connector.setSecure(false);
...........
改为
@Bean
public Connector httpConnector() {
........
connector.setSecure(true);
...........
就大功告成了
更多内容请看:Https系列之四:https的SSL证书在Android端基于okhttp,Retrofit的使用
更多内容,请关注微信公众号:颜家大少
Https系列之三:让服务器同时支持http、https,基于spring boot的更多相关文章
- Set up HTTP/2 server with Spring Boot 【基于Spring boot搭建http2.0服务器】
1. Server side With spring boot, we can set up a http server easily. Restcontroller make it easier t ...
- (19)Spring Boot 添加JSP支持【从零开始学Spring Boot】
[来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论: 您的认可是我最大的动力,感谢您的支持] 看完本文章您可能会有些疑问,可以查看之后的一篇博客: 81. Spring Boot集成JSP疑问[从 ...
- Spring Boot配置篇(基于Spring Boot 2.0系列)
1:概述 SpringBoot支持外部化配置,配置文件格式如下所示: properties files yaml files environment variables command-line ar ...
- 19. Spring Boot 添加JSP支持【从零开始学Spring Boot】
转:http://blog.csdn.net/linxingliang/article/details/52017140 这个部分比较复杂,所以单独创建一个工程来进行讲解: 大体步骤: (1) ...
- Spring Boot入门篇(基于Spring Boot 2.0系列)
1:概述: Spring Boot是用来简化Spring应用的初始化开发过程. 2:特性: 创建独立的应用(jar|war形式); 需要用到spring-boot-maven-plugin插件 直接嵌 ...
- Https系列之二:https的SSL证书在服务器端的部署,基于tomcat,spring boot
Https系列会在下面几篇文章中分别作介绍: 一:https的简单介绍及SSL证书的生成二:https的SSL证书在服务器端的部署,基于tomcat,spring boot三:让服务器同时支持http ...
- Https系列之一:https的简单介绍及SSL证书的生成
Https系列会在下面几篇文章中分别作介绍: 一:https的简单介绍及SSL证书的生成二:https的SSL证书在服务器端的部署,基于tomcat,spring boot三:让服务器同时支持http ...
- Https系列之四:https的SSL证书在Android端基于okhttp,Retrofit的使用
Https系列会在下面几篇文章中分别作介绍: 一:https的简单介绍及SSL证书的生成二:https的SSL证书在服务器端的部署,基于tomcat,spring boot三:让服务器同时支持http ...
- Spring Boot 支持 Https 有那么难吗?
https 现在已经越来越普及了,特别是做一些小程序或者公众号开发的时候,https 基本上都是刚需了. 不过一个 https 证书还是挺费钱的,个人开发者可以在各个云服务提供商那里申请一个免费的证书 ...
随机推荐
- 使用SAS和JavaScript前端上传Azure Bolb大文件
问题描述: Azure Storage Rest API提供了对于大文件分块上传方法,分别使用Put Block和Put Block List实现相关功能 参考链接: Uploading Large ...
- 迈向angularjs2系列(8):angular cli和angular2种子项目
文章目录 1.angular cli快速搭建项目 2.angular2-seed 3.手动配置 题外话:如何更好的阅读本篇文章 一: angular cli的安装 Angular-cli(命令行界面, ...
- redis集群搭建及注意事项
上一篇:redis的安装及注意事项 这里,在一个Linux虚拟机上搭建6个节点的redis伪集群,思路很简单,一台虚拟机上开启6个redis实例,每个redis实例有自己的端口.这样的话,相当于模拟出 ...
- MyBatis框架知识整理
MyBatis框架 一.介绍: MyBatis实际上是Ibatis3.0版本以后的持久化层框架[也就是和数据库打交道的框架]! 和数据库打交道的技术有: 原生的JDBC技术---> Spring ...
- Oracle03——游标、异常、存储过程、存储函数、触发器和Java代码访问Oracle对象
作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7476717.html 1.游标(光标)Cursor 在写java程序中有集合的概念,那么 ...
- macaca测试web小例子
上午刚把macaca的环境在公司的电脑上吧web 端的环境给搭建好,于是乎,看看网上的例子,看看官方的文档 https://macacajs.github.io/wd.py/ 可以在这个链接看到原滋原 ...
- 大数据平台搭建-hadoop/hbase集群的搭建
版本要求 java 版本:1.8.*(1.8.0_60) 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downl ...
- python学习总结(函数进阶)
-------------------程序运行原理------------------- 1.模块的内建__name__属性,主模块其值为__main__,导入模块其值为模块名 1.创建时间, ...
- 程序员也有春天之HTTP/2.0配置
哎呀,一不小心自己的博客也是HTTP/2.0了,前段时间对网站进行了https迁移并上了CDN,最终的结果是这酱紫的(重点小绿锁,安全标示以及HTTP/2.0请求). 科普 随着互联网的快速发展,HT ...
- CPU和GPU的差别
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt317 首先需要解释CPU和GPU这两个缩写分别代表什么.CPU即中央处理器, ...