feat:增加前端cors配置
This commit is contained in:
@@ -1,17 +1,22 @@
|
|||||||
package com.guo.learningprogresstracker.config;
|
package com.guo.learningprogresstracker.config;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class WebCorsConfig implements WebMvcConfigurer {
|
public class WebCorsConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
|
@Value("${cors.allowed-origins}")
|
||||||
|
private String[] allowedOrigins; // 支持多个Origin配置
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addCorsMappings(CorsRegistry registry) {
|
public void addCorsMappings(CorsRegistry registry) {
|
||||||
registry.addMapping("/**") // 配置所有路径
|
registry.addMapping("/**")
|
||||||
.allowedOrigins("http://localhost:5173")
|
.allowedOrigins(allowedOrigins)
|
||||||
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
|
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
|
||||||
.allowedHeaders("*")
|
.allowedHeaders("*")
|
||||||
.allowCredentials(true);
|
.allowCredentials(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,3 +7,8 @@ spring:
|
|||||||
password: GUOxy5157
|
password: GUOxy5157
|
||||||
server:
|
server:
|
||||||
port: 5157
|
port: 5157
|
||||||
|
|
||||||
|
cors:
|
||||||
|
allowed-origins:
|
||||||
|
- http://192.168.123.199:5158
|
||||||
|
- http://39.105.149.197:5158
|
||||||
|
|||||||
@@ -7,3 +7,6 @@ spring:
|
|||||||
password:
|
password:
|
||||||
server:
|
server:
|
||||||
port: 5157
|
port: 5157
|
||||||
|
|
||||||
|
cors:
|
||||||
|
allowed-origins: http://localhost:5158
|
||||||
Reference in New Issue
Block a user