fit:修复前端cors配置
This commit is contained in:
@@ -0,0 +1,13 @@
|
|||||||
|
package com.guo.learningprogresstracker.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Configuration
|
||||||
|
@ConfigurationProperties(prefix = "cors")
|
||||||
|
public class CorsProperties {
|
||||||
|
private String[] allowedOrigins;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,20 +1,27 @@
|
|||||||
package com.guo.learningprogresstracker.config;
|
package com.guo.learningprogresstracker.config;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
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;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class WebCorsConfig implements WebMvcConfigurer {
|
public class WebCorsConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
@Value("${cors.allowed-origins}")
|
private final CorsProperties corsProperties;
|
||||||
private String[] allowedOrigins; // 支持多个Origin配置
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addCorsMappings(CorsRegistry registry) {
|
public void addCorsMappings(CorsRegistry registry) {
|
||||||
|
log.info("允许cors的地址配置:{}", Arrays.toString(corsProperties.getAllowedOrigins()));
|
||||||
registry.addMapping("/**")
|
registry.addMapping("/**")
|
||||||
.allowedOrigins(allowedOrigins)
|
.allowedOrigins(corsProperties.getAllowedOrigins())
|
||||||
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
|
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
|
||||||
.allowedHeaders("*")
|
.allowedHeaders("*")
|
||||||
.allowCredentials(true);
|
.allowCredentials(true);
|
||||||
|
|||||||
@@ -9,4 +9,6 @@ server:
|
|||||||
port: 5157
|
port: 5157
|
||||||
|
|
||||||
cors:
|
cors:
|
||||||
allowed-origins: http://localhost:5158
|
allowed-origins:
|
||||||
|
- http://localhost:5158
|
||||||
|
- http://192.168.123.199:5158
|
||||||
Reference in New Issue
Block a user