前端小伙伴进行Oss操作时需要调用接口获得凭证,以下可以直接复用:
package com.szlaozi.ldd.company.controller;
import com.aliyun.oss.ClientException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.szlaozi.ldd.company.controller.entity.StsToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: Liu...cx..
* @Description:
* @Date: 2020/1/18 9:55
* @Modified:
*/
@Slf4j
@RestController
@CrossOrigin
public class CommonController {
@Value("${aliyun.sts.accessKeyId}")
private String accesskeyid;
@Value("${aliyun.sts.accessKeySecret}")
private String accesskeysecret;
@Value("${aliyun.sts.endPoint}")
private String endPoint;
@Value("${aliyun.sts.roleArn}")
private String rolearn;
@Value("${aliyun.sts.roleSessionName}")
private String rolesessionName;
/**
* 获取阿里云图片上传 STStoken接口
*
* @return
*/
@PostMapping("/companies/getStsToken")
public StsToken getStsToken() {
String endpoint = endPoint;
String accessKeyId = accesskeyid;
String accessKeySecret = accesskeysecret;
String roleArn = rolearn;
String roleSessionName = rolesessionName;
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 request = new AssumeRoleRequest();
request.setMethod(MethodType.POST);
request.setRoleArn(roleArn);
request.setRoleSessionName(roleSessionName);
request.setDurationSeconds(1000L); // 设置凭证有效时间
final AssumeRoleResponse response = client.getAcsResponse(request);
log.info("->Expiration is -> {}:", response.getCredentials().getExpiration() + ",Access Key Id:" + response.getCredentials().getAccessKeyId() + ",Access Key Secret:" + response.getCredentials().getAccessKeySecret() + ",Security Token:" + response.getCredentials().getSecurityToken() + ",RequestId:" + response.getRequestId());
StsToken stsToken = new StsToken();
stsToken.setAccessKeyId(response.getCredentials().getAccessKeyId());
stsToken.setAccessKeySecret(response.getCredentials().getAccessKeySecret());
stsToken.setStsToken(response.getCredentials().getSecurityToken());
return stsToken;
} catch (ClientException e) {
e.printStackTrace();
log.info("->Error code: is -> {}:", e.getErrorCode() + ",Error message:" + e.getErrorMessage() + ",RequestId:" + e.getRequestId());
} catch (ServerException e) {
e.printStackTrace();
} catch (com.aliyuncs.exceptions.ClientException e) {
e.printStackTrace();
}
return null;
}
}
其中需要配置文件:
sts:
accessKeyId: *************************
accessKeySecret: *************************
endPoint: *************************
roleArn: acs:ram: *************************
roleSessionName: *************************
相关依赖:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-sts</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.5.0</version>
</dependency>