Commit 207f8589 authored by Quxl's avatar Quxl

Merge branch 'master' of http://key@gitlab.egolm.com/key/shop.git

parents 0731cbcb 2d44429c
......@@ -106,6 +106,42 @@
<artifactId>sigar</artifactId>
<version>1.6.4</version>
</dependency>
<!-- 阿里云 start -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</artifactId>
<version>2.11.6</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<!-- <version>2.3.0</version> -->
<version>3.5.0</version>
</dependency>
<!-- 阿里云 end -->
<!-- 腾讯云 start -->
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.6.7</version>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>cos-sts-java</artifactId>
<version>3.0.4</version>
</dependency>
<!-- 腾讯云 end-->
</dependencies>
<build>
<plugins>
......@@ -150,5 +186,17 @@
<name>mvnrepository</name>
<url>http://mvnrepository.com/</url>
</repository>
<repository>
<id>bintray-qcloud-maven-repo</id>
<name>qcloud-maven-repo</name>
<url>https://dl.bintray.com/qcloud/maven-repo/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
\ No newline at end of file
package com.egolm.shop.controller;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.aliyun.oss.ClientException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.egolm.common.StringUtil;
import com.egolm.common.bean.Rjx;
import com.egolm.shop.service.SystemCtrlService;
import com.egolm.shop.util.ShopContstrant;
import com.tencent.cloud.CosStsClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
@Api(tags={"服务云接口"})
@RestController
@RequestMapping("yun")
public class YunController {
@Autowired
private SystemCtrlService systemCtrlService;
@ApiOperation("获取上传凭证及上传的云平台")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "langID", dataType = "String", required = false, value = "语言", defaultValue = "936"),
@ApiImplicitParam(paramType = "query", name = "sign", dataType = "String", required = true, value = "签名", defaultValue = ""),
@ApiImplicitParam(paramType = "query", name = "timestamp", dataType = "String", required = true, value = "时间戳", defaultValue = "")
})
@RequestMapping(value = "/getPolicy",method=RequestMethod.GET)
public String createGuest(HttpServletRequest request, HttpServletResponse response) {
Map<String, Object> choiceMap = systemCtrlService.queryTSystemCtrlByCode(ShopContstrant.Choice_OSS_KEY, "936");
if(choiceMap != null) {
String value1 = choiceMap.get("sValue1") + "";
System.out.println(value1);
//https://help.aliyun.com/document_detail/100624.html?spm=a2c4g.11186623.2.10.74ba41f0DhTvNE
if (value1.equalsIgnoreCase(ShopContstrant.Aliyun_OSS_KEY)) {
try {
Map<String,Object> map = systemCtrlService.queryTSystemCtrlByCode(ShopContstrant.Aliyun_OSS_KEY, "936");
if (map != null) {
String endPonit = map.get("sValue1") + "";
String bucketName = map.get("sValue2") + "";
String appID = map.get("sValue3") + "";
String secret = map.get("sValue4") + "";
String webUrl = map.get("sValue5") + "";
String endpoint = "sts.aliyuncs.com";
String accessKeyId = "<access-key-id>";
String accessKeySecret = "<access-key-secret>";
String roleArn = "<role-arn>";
String roleSessionName = "session-name";
String policy = "{\n" +
" \"Version\": \"1\", \n" +
" \"Statement\": [\n" +
" {\n" +
" \"Action\": [\n" +
" \"oss:*\"\n" +
" ], \n" +
" \"Resource\": [\n" +
" \"acs:oss:*:*:*\" \n" +
" ], \n" +
" \"Effect\": \"Allow\"\n" +
" }\n" +
" ]\n" +
"}";
try {
// 添加endpoint(直接使用STS endpoint,前两个参数留空,无需添加region ID)
DefaultProfile.addEndpoint("", "", "Sts", endpoint);
// 构造default profile(参数留空,无需添加region ID)
IClientProfile profile = DefaultProfile.getProfile("", accessKeyId, accessKeySecret);
// 用profile构造client
DefaultAcsClient client = new DefaultAcsClient(profile);
final AssumeRoleRequest aliyunRequest = new AssumeRoleRequest();
aliyunRequest.setMethod(MethodType.POST);
aliyunRequest.setRoleArn(roleArn);
aliyunRequest.setRoleSessionName(roleSessionName);
aliyunRequest.setPolicy(policy); // 若policy为空,则用户将获得该角色下所有权限
aliyunRequest.setDurationSeconds(1000L); // 设置凭证有效时间
final AssumeRoleResponse aliyunResponse = client.getAcsResponse(aliyunRequest);
System.out.println("Expiration: " + aliyunResponse.getCredentials().getExpiration());
System.out.println("Access Key Id: " + aliyunResponse.getCredentials().getAccessKeyId());
System.out.println("Access Key Secret: " + aliyunResponse.getCredentials().getAccessKeySecret());
System.out.println("Security Token: " + aliyunResponse.getCredentials().getSecurityToken());
System.out.println("RequestId: " + aliyunResponse.getRequestId());
Map<String,Object> resultMap = new HashMap<String,Object>();
resultMap.put("tmpSecretId", aliyunResponse.getCredentials().getAccessKeyId());
resultMap.put("tmpSecretKey", aliyunResponse.getCredentials().getAccessKeySecret());
resultMap.put("sessionToken", aliyunResponse.getCredentials().getSecurityToken());
resultMap.put("webUrl", webUrl);
resultMap.put("region","");
resultMap.put("bucketName", "");
resultMap.put("type", "aliyun");
return Rjx.json(resultMap).setCode(200).toString();
} catch (ClientException e1) {
e1.printStackTrace();
return Rjx.json().setMessage("阿里云临时密钥获取失败").toJson();
}
}else {
return Rjx.json().setMessage("阿里云OSS参数未配置").toJson();
}
} catch (Exception e) {
e.printStackTrace();
return Rjx.json().setMessage("阿里云临时密钥获取异常").toJson();
}
}
if(value1.equalsIgnoreCase(ShopContstrant.Tencent_Cos_Key)) {
int expiryTime = 1800; //秒
Map<String,Object> expiryMap = systemCtrlService.queryTSystemCtrlByCode(ShopContstrant.Tencent_Expiry_Time, "936"); //过期时间
if(expiryMap != null) {
try {
String expriyMM = expiryMap.get("sValue1") + ""; //分钟
if(StringUtil.isNotEmpty(expriyMM)) {
expiryTime = Integer.valueOf(expriyMM) * 60;
}
} catch (NumberFormatException e) {
}
}
Map<String, Object> tencentyunMap = systemCtrlService.queryTSystemCtrlByCode(ShopContstrant.Tencent_Cos_Key, "936");
if(tencentyunMap == null) {
return Rjx.jsonErr().setMessage("腾讯云COS参数未配置").toJson();
}
String region = tencentyunMap.get("sValue1") + "";
String bucketName = tencentyunMap.get("sValue2") + "";
String appID = tencentyunMap.get("sValue3") + "";
String secret = tencentyunMap.get("sValue4") + "";
String webUrl = tencentyunMap.get("sValue5")+"";
TreeMap<String, Object> config = new TreeMap<String, Object>();
try {
// 替换为您的 SecretId
config.put("SecretId", appID);
// 替换为您的 SecretKey
config.put("SecretKey", secret);
// 临时密钥有效时长,单位是秒
config.put("durationSeconds", expiryTime);
// 换成您的 bucket
config.put("bucket", bucketName);
// 换成 bucket 所在地区
config.put("region", region);
// 这里改成允许的路径前缀,可以根据自己网站的用户登录态判断允许上传的目录,例子:* 或者 doc/* 或者 picture.jpg
config.put("allowPrefix", "*");
// 密钥的权限列表。简单上传、表单上传和分片上传需要以下的权限,其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
String[] allowActions = new String[] {
// 简单上传
"name/cos:PutObject",
// 表单上传、小程序上传
"name/cos:PostObject",
// 分片上传
"name/cos:InitiateMultipartUpload",
"name/cos:ListMultipartUploads",
"name/cos:ListParts",
"name/cos:UploadPart",
"name/cos:CompleteMultipartUpload"
};
config.put("allowActions", allowActions);
System.out.println(config);
JSONObject credential = CosStsClient.getCredential(config);
System.out.println(credential);
//成功返回临时密钥信息,如下打印密钥信息
JSONObject credentialObj = credential.getJSONObject("credentials");
Map<String,Object> resultMap = new HashMap<String,Object>();
resultMap.put("tmpSecretId", credentialObj.getString("tmpSecretId"));
resultMap.put("tmpSecretKey", credentialObj.getString("tmpSecretKey"));
resultMap.put("sessionToken", credentialObj.getString("sessionToken"));
resultMap.put("webUrl", webUrl);
resultMap.put("region", region);
resultMap.put("bucketName", bucketName);
resultMap.put("type", "tencentyun");
return Rjx.json(resultMap).setCode(200).toString();
}catch (Exception e) {
e.printStackTrace();
//失败抛出异常
return Rjx.json().setMessage("腾讯云临时密钥获取失败").toJson();
}
}
}
return Rjx.jsonOk().toString();
}
}
......@@ -15,4 +15,13 @@ public class ShopContstrant {
public static final String SystemCtl_Redis_Base_Key = "SystemCtl"; //与job服务中缓存的Key一样 存放在 opsForHash中
public static final String CLIENT_MD5KEY ="Client_Md5Key"; //客户端计算签名的MD5 Key
//上传的
public static final String Choice_OSS_KEY= "ChoiceOssKEY"; //OSS选择
public static final String Self_OSS_KEY="SelfOssKey"; //自建OSS
public static final String Aliyun_OSS_KEY="AliyunOssKey"; //阿里云OSS
public static final String Tencent_Cos_Key="TencentCosKey"; //腾讯云COS
public static final String Aliyun_Expiry_Time="AliOssFileExpiryTime"; //阿里文件 过期时间 单位分钟
public static final String Tencent_Expiry_Time="TencentExpiryTime"; //腾讯文件 过期时间 单位分钟
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment