视频直播支持HTTPS安全加速、强制跳转,本文介绍安全加速的原理、优势、使用方法和注意事项。
背景
HTTP协议以明文方式发送内容,不提供数据加密,因此在传输过程中很容易被截获和理解。
功能简介
视频直播支持配置HTTPS安全加速以及强制跳转,使用强制跳转需要在已经完成HTTPS证书配置的前提下。
HTTPS:
安全超文本传输协议(Hyper Text Transfer Protocol over Secure Socket Layer,简称 HTTPS),是以安全为目标的HTTP通道。简单来说,HTTP协议以明文方式发送内容,而HTTPS是HTTP的安全版,将HTTP用SSL/TLS协议进行封装,HTTPS的安全基础是SSL/TLS。
HTTPS提供了身份验证与加密通讯方法,被广泛用于万维网上安全敏感的通讯,根据2017年EFF(Electronic Frontier Foundation)发布的报告,目前全球已有超过一半的网页端流量采用了加密的HTTPS进行传输。
强制跳转:
将客户端至边缘节点的原请求方式强制重定向为HTTP或者HTTPS。
如果您的加速域名开启了HTTPS安全加速,您可以自定义设置,将终端用户的原请求方式进行强制跳转。例如,您开启HTTP -> HTTPS后,终端用户发起了一个HTTP请求,服务端返回301重定向响应,原来的HTTP请求强制重定向为HTTPS请求,如下图所示。
功能原理
在阿里云视频直播控制台开启的HTTPS协议,将实现客户端和阿里云视频直播节点之间请求的HTTPS加密。视频直播节点返回从源站获取的资源给客户端时,按照源站的配置方式进行。建议源站配置并开启HTTPS,实现全链路的HTTPS加密。
HTTPS加密流程如下图所示。
客户端发起HTTPS请求。
服务端生成公钥和私钥(可以自己制作,也可以向专业组织申请)。
服务端把相应的公钥证书传送给客户端。
客户端解析证书的正确性。
如果证书正确,则会生成一个随机数(密钥),并用公钥对随机数进行加密,传输给服务端。
如果证书不正确,则SSL握手失败。
说明正确性包括:
证书未过期;
发行服务器证书的CA可靠;
发行者证书的公钥能够正确解开服务器证书的发行者的数字签名;
服务器证书上的域名和服务器的实际域名相匹配。
服务端用之前的私钥进行解密,得到随机数(密钥)。
服务端用密钥对传输的数据进行加密。
客户端用密钥对服务端的加密数据进行解密,拿到相应的数据。
功能优势
传输安全:HTTPS安全传输,有效防止HTTP明文传输中的窃听、篡改、冒充和劫持风险。
信息加密:数据传输过程中对关键信息进行加密,防止Session ID或Cookie内容被捕获造成的敏感信息泄露。
数据完整:数据传输过程中进行完整性校验,防止DNS或内容遭第三方劫持、篡改等中间人攻击(MITM)。
主流趋势:主流浏览器将HTTP标识为不安全,影响用户体验和访问安全。HTTPS是主流趋势,支持HTTP/2,提升搜索权重和用户体验。
强烈建议您将访问协议升级到HTTPS,以提高安全性和用户体验。
使用方法
HTTPS安全加速
您需要配置HTTPS证书,才能通过HTTPS方式访问资源,实现HTTPS安全加速。关于证书,请参见证书格式说明。
您可以通过控制台和API的方式配置HTTPS安全加速。
通过控制台操作,请参见配置HTTPS安全加速。
通过调用APISetLiveDomainCertificate设置域名证书。
Java SDK设置域名证书的示例代码如下:
// This file is auto-generated, don't edit it. Thanks.
package demo;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.core.http.HttpClient;
import com.aliyun.core.http.HttpMethod;
import com.aliyun.core.http.ProxyOptions;
import com.aliyun.httpcomponent.httpclient.ApacheAsyncHttpClientBuilder;
import com.aliyun.sdk.service.live20161101.models.*;
import com.aliyun.sdk.service.live20161101.*;
import com.google.gson.Gson;
import darabonba.core.RequestConfiguration;
import darabonba.core.client.ClientOverrideConfiguration;
import darabonba.core.utils.CommonUtil;
import darabonba.core.TeaPair;
//import javax.net.ssl.KeyManager;
//import javax.net.ssl.X509TrustManager;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.io.*;
public class SetLiveDomainCertificate {
public static void main(String[] args) throws Exception {
// HttpClient Configuration
/*HttpClient httpClient = new ApacheAsyncHttpClientBuilder()
.connectionTimeout(Duration.ofSeconds(10)) // Set the connection timeout time, the default is 10 seconds
.responseTimeout(Duration.ofSeconds(10)) // Set the response timeout time, the default is 20 seconds
.maxConnections(128) // Set the connection pool size
.maxIdleTimeOut(Duration.ofSeconds(50)) // Set the connection pool timeout, the default is 30 seconds
// Configure the proxy
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<YOUR-PROXY-HOSTNAME>", 9001))
.setCredentials("<YOUR-PROXY-USERNAME>", "<YOUR-PROXY-PASSWORD>"))
// If it is an https connection, you need to configure the certificate, or ignore the certificate(.ignoreSSL(true))
.x509TrustManagers(new X509TrustManager[]{})
.keyManagers(new KeyManager[]{})
.ignoreSSL(false)
.build();*/
// Configure Credentials authentication information, including ak, secret, token
StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
.accessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
.accessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
//.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
.build());
// Configure the Client
AsyncClient client = AsyncClient.builder()
.region("<Your RegionId>") // Region ID
//.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
.credentialsProvider(provider)
//.serviceConfiguration(Configuration.create()) // Service-level configuration
// Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
.overrideConfiguration(
ClientOverrideConfiguration.create()
// Endpoint 请参考 https://api.aliyun.com/product/live
.setEndpointOverride("live.aliyuncs.com")
//.setConnectTimeout(Duration.ofSeconds(30))
)
.build();
// Parameter settings for API request
SetLiveDomainCertificateRequest setLiveDomainCertificateRequest = SetLiveDomainCertificateRequest.builder()
.domainName("<Your DomainName>")
.certName("<Your CertName>")
.certType("<Your CertType>")
.SSLProtocol("<Your SSLProtocol>")
.SSLPub("<Your SSLPub>")
.SSLPri("<Your SSLPri>")
.forceSet("<Your ForceSet>")
// Request-level configuration rewrite, can set Http request parameters, etc.
// .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
.build();
// Asynchronously get the return value of the API request
CompletableFuture<SetLiveDomainCertificateResponse> response = client.setLiveDomainCertificate(setLiveDomainCertificateRequest);
// Synchronously get the return value of the API request
SetLiveDomainCertificateResponse resp = response.get();
System.out.println(new Gson().toJson(resp));
// Asynchronous processing of return values
/*response.thenAccept(resp -> {
System.out.println(new Gson().toJson(resp));
}).exceptionally(throwable -> { // Handling exceptions
System.out.println(throwable.getMessage());
return null;
});*/
// Finally, close the client
client.close();
}
}
以上示例代码表示对域名<DomainName>配置HTTPS证书,证书名称为<CertName>,证书类型为<Upload>,公钥内容为<-----BEGIN CERTIFICATE***>,私钥内容为<-----BEGIN RSA PRIVATE KEY*****>
公钥私钥内容只支持PEM格式。
配置成功后,证书内容会保存至云盾证书控制台,可以在证书管理-》SSL证书管理-》上传证书内查看。
使用JAVA SDK,具体请参见Java SDK使用说明。
相关API:
接口 | 描述 | 参考文档 |
SetLiveDomainCertificate | 设置某域名下证书功能是否启用及修改证书信息。 | |
DescribeLiveCertificateList | 获取证书列表信息。 | |
DescribeLiveCertificateDetail | 获取证书详细信息。 | |
BatchDeleteLiveDomainConfigs | 批量删除域名配置。 |
强制跳转
配置强制跳转之前,请确保已成功配置HTTPS证书。
您可以通过控制台和API的方式配置强制跳转。
通过控制台操作,请参见配置强制跳转。
通过调用APIBatchSetLiveDomainConfigs进行强制跳转设置。
Java SDK设置HTTPS强制跳转的示例代码如下:
// This file is auto-generated, don't edit it. Thanks.
package demo;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.core.http.HttpClient;
import com.aliyun.core.http.HttpMethod;
import com.aliyun.core.http.ProxyOptions;
import com.aliyun.httpcomponent.httpclient.ApacheAsyncHttpClientBuilder;
import com.aliyun.sdk.service.live20161101.models.*;
import com.aliyun.sdk.service.live20161101.*;
import com.google.gson.Gson;
import darabonba.core.RequestConfiguration;
import darabonba.core.client.ClientOverrideConfiguration;
import darabonba.core.utils.CommonUtil;
import darabonba.core.TeaPair;
//import javax.net.ssl.KeyManager;
//import javax.net.ssl.X509TrustManager;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.io.*;
public class BatchSetLiveDomainConfigs {
public static void main(String[] args) throws Exception {
// HttpClient Configuration
/*HttpClient httpClient = new ApacheAsyncHttpClientBuilder()
.connectionTimeout(Duration.ofSeconds(10)) // Set the connection timeout time, the default is 10 seconds
.responseTimeout(Duration.ofSeconds(10)) // Set the response timeout time, the default is 20 seconds
.maxConnections(128) // Set the connection pool size
.maxIdleTimeOut(Duration.ofSeconds(50)) // Set the connection pool timeout, the default is 30 seconds
// Configure the proxy
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<YOUR-PROXY-HOSTNAME>", 9001))
.setCredentials("<YOUR-PROXY-USERNAME>", "<YOUR-PROXY-PASSWORD>"))
// If it is an https connection, you need to configure the certificate, or ignore the certificate(.ignoreSSL(true))
.x509TrustManagers(new X509TrustManager[]{})
.keyManagers(new KeyManager[]{})
.ignoreSSL(false)
.build();*/
// Configure Credentials authentication information, including ak, secret, token
StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
.accessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
.accessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
//.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
.build());
// Configure the Client
AsyncClient client = AsyncClient.builder()
.region("<Your RegionId>") // Region ID
//.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
.credentialsProvider(provider)
//.serviceConfiguration(Configuration.create()) // Service-level configuration
// Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
.overrideConfiguration(
ClientOverrideConfiguration.create()
// Endpoint 请参考 https://api.aliyun.com/product/live
.setEndpointOverride("live.aliyuncs.com")
//.setConnectTimeout(Duration.ofSeconds(30))
)
.build();
// Parameter settings for API request
BatchSetLiveDomainConfigsRequest batchSetLiveDomainConfigsRequest = BatchSetLiveDomainConfigsRequest.builder()
.domainNames("<Your DomainNames>")
.functions("<Your Functions>")
// Request-level configuration rewrite, can set Http request parameters, etc.
// .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
.build();
// Asynchronously get the return value of the API request
CompletableFuture<BatchSetLiveDomainConfigsResponse> response = client.batchSetLiveDomainConfigs(batchSetLiveDomainConfigsRequest);
// Synchronously get the return value of the API request
BatchSetLiveDomainConfigsResponse resp = response.get();
System.out.println(new Gson().toJson(resp));
// Asynchronous processing of return values
/*response.thenAccept(resp -> {
System.out.println(new Gson().toJson(resp));
}).exceptionally(throwable -> { // Handling exceptions
System.out.println(throwable.getMessage());
return null;
});*/
// Finally, close the client
client.close();
}
}
以上示例代码表示对域名<DomainNames>配置HTTPS强制跳转,HTTP请求将强制重定向为HTTPS请求。
使用JAVA SDK,具体请参见Java SDK使用说明。
相关API:
接口 | 描述 | 参考文档 |
BatchSetLiveDomainConfigs | 批量配置域名。 可以通过Functions参数的https_force记录进行HTTPS强制跳转配置。 |
注意事项
配置 | 说明 |
停用和启用HTTPS |
|
上传证书/私钥 |
|
查看证书 | 允许用户查看证书,但是只支持查看证书,由于私钥信息敏感不支持私钥查看,请您妥善保管证书相关信息。 |
修改编辑证书 | 支持修改编辑证书,但注意生效时间是1小时,请慎重操作。 |