Springboot以Tomcat为容器实现http重定向到https的两种方式
1 简介
本文将介绍在Springboot
中如何通过代码实现Http
到Https
的重定向,本文仅讲解Tomcat
作为容器的情况,其它容器将在以后一一道来。
建议阅读之前的相关文章:
(2)HTTPS之密钥知识与密钥工具Keytool和Keystore-Explorer
2 相关概念
2.1 什么叫重定向
所谓重定向,就是本来你想浏览地址A的,但是到达服务端后,服务端认为地址A的界面不在了或者你没权限访问等原因,不想你访问地址A;就告诉你另一个地址B,然后你再去访问地址B。
对于重定向一般有两个返回码:
- 301:永久性重定向;
- 302:暂时性重定向。
通过Chrome
查看网络详情,记录了几个网站的重定向情况:
网站 | 域名 | 重定向代码 | 重定向后的网址 |
---|---|---|---|
南瓜慢说 | www.pkslow.com | 301 | https://www.pkslow.com |
www.google.com | 307 | https://www.google.com | |
Apple | www.apple.com | 307 | https://www.apple.com |
支付宝 | www.alipay.com | 301 | https://www.alipay.com |
www.qq.com | 302 | https://www.qq.com | |
百度 | www.baidu.com | 307 | https://www.baidu.com |
注:307也是重定向的一种,是新的状态码。
2.2 为什么要重定向
结合我上面特意列的表格,是不是大概想到了为何要做这种重定向?不难发现上面的重定向都在做一件事,就是把http
重定向为https
。原因如下:
(1)http
是不安全的,应该使用安全的https
网址;
(2)但不能要求用户每次输入网站都输入https:// 吧,这也太麻烦了,所以大家都是习惯于只输入域名,甚至连www. 都不愿意输入。因此,用户的输入其实都是访问http
的网页,就需要重定向到https
以达到安全访问的要求。
2.3 如何做到重定向
首先,服务器必须要同时支持http
和https
,不然也就没有重定向一说了。因为https
是必须提供支持的,那为何还要提供http
的服务呢?直接访问https
不就行了,不是多此一举吗?原因之前已经讲过了,大家是习惯于只输入简单域名访问的,这时到达的就是http
,如果不提供http
的支持,用户还以为你的网站已经挂了呢。
两种协议都提供支持,所以是需要打开两个Socket
端口的,一般http
为80
,而https
为443
。然后就需要把所有访问http
的请求,重定向到https
即可。不同的服务器有不同的实现,现在介绍Springboot+Tomcat
的实现。
3 Springboot Tomcat实现重定向
Springboot
以Tomcat
作为Servlet
容器时,有两种方式可以实现重定向,一种是没有使用Spring Security
的,另一种是使用了Spring Security
的。代码结构如下:
主类的代码如下:
package com.pkslow.ssl;
import com.pkslow.ssl.config.containerfactory.HttpToHttpsContainerFactoryConfig;
import com.pkslow.ssl.config.security.EnableHttpWithHttpsConfig;
import com.pkslow.ssl.config.security.HttpToHttpsWebSecurityConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
@SpringBootApplication
@Import({EnableHttpWithHttpsConfig.class, HttpToHttpsWebSecurityConfig.class})
//@Import(HttpToHttpsContainerFactoryConfig.class)
@ComponentScan(basePackages = "com.pkslow.ssl.controller")
public class SpringbootSslApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootSslApplication.class, args);
}
}
@ComponentScan(basePackages = "com.pkslow.ssl.controller")
:没有把config
包扫描进来,是因为想通过@Import
来控制使用哪种方式来进行重定向。当然还可以使用其它方式来控制,如@ConditionalOnProperty
,这里就不展开讲了。
当没有使用Spring Security
时,使用@Import(HttpToHttpsContainerFactoryConfig.class)
;
当使用Spring Security
时,使用@Import({EnableHttpWithHttpsConfig.class, HttpToHttpsWebSecurityConfig.class})
。
配置文件application.properties内容如下:
server.port=443
http.port=80
server.ssl.enabled=true
server.ssl.key-store-type=jks
server.ssl.key-store=classpath:localhost.jks
server.ssl.key-store-password=changeit
server.ssl.key-alias=localhost
需要指定两个端口,server.port
为https
端口;http.port
为http
端口。注意在没有https
的情况下,server.port
指的是http
端口。
3.1 配置Container Factory实现重定向
配置的类为HttpToHttpsContainerFactoryConfig
,代码如下:
package com.pkslow.ssl.config.containerfactory;
import org.apache.catalina.Context;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HttpToHttpsContainerFactoryConfig {
@Value("${server.port}")
private int httpsPort;
@Value("${http.port}")
private int httpPort;
@Bean
public TomcatServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat =
new TomcatServletWebServerFactory() {
@Override
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(createHttpConnector());
return tomcat;
}
private Connector createHttpConnector() {
Connector connector =
new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setScheme("http");
connector.setSecure(false);
connector.setPort(httpPort);
connector.setRedirectPort(httpsPort);
return connector;
}
}
createHttpConnector()
:这个方法主要是实现了在有https
前提下,打开http
的功能,并配置重定向的https
的端口。
3.2 配置Spring security实现重定向
有两个配置类,一个为打开http
服务,一个为实现重定向。
EnableHttpWithHttpsConfig
主要作用是在已经有https
的前提下,还要打开http
服务。
package com.pkslow.ssl.config.security;
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Configuration
public class EnableHttpWithHttpsConfig {
@Value("${http.port}")
private int httpPort;
@Component
public class CustomContainer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
@Override
public void customize(TomcatServletWebServerFactory factory) {
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setPort(httpPort);
connector.setScheme("http");
connector.setSecure(false);
factory.addAdditionalTomcatConnectors(connector);
}
}
}
HttpToHttpsWebSecurityConfig
主要是针对Spring Security
的配置,众所周知,Spring Security
是功能十分强大,但又很复杂的。代码中已经写了关键的注释:
package com.pkslow.ssl.config.security;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class HttpToHttpsWebSecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${server.port}")
private int httpsPort;
@Value("${http.port}")
private int httpPort;
@Override
protected void configure(HttpSecurity http) throws Exception {
//redirect to https - 用spring security实现
http.portMapper().http(httpPort).mapsTo(httpsPort);
http.requiresChannel(
channel -> channel.anyRequest().requiresSecure()
);
//访问路径/hello不用登陆获得权限
http.authorizeRequests()
.antMatchers("/hello").permitAll()
.anyRequest().authenticated().and();
}
@Override
public void configure(WebSecurity web) throws Exception {
//过滤了actuator后,不会重定向,也不用权限校验,这个功能非常有用
web.ignoring()
.antMatchers("/actuator")
.antMatchers("/actuator/**");
}
}
4 总结
最后实现了重定向,结果展示:
本文详细代码可在南瓜慢说公众号回复<SpringbootSSLRedirectTomcat>获取。
参考链接:
Spring Security: https://docs.spring.io/spring-security/site/docs/5.3.2.BUILD-SNAPSHOT/reference/html5/#servlet-http-redirect
Springboot 1.4重定向:https://jonaspfeifer.de/redirect-http-https-spring-boot/
欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章!
欢迎关注微信公众号<南瓜慢说>,将持续为你更新...
Springboot以Tomcat为容器实现http重定向到https的两种方式的更多相关文章
- nginx开启ssl并把http重定向到https的两种方式
1 简介 Nginx是一个非常强大和流行的高性能Web服务器.本文讲解Nginx如何整合https并将http重定向到https. https相关文章如下: (1)Springboot整合https原 ...
- Springboot以Jetty为容器实现http重定向到https
1 简介 之前讲解的Springboot整合https用的是tomcat作为容器,tomcat也是一个流行多年的老牌Java容器了.但针对不同的场景,还是会有不同的选择,如Jetty.Jetty是架构 ...
- Springboot 创建的maven获取resource资源下的文件的两种方式
Springboot 创建的maven项目 打包后获取resource下的资源文件的两种方式: 资源目录: resources/config/wordFileXml/wordFileRecord.xm ...
- SpringBoot定时任务 - 集成quartz实现定时任务(单实例和分布式两种方式)
最为常用定时任务框架是Quartz,并且Spring也集成了Quartz的框架,Quartz不仅支持单实例方式还支持分布式方式.本文主要介绍Quartz,基础的Quartz的集成案例本,以及实现基于数 ...
- 在CentOS下的docker容器中部署spring boot应用的两种方式
我们通常在 windows 环境下开发 Java,而通常是部署在Linux的服务器中,而CentOS通常是大多数企业的首选,基于Docker的虚拟化容器技术,多数Java应用选择这种方式部署服务.本文 ...
- SpringBoot集成Mybatis实现多表查询的两种方式(基于xml)
下面将在用户和账户进行一对一查询的基础上进行介绍SpringBoot集成Mybatis实现多表查询的基于xml的两种方式. 首先我们先创建两个数据库表,分别是user用户表和account账户表 ...
- 引入springboot的两种方式以及springboot容器的引入
一.在项目中引入springboot有两种方式: 1.引入spring-boot-starter-parent 要覆盖parent自带的jar的版本号有两种方式: (1)在pom中重新引入这个jar, ...
- springboot 注册服务注册中心(zk)的两种方式
在使用springboot进行开发的过程中,我们经常需要处理这样的场景:在服务启动的时候,需要向服务注册中心(例如zk)注册服务状态,以便当服务状态改变的时候,可以故障摘除和负载均衡. 我遇到过两种注 ...
- springboot打成jar包和war包的两种方式,并引入第三方jar包!
springboot打成jar包和war包的两种方式,并引入第三方jar包! 首先把需要引入的第三方jar包引入到本地项目中,在引用的模块下加一个lib文件夹 一.打成jar包 1.修改pom文件里的 ...
随机推荐
- Spring-Cloud-Netflix-Eureka注册中心
TOC 概述 eureka是Netflix的子模块之一,也是一个核心的模块 eureka里有2个组件: 一个是EurekaServer(一个独立的项目) 这个是用于定位服务以实现中间层服务器的负载平衡 ...
- [斯坦福大学2014机器学习教程笔记]第五章-控制语句:for,while,if语句
在本节中,我们将学习如何为Octave程序写控制语句. 首先,我们先学习如何使用for循环.我们将v设为一个10行1列的零向量. 接着,我们写一个for循环,让i等于1到10.写出来就是for i = ...
- 玩转控件:封装Dev的SearchLookupEdit
鸣谢 随着前面几个章节对控件封装与扩展的分享,不少小伙伴儿们在作者公众号上反馈,并联系作者,表示通过这些系列和源码能学到不少细节上的东西,并运用到了自己的实际项目当中,也有不少伙伴儿反馈更好更优的处理 ...
- 牛客寒假基础集训营 | Day1 G-eli和字符串
G-eli和字符串 题目描述 eli拿到了一个仅由小写字母组成的字符串. 她想截取一段连续子串,这个子串包含至少 kkkkkkkkk 个相同的某个字母. 她想知道,子串的长度最小值是多少? 注:所谓连 ...
- 浅谈Java接口(Interface)
浅谈Java接口 先不谈接口,不妨设想一个问题? 如果你写了个Animal类,有许多类继承了他,包括Hippo(河马), Dog, Wolf, Cat, Tiger这几个类.你把这几个类拿给别人用,但 ...
- CSS 基础(二)
本节内容: 文本 字体 链接 列表 表格 一.文本 文本颜色 颜色属性被用来设置文字的颜色. 三种方式: 十六进制值 - 如: #FF0000 一个RGB值 - 如: RGB(255,0,0) 颜色的 ...
- 【前端】CSS总结
======================== CSS层叠样式表======================== 命名规则:使用字母.数字或下划线和减号构成,不要以数字开头 一.css的语法-- ...
- Lua 5.3 -- SOL2.0 用户指南 【1】
SOL2.2 是一个快速.简单的C++与LUA的绑定器.如果确定要在你的程序里面同时运行Lua和C++,SOL 是一个高性能的绑定器,是一个API使用方便的 GO-TO 框架. 简单看一下特点:这个链 ...
- 使用SVG内置API计算图形或点经过transform之后的新坐标
一个应用场景是,点击一条路径,显示该路径的控制点.因为有transform变形( 平移.缩放.倾斜.旋转等变换),所以获取变形后的新坐标需要计算. 纯数学的方法,就是用2D变换矩阵的一些公式去运算,过 ...
- python编程语言是什么?它能做什么?
Python是一种全栈的开发语言,你如果能学好Python,前端,后端,测试,大数据分析,爬虫等这些工作你都能胜任. 当下Python有多火我不再赘述,,Python有哪些作用呢? 就目前Python ...