参考文章:
http原理
Http与Https的区别:
- HTTP 的URL 以http:// 开头,而HTTPS 的URL 以https:// 开头
- HTTP 无法加密,是不安全的,而 HTTPS 对传输的数据进行加密,是安全的
- HTTP 标准端口是80 ,而 HTTPS 的标准端口是443
- 在OSI 网络模型中,HTTP工作于应用层,而HTTPS 的安全传输机制工作在传输层
- HTTP无需证书,而HTTPS 需要CA机构wosign的颁发的SSL证书
URI、URL和URN的区别
URI(Uniform Resource Identifier ):统一资源标识符,就是在某一规则下能把一个资源独一无二地标识出来。
URL(Uniform Resource Locator):统一资源定位符
URN(Uniform Resource Name):统一资源名称。
人有身份证和住址。住址可以理解为 URL,身份证理解了URN,他们都市URI。
https提交数据的时候为什么还是明文的?
提交的时候还没有加密,加密是在传输层加密的,你想看加密后的,应该用Wireshark等抓包。
Spirng Boot之HTTPS配置
- 生成或者获取ssl证书
- 在Spring Boot里配置开启HTTPS
- HTTP自动重定向到HTTPS
1. 生成或者获取ssl证书
获取SSL证书主要有两种,
- 自己通过工具自动生成,请参考Spring Boot之HTTPS配置
- 通过SSL证书服务商获取教你启用HTTPS,让你的网站也能通过https访问
2.在Spring Boot里配置开记HTTPS
# 告诉Spring Security 请求也需要透过HTTPS, 签名文件;
security.require-ssl=true
# 指定密钥仓库类型
server.ssl.key-store-type:PKCS12
# 指定密钥仓库路径
server.ssl.key-store=classpath:keystore.p12
# 指定密钥签名密码
server.ssl.key-store-password=password
# 指定密钥别名
server.ssl.key-alias=tomcat
上面的密钥信息在生成的时候已经指定了的。在这里复制过来就行。
3. HTTP自动重定向到HTTPS
配置很简单,在入口类中添加相应的重定向Bean就行了,如下:
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 ConnectorConfig {
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
@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(getHttpConnector());
return tomcat;
}
private Connector getHttpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(8443);
return connector;
}
}
这个时候当我们访问http://localhost:8080/SpringBootBase的时候系统会自动重定向到https://localhost:8443/SpringBootBase这个地址上。这里的Connector实际就是server.xml中配置的Tomcat的Connector节点。