|
@@ -1,24 +1,43 @@
|
|
package com.caimei365.manager.config;
|
|
package com.caimei365.manager.config;
|
|
|
|
|
|
|
|
+import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Bean;
|
|
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.WebMvcConfigurer;
|
|
|
|
|
|
+import org.springframework.core.Ordered;
|
|
|
|
+import org.springframework.core.annotation.Order;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.web.cors.CorsConfiguration;
|
|
|
|
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
|
|
|
|
|
-@Configuration
|
|
|
|
-public class CorsConfig {
|
|
|
|
- @Bean
|
|
|
|
- public WebMvcConfigurer corsConfigurer() {
|
|
|
|
- return new WebMvcConfigurer() {
|
|
|
|
- @Override
|
|
|
|
- public void addCorsMappings(CorsRegistry registry) {
|
|
|
|
- registry.addMapping("/**")
|
|
|
|
- .allowedOrigins("*")
|
|
|
|
- .allowedMethods("GET", "POST", "PUT", "DELETE")
|
|
|
|
- .allowedHeaders("*")
|
|
|
|
- .allowedOriginPatterns("*")
|
|
|
|
- .maxAge(3600);
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
|
|
+import javax.servlet.*;
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Description
|
|
|
|
+ *
|
|
|
|
+ * @author : Charles
|
|
|
|
+ * @date : 2023/9/21
|
|
|
|
+ */
|
|
|
|
+@Component
|
|
|
|
+@Order(Ordered.HIGHEST_PRECEDENCE)
|
|
|
|
+public class CorsConfig implements Filter {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
|
|
|
|
+ HttpServletResponse response = (HttpServletResponse) res;
|
|
|
|
+ HttpServletRequest request = (HttpServletRequest) req;
|
|
|
|
+ response.setHeader("Access-Control-Allow-Origin", "*"); // 允许的前端地址
|
|
|
|
+ response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); // 允许的 HTTP 方法
|
|
|
|
+ response.setHeader("Access-Control-Allow-Headers", "*"); // 允许的请求头
|
|
|
|
+ response.setHeader("Access-Control-Max-Age", "3600");
|
|
|
|
+ if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
|
|
|
|
+ response.setStatus(HttpServletResponse.SC_OK);
|
|
|
|
+ } else {
|
|
|
|
+ chain.doFilter(req, res);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|