cat_dog_orange 2023-04-05 19:45 采纳率: 36.4%
浏览 34
已结题

使用Android Studio开发Spring Boot项目,获取到阿里云OSS中的STS值时,报错

使用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);
    }
}

  • 写回答

1条回答 默认 最新

  • 程序yang 全栈领域优质创作者 2023-04-06 08:43
    关注

    在build.gradle文件中加入这个依赖试试:

    implementation 'org.springframework:spring-web:5.3.14'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月7日
  • 已采纳回答 4月7日
  • 创建了问题 4月5日

悬赏问题

  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低