|
@@ -9,6 +9,9 @@ import com.aliyuncs.profile.DefaultProfile;
|
|
|
import com.aliyuncs.profile.IClientProfile;
|
|
|
import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
|
|
|
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
+import org.springframework.data.redis.core.ValueOperations;
|
|
|
|
|
|
import java.net.URL;
|
|
|
import java.util.Date;
|
|
@@ -16,12 +19,23 @@ import java.util.Date;
|
|
|
/**
|
|
|
* Created by yao on 17-10-10.
|
|
|
*/
|
|
|
-public class AliStsService {
|
|
|
+public class AliStsOssAuth {
|
|
|
+
|
|
|
+ private StringRedisTemplate stringRedisTemplate;
|
|
|
+
|
|
|
+ private String accessKeyIdTemp = null;
|
|
|
+ private String accessKeySecretTemp = null;
|
|
|
+ private String securityTokenTemp = null;
|
|
|
+
|
|
|
+ public AliStsOssAuth(StringRedisTemplate stringRedisTemplate) {
|
|
|
+ this.stringRedisTemplate = stringRedisTemplate;
|
|
|
+ }
|
|
|
+
|
|
|
// 目前只有"cn-hangzhou"这个region可用, 不要使用填写其他region的值
|
|
|
- public static final String REGION_CN_HANGZHOU = "cn-hangzhou";
|
|
|
+ public final String REGION_CN_HANGZHOU = "cn-hangzhou";
|
|
|
// 当前 STS API 版本
|
|
|
- public static final String STS_API_VERSION = "2015-04-01";
|
|
|
- public static AssumeRoleResponse assumeRole(String accessKeyId, String accessKeySecret,
|
|
|
+ public final String STS_API_VERSION = "2015-04-01";
|
|
|
+ public AssumeRoleResponse assumeRole(String accessKeyId, String accessKeySecret,
|
|
|
String roleArn, String roleSessionName, String policy,
|
|
|
ProtocolType protocolType) throws ClientException {
|
|
|
try {
|
|
@@ -44,7 +58,7 @@ public class AliStsService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static void Auth(){
|
|
|
+ public void Auth(){
|
|
|
// 只有 RAM用户(子账号)才能调用 AssumeRole 接口
|
|
|
// 阿里云主账号的AccessKeys不能用于发起AssumeRole请求
|
|
|
// 请首先在RAM控制台创建一个RAM用户,并为这个用户创建AccessKeys
|
|
@@ -71,17 +85,34 @@ public class AliStsService {
|
|
|
// 此处必须为 HTTPS
|
|
|
ProtocolType protocolType = ProtocolType.HTTPS;
|
|
|
try {
|
|
|
- final AssumeRoleResponse response = assumeRole(accessKeyId, accessKeySecret,
|
|
|
- roleArn, roleSessionName, policy, protocolType);
|
|
|
+ stringRedisTemplate.opsForValue().set("aaa", "111");
|
|
|
+ System.out.println(stringRedisTemplate.opsForValue().get("aaa"));
|
|
|
+ ValueOperations valueOperations = stringRedisTemplate.opsForValue();
|
|
|
+ if(valueOperations.get("accessKeyIdTemp")!=null
|
|
|
+ && valueOperations.get("accessKeySecretTemp")!=null
|
|
|
+ && valueOperations.get("securityTokenTemp")!=null ){
|
|
|
+ accessKeyIdTemp = valueOperations.get("accessKeyIdTemp").toString();
|
|
|
+ accessKeySecretTemp = valueOperations.get("accessKeySecretTemp").toString();
|
|
|
+ securityTokenTemp = valueOperations.get("securityTokenTemp").toString();
|
|
|
+ }else{
|
|
|
+ AssumeRoleResponse response = assumeRole(accessKeyId, accessKeySecret,
|
|
|
+ roleArn, roleSessionName, policy, protocolType);
|
|
|
+
|
|
|
+ System.out.println("Expiration: " + response.getCredentials().getExpiration());
|
|
|
+ System.out.println("Access Key Id: " + response.getCredentials().getAccessKeyId());
|
|
|
+ System.out.println("Access Key Secret: " + response.getCredentials().getAccessKeySecret());
|
|
|
+ System.out.println("Security Token: " + response.getCredentials().getSecurityToken());
|
|
|
+
|
|
|
+ accessKeyIdTemp = response.getCredentials().getAccessKeyId();
|
|
|
+ accessKeySecretTemp = response.getCredentials().getAccessKeySecret();
|
|
|
+ securityTokenTemp = response.getCredentials().getSecurityToken();
|
|
|
|
|
|
- System.out.println("Expiration: " + response.getCredentials().getExpiration());
|
|
|
- System.out.println("Access Key Id: " + response.getCredentials().getAccessKeyId());
|
|
|
- System.out.println("Access Key Secret: " + response.getCredentials().getAccessKeySecret());
|
|
|
- System.out.println("Security Token: " + response.getCredentials().getSecurityToken());
|
|
|
+ valueOperations.set("accessKeyIdTemp", accessKeyIdTemp);
|
|
|
+ valueOperations.set("accessKeySecretTemp", accessKeySecretTemp);
|
|
|
+ valueOperations.set("securityTokenTemp", securityTokenTemp);
|
|
|
+ }
|
|
|
|
|
|
- String accessKeyIdTemp = response.getCredentials().getAccessKeyId();
|
|
|
- String accessKeySecretTemp = response.getCredentials().getAccessKeySecret();
|
|
|
- String securityTokenTemp = response.getCredentials().getSecurityToken();
|
|
|
+ //原始完整路径:http://efunaudio.oss-cn-beijing.aliyuncs.com/audio/001/00103035/00103035018.mp3
|
|
|
String endpoint = "oss-cn-beijing.aliyuncs.com";
|
|
|
OSSClient client = new OSSClient(endpoint, accessKeyIdTemp, accessKeySecretTemp, securityTokenTemp);
|
|
|
String bucketName = "efunaudio";
|
|
@@ -95,6 +126,8 @@ public class AliStsService {
|
|
|
System.out.println("Failed to get a token.");
|
|
|
System.out.println("Error code: " + e.getErrCode());
|
|
|
System.out.println("Error message: " + e.getErrMsg());
|
|
|
+ } catch (Exception e){
|
|
|
+ System.out.println("Error message: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
}
|