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

使用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日

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理