HTTP面试题

参考文章:

一文带你了解HTTPS

看懂HTTPS

URI、URL和URN的区别

Spirng Boot之HTTPS配置

你启用HTTPS,让你的网站也能通过https访问

http原理

在这里插入图片描述

Http与Https的区别:

  1. HTTP 的URL 以http:// 开头,而HTTPS 的URL 以https:// 开头
  2. HTTP 无法加密,是不安全的,而 HTTPS 对传输的数据进行加密,是安全的
  3. HTTP 标准端口是80 ,而 HTTPS 的标准端口是443
  4. 在OSI 网络模型中,HTTP工作于应用层,而HTTPS 的安全传输机制工作在传输层
  5. 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配置

  1. 生成或者获取ssl证书
  2. 在Spring Boot里配置开启HTTPS
  3. HTTP自动重定向到HTTPS
1. 生成或者获取ssl证书

获取SSL证书主要有两种,

  1. 自己通过工具自动生成,请参考Spring Boot之HTTPS配置
  2. 通过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节点。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值