ApsaraVideo for Live支援通過設定Referer黑白名單、User-Agent黑白名單、IP黑白名單進行存取控制,本文介紹ApsaraVideo for Live存取控制功能、適用情境和使用方式。
簡介
存取控制是在雲端配置視頻資源的存取原則,達到基本的保護目的,具有使用門檻低(僅雲端配置不需要額外開發)、快速生效等優點,主要手段有:
Referer黑白名單
User-Agent黑白名單
IP黑白名單
User-Agent黑白名單,由於配置繁瑣且存在誤操作風險,暫未開放控制台設定,如有需求可以提交工單聯絡阿里雲支援人員。關於如何提交工單,請參見聯絡我們。
適用情境
存取控制的使用門檻很低,只需簡單配置即可使用,能起到一定的保護作用,特別是Web端。
Referer和User-Agent都基於HTTP Header,容易被偽造,安全性低。
IP黑白名單機制在大規模使用者管理上存在局限性,不適合廣泛的內容消費情境。
Referer黑白名單
介紹
通過HTTP協議的Referer機制,使用者可配置Referer黑白名單來限制視頻資源的訪問。
請求到達CDN節點後,節點根據預設的黑白名單進行過濾,符合規則的請求可訪問視頻資料,否則返回403響應碼。
Referer黑白名單是可選配置,預設不啟用。
配置後會自動添加泛網域名稱支援。例如填寫
example.com,最終配置生效的是*.example.com,所有子級網域名稱都會生效。支援設定是否允許空Referer欄位訪問資源,即允許通過瀏覽器地址欄直接存取資源URL。
使用方式
控制台:具體操作請參見防盜鏈。
API:通過調用BatchSetLiveDomainConfigs介面,可大量設定直播網域名稱,包括Referer黑白名單。
在pom.xml檔案中引入Java SDK:
<dependency> <groupId>com.aliyun</groupId> <artifactId>alibabacloud-live20161101</artifactId> <version>2.0.3</version> </dependency>Java SDK設定Referer防盜鏈的範例程式碼如下:
// 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>設定Referer防盜鏈白名單,白名單網域名稱列表為<referer_white_domain_list>,且不允許空Referer訪問(allow_empty值為<off>)。
使用JAVA SDK,具體請參見Java SDK使用說明。
注意事項
重要黑白名單互斥,只能選擇一種。
移動端預設支援空Referer訪問,可選擇關閉。若不允許空Referer訪問,可配合阿里雲播放器設定Referer。
當您設定不允許空Referer訪問時,請務必配置HTTPS安全加速,並開啟強制跳轉HTTPS(HTTP>HTTPS)。部分瀏覽器處理HTTPS請求HTTP的資源時,會移除Referer,導致無法訪問。
樣本
當設定播流網域名稱
demo.developer.aliyundoc.com的Referer白名單為example.com,且不允許空Referer訪問。請求資料:
curl -i 'http://demo.developer.aliyundoc.com/apptest/stream0000'返回中包含:
X-Tengine-Error:denied by Referer ACL當請求帶上允許的Referer後即正常返回:
curl -i 'http://demo.developer.aliyundoc.com/apptest/stream0000' \ -H 'Referer: http://www.example.com'
User-Agent黑白名單
介紹
User-Agent是一個字串頭,用於識別使用者的作業系統、瀏覽器及其版本等資訊。通過User-Agent黑白名單可以限制特定瀏覽器或終端的訪問。
使用方式
提交工單聯絡阿里雲支援人員。關於如何提交工單,請參見聯絡我們。
樣本
如PC端IE9瀏覽器:
User-Agent:Mozilla/5.0(compatible;MSIE9.0;WindowsNT6.1;Trident/5.0;可類比HTTP請求驗證:
curl -i 'http://demo.developer.aliyundoc.com/apptest/stream0000' \ -H 'User-Agent: iPhone OS;MI 5'
IP黑白名單
介紹
支援配置IP黑名單或白名單,拒絕或只允許特定IP的訪問。
添加IP到黑名單,拒絕該IP訪問當前加速網域名稱。
添加IP到白名單,僅允許該IP訪問當前加速網域名稱。
支援IP列表添加,並支援添加IP網段。
例如:網段127.0.0.0/24,24表示採用子網路遮罩中的前24位為有效位,即用32-24=8bit來表示主機號,該子網可以容納28-2=254台主機,故可表示IP網段範圍:127.0.0.0~127.0.0.254。
支援IPV6地址。
使用方式
控制台:具體操作,請參見IP黑白名單。
API:通過調用BatchSetLiveDomainConfigs介面,可大量設定直播網域名稱,包括IP黑白名單。
Java SDK設定IP黑白名單的範例程式碼如下:
// 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>設定IP白名單,白名單IP列表為<127.0.0.0/24>。
使用JAVA SDK,具體請參見Java SDK使用說明。
注意事項
重要黑、白名單互斥,您只能選擇一種方式。
最多可配置1000個IPv6地址或3000個IPv4地址。IPv6地址不區分大小寫,不支援::縮寫格式。