1. 程式人生 > >阿裏雲視頻 播放獲取簽名方法

阿裏雲視頻 播放獲取簽名方法

ppi 5-0 session ins clas name dom sim addheader

public class AccKeyUtilController extends BaseController {

public static String REGION_ID = "cn-beijing";
public static String ACCESSKEY_ID = "《你的AccessKey——id》";
public static String SECRET = "《賬號secret》";
public static String ROLE_ARN = "《賬號的 arn》";
private final static String ISO8601_DATE_FORMAT = "yyyy-MM-dd‘T‘HH:mm:ss‘Z‘";
private final static String KEY = "《鑒權key》";

@RequestMapping(value="/getSign", method= RequestMethod.POST)
public Map<String, String> getSignature(HttpServletRequest request, HttpServletResponse resp) {
/*resp.addHeader("Content-type", "application/json;charset=UTF-8");
resp.addHeader("Access-Control-Allow-Origin", "*");*/
/*resp.setHeader("Content-type", "application/json;charset=UTF-8");
resp.setHeader("Access-Control-Allow-Origin", "*");*/
String mediaId = null;
try {
JSONObject params = this.getJSONObject(request.getInputStream());
mediaId = (String)params.get("mediaId");
} catch (IOException e) {
e.printStackTrace();
}

IClientProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESSKEY_ID, SECRET);
DefaultAcsClient client = new DefaultAcsClient(profile);
Map<String, String> resultMap = new HashMap<>();
AssumeRoleResponse response = null;
AssumeRoleResponse.Credentials credentials;
try {
response = assumeRole(client, ROLE_ARN);
credentials = response.getCredentials();
System.out.println(credentials.getAccessKeyId() + "\n---akid" + "\n" +
credentials.getAccessKeySecret() + "\n---aks" + "\n" +
credentials.getSecurityToken() + "\n---st" + "\n" +
credentials.getExpiration());

String expiretime = nowTime();
try {
//時間 進行轉碼
String encode = AcsURLEncoder.encode(expiretime);
System.out.println(expiretime + "---時間");
//拼裝字符串
String data = "ExpireTime=" + encode + "&MediaId=" + mediaId;
System.out.println(data);

//開始生成 簽名
SecretKeySpec signKey = new SecretKeySpec(KEY.getBytes(), "HmacSHA1");
System.out.println(signKey);
Mac mac = null;
try {
mac = Mac.getInstance("HmacSHA1");
mac.init(signKey);
//對生成的簽名進行 BASE64轉碼
byte[] bytes = mac.doFinal(data.getBytes());
String encode2 = new BASE64Encoder().encode(bytes);
System.out.println(encode2);
//把需要的數據 放入MAP中 返回到前臺 s
resultMap.put("accessKeyId",credentials.getAccessKeyId());
resultMap.put("securityToken",credentials.getSecurityToken());
resultMap.put("accessKeySecret",credentials.getAccessKeySecret());
resultMap.put("ExpireTime",expiretime);
resultMap.put("Signature",encode2);
resultMap.put("domainRegion",REGION_ID);
return resultMap;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

} catch (ClientException e) {
e.printStackTrace();
}
return null;
}

private static AssumeRoleResponse assumeRole(DefaultAcsClient client, String roleArn) throws ClientException {
final AssumeRoleRequest request = new AssumeRoleRequest();
request.setVersion("2015-04-01");
request.setMethod(MethodType.POST);
request.setProtocol(ProtocolType.HTTPS);
request.setDurationSeconds(1200L);
request.setRoleArn(roleArn);
request.setRoleSessionName("test-token");
return client.getAcsResponse(request);
}

public static String nowTime() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT+10"));
SimpleDateFormat df = new SimpleDateFormat(ISO8601_DATE_FORMAT);
Date date = new Date();
return df.format(date);
}
}


//前臺播放器
var player = new Aliplayer({
id: ‘J_prismPlayer‘,
width: ‘80%‘,
autoplay: false,

//播放方式三:僅MTS用戶使用
vid: ‘<視頻ID>‘,
accId: ‘<驗證簽名後--返回的accID>‘,
format:‘m3u8‘,
accSecret: ‘<驗證簽名後--返回的>‘,

stsToken: ‘<簽證簽名後--返回的>‘,
    domainRegion: ‘cn-beijing‘,
useFlashPrism:true,
authInfo: ‘{"ExpireTime": "<過期時間-GMT格式>", "MediaId": "<視頻Id>","Signature": "<驗證簽名--生成的簽名>"}‘


}, function (player) {
console.log(‘播放器創建好了。‘)
});

阿裏雲視頻 播放獲取簽名方法