目 录CONTENT

文章目录

Spring 阿里云OSS 跨域Cors配置

不争
2024-01-02 / 0 评论 / 0 点赞 / 78 阅读 / 7598 字

Spring 阿里云OSS 跨域Cors配置

1.导入依赖

        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.8.0</version>
        </dependency>
  1. 配置文件(id和key从控制台找)
@Slf4j
public class UploadUtil {
    public static String upload(Part part) {
        // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-guangzhou.aliyuncs.com";

        // 填写Bucket名称,例如examplebucket。
        String bucketName = "javasm20";

        // 文件名
        String fileName = part.getSubmittedFileName();

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, "id", "key");

        try {
            // 文件流对象
            InputStream inputStream = part.getInputStream();
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, inputStream);
            // 创建PutObject请求
            PutObjectResult result = ossClient.putObject(putObjectRequest);
        } catch (OSSException oe) {
            log.info("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            log.info("Error Message:" + oe.getErrorMessage());
            log.info("Error Code:" + oe.getErrorCode());
            log.info("Request ID:" + oe.getRequestId());
            log.info("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            log.info("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            log.info("Error Message:" + ce.getMessage());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return "https://javasm200.oss-cn-guangzhou.aliyuncs.com/" + fileName;
    }
}

3.Controller层

    @PostMapping("upload")
    public AxiosResult upload(@RequestPart Part imgFile){
        String upload = UploadUtil.upload(imgFile);
        return AxiosResult.success(upload);
    }

Cors跨域配置

@Configuration
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration corsConfiguration = new CorsConfiguration();
        /*是否允许请求带有验证信息*/
        corsConfiguration.setAllowCredentials(true);
        /*允许访问的客户端域名*/
        corsConfiguration.addAllowedOrigin("*");
//        corsConfiguration.addAllowedOriginPattern("*");
        /*允许服务端访问的客户端请求头*/
        corsConfiguration.addAllowedHeader("*");
        /*允许访问的方法名,GET POST等*/
        corsConfiguration.addAllowedMethod("*");
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(urlBasedCorsConfigurationSource);
    }
}
0

评论区