Browse Source

配置optonal请求不拦截

yq 4 years ago
parent
commit
d7c0f4ac90

+ 33 - 0
fiveep-service/src/main/java/com/bizmatics/service/config/CorsFilter.java

@@ -0,0 +1,33 @@
+package com.bizmatics.service.config;
+
+import org.elasticsearch.rest.RestRequest;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.filter.OncePerRequestFilter;
+
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+/**
+ * @author yq
+ * @date 2021/7/26 13:58
+ */
+@Configuration
+public class CorsFilter extends OncePerRequestFilter {
+
+    @Override
+    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
+        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
+        httpServletResponse.setHeader("Access-Control-Allow-Methods",
+                "POST, GET, OPTIONS, DELETE");
+        httpServletResponse.setHeader("Access-Control-Max-Age", "3600");
+        httpServletResponse.setHeader("Access-Control-Allow-Headers",
+                "Content-Type, x-requested-with, X-Custom-Header, Request-Ajax");//允许自定义的请求头
+        if(httpServletRequest.getMethod().toUpperCase().equals(RestRequest.Method.OPTIONS.name())){
+            return;
+        }
+        filterChain.doFilter(httpServletRequest, httpServletResponse);
+    }
+}

+ 25 - 25
fiveep-service/src/main/java/com/bizmatics/service/config/MyWebConfigurer.java

@@ -1,25 +1,25 @@
-package com.bizmatics.service.config;
-
-import org.springframework.boot.SpringBootConfiguration;
-import org.springframework.web.servlet.config.annotation.CorsRegistry;
-import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
-
-@SpringBootConfiguration
-public class MyWebConfigurer implements WebMvcConfigurer {
-
-    @Override
-    public void addCorsMappings(CorsRegistry registry) {
-        // 设置允许跨域的路径
-        /**
-         * 所有请求都允许跨域,使用这种配置就不需要
-         * 在interceptor中配置header了
-         */
-        registry.addMapping("/**")
-                .allowCredentials(true)
-                .allowedOrigins("*")
-                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
-                .allowedHeaders("*")
-                .maxAge(3600);
-    }
-
-}
+//package com.bizmatics.service.config;
+//
+//import org.springframework.boot.SpringBootConfiguration;
+//import org.springframework.web.servlet.config.annotation.CorsRegistry;
+//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+//
+//@SpringBootConfiguration
+//public class MyWebConfigurer implements WebMvcConfigurer {
+//
+//    @Override
+//    public void addCorsMappings(CorsRegistry registry) {
+//        // 设置允许跨域的路径
+//        /**
+//         * 所有请求都允许跨域,使用这种配置就不需要
+//         * 在interceptor中配置header了
+//         */
+//        registry.addMapping("/**")
+//                .allowCredentials(true)
+//                .allowedOrigins("*")
+//                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
+//                .allowedHeaders("*")
+//                .maxAge(3600);
+//    }
+//
+//}