|
@@ -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);
|
|
|
+ }
|
|
|
+}
|