7 changed files with 248 additions and 25 deletions
@ -0,0 +1,48 @@ |
|||
package com.ccsens.mt.api; |
|||
|
|||
import com.ccsens.cloudutil.annotation.MustLogin; |
|||
import com.ccsens.mt.bean.dto.CompeteDto; |
|||
import com.ccsens.mt.bean.vo.CompeteVo; |
|||
import com.ccsens.mt.service.IKCPlayerService; |
|||
import com.ccsens.util.JsonResponse; |
|||
import com.ccsens.util.bean.dto.QueryDto; |
|||
import io.swagger.annotations.Api; |
|||
import io.swagger.annotations.ApiOperation; |
|||
import io.swagger.annotations.ApiParam; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.validation.annotation.Validated; |
|||
import org.springframework.web.bind.annotation.RequestBody; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RequestMethod; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
|
|||
import javax.annotation.Resource; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
@Slf4j |
|||
@Api(tags = "云点播", description = "") |
|||
@RestController |
|||
@RequestMapping("/kcPlayer") |
|||
public class KCPlayerController { |
|||
@Resource |
|||
private IKCPlayerService kcPlayerService; |
|||
|
|||
@ApiOperation(value = "查看云点播签名", notes = "从redis获取云点播签名,有则返回,没有则调用工具类查询,存入redis并返回") |
|||
@RequestMapping(value = "/get", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) |
|||
public JsonResponse<String> getSignature() { |
|||
log.info("查看云点播签名"); |
|||
String signature = kcPlayerService.getSignature(); |
|||
log.info("查看云点播签名:{}",signature); |
|||
return JsonResponse.newInstance().ok(signature); |
|||
} |
|||
|
|||
@ApiOperation(value = "查看云点播签名", notes = "从redis获取云点播签名,有则返回,没有则调用工具类查询,存入redis并返回") |
|||
@RequestMapping(value = "/receive", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) |
|||
public JsonResponse receive(@RequestBody Map map) { |
|||
log.info("接受文件上传通知:{}",map); |
|||
|
|||
return JsonResponse.newInstance().ok(); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,5 @@ |
|||
package com.ccsens.mt.service; |
|||
|
|||
public interface IKCPlayerService { |
|||
String getSignature(); |
|||
} |
@ -0,0 +1,53 @@ |
|||
package com.ccsens.mt.service; |
|||
|
|||
import cn.hutool.core.util.ObjectUtil; |
|||
import com.ccsens.mt.util.Constant; |
|||
import com.ccsens.util.CodeEnum; |
|||
import com.ccsens.util.KCPlayerSignature; |
|||
import com.ccsens.util.RedisUtil; |
|||
import com.ccsens.util.exception.BaseException; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.transaction.annotation.Propagation; |
|||
import org.springframework.transaction.annotation.Transactional; |
|||
|
|||
import javax.annotation.Resource; |
|||
import java.util.Random; |
|||
@Slf4j |
|||
@Service |
|||
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) |
|||
public class KCPlayerService implements IKCPlayerService{ |
|||
@Resource |
|||
private RedisUtil redisUtil; |
|||
|
|||
/** |
|||
* 获取云点播签名 |
|||
* @return 返回签名 |
|||
*/ |
|||
@Override |
|||
public String getSignature() { |
|||
String signature; |
|||
//查询redis
|
|||
Object o = redisUtil.get(Constant.Redis.KC_PLAYER_SIGNATURE); |
|||
if(ObjectUtil.isNotNull(o)){ |
|||
return (String) o; |
|||
} |
|||
|
|||
KCPlayerSignature sign = new KCPlayerSignature(); |
|||
sign.setCurrentTime(System.currentTimeMillis() / 1000); |
|||
sign.setRandom(new Random().nextInt(java.lang.Integer.MAX_VALUE)); |
|||
// 签名有效期:2天
|
|||
sign.setSignValidDuration(3600 * 24 * 2); |
|||
try { |
|||
signature = sign.getUploadSignature(); |
|||
log.info("获取云点播签名成功:{}",signature); |
|||
//存入redis
|
|||
redisUtil.set(Constant.Redis.KC_PLAYER_SIGNATURE,signature,3600 * 24); |
|||
} catch (Exception e) { |
|||
log.error("获取云点播签名失败",e); |
|||
throw new BaseException(CodeEnum.SYS_ERROR); |
|||
|
|||
} |
|||
return signature; |
|||
} |
|||
} |
@ -0,0 +1,104 @@ |
|||
package com.ccsens.util; |
|||
|
|||
|
|||
import java.util.Base64; |
|||
import java.util.Random; |
|||
import javax.crypto.Mac; |
|||
import javax.crypto.spec.SecretKeySpec; |
|||
|
|||
|
|||
/** |
|||
* 签名工具类 |
|||
* @author 逗 |
|||
*/ |
|||
public class KCPlayerSignature{ |
|||
private String secretId = "AKIDTr6B7uA2qCRWRDRXyeUAZpkfR2tupVFu"; |
|||
private String secretKey = "wVtKAuXEjXFpxQmz9PM6hWallNEyZ0Na"; |
|||
private long currentTime; |
|||
private int random; |
|||
private int signValidDuration; |
|||
//签名算法
|
|||
private static final String HMAC_ALGORITHM = "HmacSHA1"; |
|||
private static final String CONTENT_CHARSET = "UTF-8"; |
|||
|
|||
public static byte[] byteMerger(byte[] byte1, byte[] byte2) { |
|||
byte[] byte3 = new byte[byte1.length + byte2.length]; |
|||
System.arraycopy(byte1, 0, byte3, 0, byte1.length); |
|||
System.arraycopy(byte2, 0, byte3, byte1.length, byte2.length); |
|||
return byte3; |
|||
} |
|||
|
|||
// 获取签名
|
|||
public String getUploadSignature() throws Exception { |
|||
String strSign = ""; |
|||
String contextStr = ""; |
|||
|
|||
// 生成原始参数字符串
|
|||
long endTime = (currentTime + signValidDuration); |
|||
contextStr += "secretId=" + java.net.URLEncoder.encode(secretId, "utf8"); |
|||
contextStr += "¤tTimeStamp=" + currentTime; |
|||
contextStr += "&expireTime=" + endTime; |
|||
contextStr += "&random=" + random; |
|||
contextStr += "&sourceContext=id"; |
|||
|
|||
|
|||
try { |
|||
Mac mac = Mac.getInstance(HMAC_ALGORITHM); |
|||
SecretKeySpec secretKey = new SecretKeySpec(this.secretKey.getBytes(CONTENT_CHARSET), mac.getAlgorithm()); |
|||
mac.init(secretKey); |
|||
|
|||
byte[] hash = mac.doFinal(contextStr.getBytes(CONTENT_CHARSET)); |
|||
byte[] sigBuf = byteMerger(hash, contextStr.getBytes("utf8")); |
|||
strSign = base64Encode(sigBuf); |
|||
strSign = strSign.replace(" ", "").replace("\n", "").replace("\r", ""); |
|||
} catch (Exception e) { |
|||
throw e; |
|||
} |
|||
return strSign; |
|||
} |
|||
|
|||
private String base64Encode(byte[] buffer) { |
|||
Base64.Encoder encoder = Base64.getEncoder(); |
|||
return encoder.encodeToString(buffer); |
|||
} |
|||
|
|||
|
|||
public void setSecretId(String secretId) { |
|||
this.secretId = secretId; |
|||
} |
|||
|
|||
public void setSecretKey(String secretKey) { |
|||
this.secretKey = secretKey; |
|||
} |
|||
|
|||
public void setCurrentTime(long currentTime) { |
|||
this.currentTime = currentTime; |
|||
} |
|||
|
|||
public void setRandom(int random) { |
|||
this.random = random; |
|||
} |
|||
|
|||
public void setSignValidDuration(int signValidDuration) { |
|||
this.signValidDuration = signValidDuration; |
|||
} |
|||
|
|||
static void main(String[] args) { |
|||
KCPlayerSignature sign = new KCPlayerSignature(); |
|||
// 设置 App 的云 API 密钥
|
|||
sign.setSecretId("个人 API 密钥中的 Secret Id"); |
|||
sign.setSecretKey("个人 API 密钥中的 Secret Key"); |
|||
sign.setCurrentTime(System.currentTimeMillis() / 1000); |
|||
sign.setRandom(new Random().nextInt(java.lang.Integer.MAX_VALUE)); |
|||
// 签名有效期:2天
|
|||
sign.setSignValidDuration(3600 * 24 * 2); |
|||
|
|||
try { |
|||
String signature = sign.getUploadSignature(); |
|||
System.out.println("signature : " + signature); |
|||
} catch (Exception e) { |
|||
System.out.print("获取签名失败"); |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue