阿里云视频点播Java-服务(如何进行HLS的加密与播放)


话不多说,上代码。这是我提交工单,问了好久才找到的!

package com.meeno.wzq.util;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.kms.model.v20160120.DecryptRequest;
import com.aliyuncs.kms.model.v20160120.DecryptResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.meeno.wzq.config.AliVideoConfig;
import com.sun.net.httpserver.*;
import com.sun.net.httpserver.spi.HttpServerProvider;
import org.apache.commons.codec.binary.Base64;


import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URI;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * @program: server-java
 * @description: 阿里云加密解密
 * @author: Wzq
 * @create: 2019-06-18 17:00
 */


public class AuthorizationServer {
    private static DefaultAcsClient client;
    static {
        String region = AliVideoConfig.regionId;
        String accessKeyId = AliVideoConfig.accessKeyId;
        String accessKeySecret = AliVideoConfig.accessKeySecret;
        client = new DefaultAcsClient(DefaultProfile.getProfile(region, accessKeyId, accessKeySecret));
    }
    public class AuthorizationHandler implements HttpHandler {
        public void handle(HttpExchange httpExchange) throws IOException {
            String requestMethod = httpExchange.getRequestMethod();
            if(requestMethod.equalsIgnoreCase("GET")){
//从URL中取得密文密钥
                String ciphertext = getCiphertext(httpExchange);
                if (null == ciphertext)
                    return;
//从KMS中解密出来,并Base64 decode
                byte[] key = decrypt(ciphertext);
//设置header
                setHeader(httpExchange, key);
//返回密钥
                OutputStream responseBody = httpExchange.getResponseBody();
                responseBody.write(key);
                responseBody.close();
            }
        }
        private void setHeader(HttpExchange httpExchange, byte[] key) throws IOException {
            Headers responseHeaders = httpExchange.getResponseHeaders();
            responseHeaders.set("Access-Control-Allow-Origin", "*");
            httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, key.length);
        }
        private byte[] decrypt(String ciphertext) {
            DecryptRequest request = new DecryptRequest();
            request.setCiphertextBlob(ciphertext);
            request.setProtocol(ProtocolType.HTTPS);
            try {
                DecryptResponse response = client.getAcsResponse(request);
                String plaintext = response.getPlaintext();
//注意:需要base64 decode
                return Base64.decodeBase64(plaintext);
            } catch (ClientException e) {
                e.printStackTrace();
                return null;
            }
        }
        private String getCiphertext(HttpExchange httpExchange) {
            URI uri = httpExchange.getRequestURI();
            String queryString = uri.getQuery();
            String pattern = "Ciphertext=(\\w*)";
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(queryString);
            if (m.find())
                return m.group(1);
            else {
                System.out.println("Not Found Ciphertext");
                return null;
            }
        }
    }
    private void startService() throws IOException {
        HttpServerProvider provider = HttpServerProvider.provider();
//监听端口8888,能同时接受10个请求
        try {
            Socket socket = new Socket("127.0.0.1", 8888);
            socket.close();
            System.out.println("8888 运行中....");
        } catch (IOException e) {
           /* HttpsServer httpsServer = provider.createHttpsServer(new InetSocketAddress(8888), 100000);
            httpsServer.createContext("/", new AuthorizationHandler());
            httpsServer.start();
            System.out.println("server started...");*/


//
//            HttpsServer httpsServer = HttpsServer.create(new InetSocketAddress(8888),100000);
//
//
//
//            httpsServer.createContext("/", new AuthorizationHandler());
//            httpsServer.start();
//            System.out.println("start server sucessfully!");


            HttpServer httpserver = provider.createHttpServer(new InetSocketAddress(8888), 100000);
            httpserver.createContext("/", new AuthorizationHandler());
            httpserver.start();
            System.out.println("server started");
        }
    }


    public static void startHlsService(){
        AuthorizationServer server = new AuthorizationServer();
        try {
            server.startService();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws IOException {
        AuthorizationServer server = new AuthorizationServer();
        try {
            server.startService();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

技术支持:高岳峰,闫宇峰,施凯雷,杨珂

他的个人博客地址:http://120.78.93.197/

这是我的公众号 有最新的it咨询,和个人工作的记录:

这是我的个人微信遇到问题欢迎,提问:

最后加上高质量的淘宝店:如有质量问题随时滴滴我,童叟无欺!

    

【童装园服定制店铺】https://m.tb.cn/h.ef2J8CD?sm=6e0f74 点击链接,再选择浏览器咑閞;或復·制这段描述¥zfwjY4JVngW¥后到淘♂寳♀

   

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值