Https系列之三:让服务器同时支持http、https,基于spring boot
来源:互联网 发布:php教程视频教程全集 编辑:程序博客网 时间:2024/05/17 21:54
假设我们的网站名为: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;@SpringBootApplicationpublic 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容器中
@Beanpublic EmbeddedServletContainerFactory servletContainer()
然后,用下面的代码拦截所有的/*请求
@Overrideprotected void postProcessContext(Context context) {.......... constraint.setUserConstraint("CONFIDENTIAL"); collection.addPattern("/*");.............}
并把其关联到下面的httpConnector中
@Beanpublic Connector httpConnector()
最后,在public Connector httpConnector()中,
把http设为默认的80端口,并把http的请求跳转到443的https端口
其中443是https的默认端口,也可以设为其它的值,但要和resources/application.properties的内容对应
如下:
server.port=443server.ssl.key-store=classpath:keystore.p12server.ssl.key-store-password=123456server.ssl.keyStoreType=PKCS12server.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
要实现此要求,其实很简单,只需要把:
@Beanpublic Connector httpConnector() {........connector.setSecure(false);...........
改为
@Beanpublic Connector httpConnector() {........connector.setSecure(true);...........
- Https系列之三:让服务器同时支持http、https,基于spring boot
- Https系列之三:让服务器同时支持http、https,基于spring boot
- 让spring boot支持https和http
- Spring Boot工程支持HTTP和HTTPS
- Spring Boot支持tomcat服务器,支持http、https(ssl、tls)双协议,支持双端口
- Spring Boot支持Jetty服务器,支持http、https(ssl、tls)双协议,支持双端口
- Spring Boot支持Undertow服务器,支持http、https(ssl、tls)双协议,支持双端口
- Https系列之二:https的SSL证书在服务器端的部署,基于tomcat,spring boot
- 让CentOS服务器支持https(安全http协议)
- 让CentOS服务器支持https(安全http协议)
- 让CentOS服务器支持https(安全http协议)
- tomcat同时支持http和https访问
- 让apache服务器可以保证http和https同时访问(解决https可以访问而http不能访问)
- 让apache服务器可以保证http和https同时访问(解决https可以访问而http不能访问)
- 使用C#开发HTTP服务器之支持HTTPS
- spring boot设置http https端口
- spring-boot 添加http自动转向https
- 如何让boa网页服务器支持HTTPS
- 深入理解计算机系统(3.4)------算术和逻辑操作
- cmd命令行查看configServer中的配置文件
- ArrayList动态数组
- 批处理文件 Android Studio 连接夜神模拟器
- 为ASP.NetCore程序启用SSL
- Https系列之三:让服务器同时支持http、https,基于spring boot
- Java学习笔记(38)--包装类
- session
- Intent的用法(1)
- (SSL证书)apache添加https
- 八大排序算法代码C
- 再起航,我的学习笔记之JavaScript设计模式26(解释器模式)
- C++11获取线程的返回值
- nutch和solr做爬虫