package com.example.oss.config; import com.example.oss.util.OssUtils; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; /** * OSS Bucket CORS 初始化 *
* 应用启动时自动配置 OSS 跨域规则,解决前端直传跨域问题。 * 需在配置中开启:aliyun.oss.cors-enabled=true *
* *也可以不使用此自动配置,改为在阿里云控制台手动设置 CORS。
*/ @Slf4j @Component @Order(100) // 较晚执行,确保其他组件已就绪 @RequiredArgsConstructor public class OssCorsInitRunner implements ApplicationRunner { private final OssUtils ossUtils; @Override public void run(ApplicationArguments args) { ossUtils.configureBucketCors(); } }