使用Android Studio开发Spring Boot项目,获取到阿里云OSS中的STS值时,报错Caused by: java.lang.ClassNotFoundException: org.springframework.web.server.WebFilter。求大佬指导。
build.gradle(app)如下:
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
compile('org.springframework.boot:spring-boot-starter-parent:2.3.4.RELEASE')
compile('org.springframework.boot:spring-boot-starter-web:2.3.4.RELEASE')
compile('com.aliyun.oss:aliyun-sdk-oss:3.15.0')
compile('com.aliyun:aliyun-java-sdk-sts:3.0.0')
compile('com.aliyun:aliyun-java-sdk-core:4.4.6')
compile('org.projectlombok:lombok:1.16.10')
compile('io.springfox:springfox-boot-starter:3.0.0')
}
通用结果返回类R:
package com.example.myapplication.common;
import java.util.HashMap;
import java.util.Map;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
public class R {
@ApiModelProperty(value = "是否成功")
private Boolean success;
@ApiModelProperty(value = "返回码")
private Integer statusCode; //编码: 200成功.其他数字为失败
@ApiModelProperty(value = "返回消息")
private String msg; //返回的错误信息
@ApiModelProperty(value = "返回数据")
private Map<String, Object> data = new HashMap<>();
private R() {};
public static R ok() {
R r = new R();
r.success = true;
r.statusCode = 200;
r.msg = "成功";
return r;
}
public static R error(){
R r = new R();
r.success = false;
r.statusCode = 500;
r.msg = "失败";
return r;
}
public R success(Boolean success) {
this.success = success;
return this; //返回this,实现链式编程
}
public R message(String message) {
this.msg = message;
return this;
}
public R code(Integer code) {
this.statusCode = code;
return this;
}
public R data(String key, Object value) {
this.data.put(key, value);
return this;
}
public R data(Map<String, Object>map) {
this.data = map;
return this;
}
}
控制类:
package com.example.myapplication.controller;
import com.aliyun.oss.OSS;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.example.myapplication.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
//创建一个控制类
@RestController
public class TestController {
//从配置文件中取出相应的值
@Autowired
private OSS ossClient;
@Value("${alicloud.access-key}")
private String accessId;
@Value("${alicloud.secret-key:}")
private String accessKeySecret;
@Value("${alicloud.oss.endpoint}")
private String endpoint;
@Value("${alicloud.oss.roleArn}")
private String roleArn;
@Value("${alicloud.oss.roleSessionName}")
private String roleSessionName;
@Value("${alicloud.oss.bucketName}")
private String bucketName;
//获取临时访问凭证
@RequestMapping("/oss/sts")
public R sts() throws ClientException {
String policy = "{\n" +
" \"Version\": \"1\", \n" +
" \"Statement\": [\n" +
" {\n" +
" \"Action\": [\n" +
" \"oss:PutObject\"\n" +
" ], \n" +
" \"Resource\": [\n" +
" \"acs:oss:*:*:plantidentificationpicture/*\" \n" +
" ], \n" +
" \"Effect\": \"Allow\"\n" +
" }\n" +
" ]\n" +
"}";
Map<String, Object> respMap = null;
try {
String regionId = "cn-beijing";
DefaultProfile.addEndpoint(regionId, "Sts", endpoint);
IClientProfile profile = DefaultProfile.getProfile(regionId, accessId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
final AssumeRoleRequest request = new AssumeRoleRequest();
request.setSysMethod(MethodType.POST);
request.setRoleArn(roleArn);
request.setRoleSessionName(roleSessionName);
request.setPolicy(policy);
request.setDurationSeconds(3600L);
final AssumeRoleResponse response = client.getAcsResponse(request);
respMap = new HashMap<>();
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());
System.out.println("RequestId: " + response.getRequestId());
respMap.put("Expiration: ", response.getCredentials().getExpiration());
respMap.put("Access Key Id: ", response.getCredentials().getAccessKeyId());
respMap.put("Access Key Secret: ", response.getCredentials().getAccessKeySecret());
respMap.put("Security Token: ", response.getCredentials().getSecurityToken());
respMap.put("RequestId: ", response.getRequestId());
respMap.put("StatusCode", 200);
return R.ok().data(respMap);
} catch (ServerException e) {
System.out.println("Failed:");
System.out.println("Error code: " + e.getErrCode());
System.out.println("Error message: " + e.getErrMsg());
System.out.println("RequestId: " + e.getRequestId());
return R.error().data("data", e);
}
}
}
配置文件:
server:
port: 8080
alicloud:
accessKey: LTAI5tGrKam5hQP46T1ZVU1u
secretKey: AdhoKNSyA7NAARrfglNmFEZkO5L2r7
oss:
endpoint: oss-cn-beijing.aliyuncs.com
roleArn: acs:ram::1189356150660127:role/uploadtest
roleSessionName: SessionTest
bucketName: plantidentificationpicture
入口类:
package com.example.myapplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//创造一个入口类
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}