Skip to content

Commit

Permalink
cross-domain configuration passed
Browse files Browse the repository at this point in the history
  • Loading branch information
SleepyLGod committed Nov 2, 2022
1 parent 4183d40 commit 0fe4787
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
@SpringBootApplication
public class TranscriptionApplication {
public static void main(String[] args) {
// 注:这里传入的字段码对象,必需是声明了@SpringBootApplication的类
//启动SpringBoot程序
SpringApplication.run(TranscriptionApplication.class, args);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.pianotranscriptioncli.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // 项目中的所有接口都支持跨域
.allowedOriginPatterns("*") // 所有地址都可以访问,也可以配置具体地址
.allowCredentials(true)
.allowedMethods("*") // "GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"
.maxAge(360000); // 跨域允许时间
}

@Bean
public CorsFilter corsFilter() {
// 1.添加CORS配置信息
CorsConfiguration config = new CorsConfiguration();
// 放行哪些原始域
config.addAllowedOrigin("*");
// 是否发送Cookie信息
config.setAllowCredentials(true);
// 放行哪些原始域(请求方式)
config.addAllowedMethod("*");
// 放行哪些原始域(头部信息)
config.addAllowedHeader("*");
// 暴露哪些头部信息(因为跨域访问默认不能获取全部头部信息)
config.addExposedHeader("*");

// 2.添加映射路径
UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
configSource.registerCorsConfiguration("/**", config);

// 3.返回新的CorsFilter.
return new CorsFilter(configSource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.IOException;

@RestController
// @CrossOrigin(origins = "*", maxAge = 360000) // 不能和CorsConfig同时使用
@RequestMapping("/transcription")
public class TranscriptionController {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# 模拟发送POST测试,运行环境设置为DEV

###
POST {{host}}/mp3ToMidiWithFile
Content-Type: multipart/form-data; boundary=WebAppBoundary
Expand Down

0 comments on commit 0fe4787

Please sign in to comment.