yaobo 7 سال پیش
والد
کامیت
052c001e06
2فایلهای تغییر یافته به همراه51 افزوده شده و 51 حذف شده
  1. 38 38
      src/main/java/cn/efunbox/audio/aop/CorsFilter.java
  2. 13 13
      src/main/java/cn/efunbox/audio/config/Config.java

+ 38 - 38
src/main/java/cn/efunbox/audio/aop/CorsFilter.java

@@ -1,38 +1,38 @@
-package cn.efunbox.audio.aop;
-
-import org.springframework.stereotype.Component;
-
-import javax.servlet.*;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-
-/**
- *
- *  允许跨域访问过滤器
- * @author meng
- * @version
- * @since 2016年6月19日
- */
-@Component
-public class CorsFilter implements Filter {
-
-    final static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(CorsFilter.class);
-
-    @Override
-    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
-        HttpServletResponse response = (HttpServletResponse) res;
-
-        HttpServletRequest reqs = (HttpServletRequest) req;
-
-//        System.out.println("access from "+reqs.getHeader("Origin"));
-        response.setHeader("Access-Control-Allow-Origin",reqs.getHeader("Origin")); //直接用"*"无法解决跨域问题
-        response.setHeader("Access-Control-Allow-Credentials", "true");
-        response.setHeader("Access-Control-Allow-Methods", reqs.getHeader("Access-Control-Request-Method"));
-        response.setHeader("Access-Control-Max-Age", "3600");
-        response.setHeader("Access-Control-Allow-Headers", reqs.getHeader("Access-Control-Request-Headers"));
-        chain.doFilter(req, res);
-    }
-    public void init(FilterConfig filterConfig) {}
-    public void destroy() {}
-}
+//package cn.efunbox.audio.aop;
+//
+//import org.springframework.stereotype.Component;
+//
+//import javax.servlet.*;
+//import javax.servlet.http.HttpServletRequest;
+//import javax.servlet.http.HttpServletResponse;
+//import java.io.IOException;
+//
+///**
+// *
+// *  允许跨域访问过滤器
+// * @author meng
+// * @version
+// * @since 2016年6月19日
+// */
+//@Component
+//public class CorsFilter implements Filter {
+//
+//    final static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(CorsFilter.class);
+//
+//    @Override
+//    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
+//        HttpServletResponse response = (HttpServletResponse) res;
+//
+//        HttpServletRequest reqs = (HttpServletRequest) req;
+//
+////        System.out.println("access from "+reqs.getHeader("Origin"));
+//        response.setHeader("Access-Control-Allow-Origin",reqs.getHeader("Origin")); //直接用"*"无法解决跨域问题
+//        response.setHeader("Access-Control-Allow-Credentials", "true");
+//        response.setHeader("Access-Control-Allow-Methods", reqs.getHeader("Access-Control-Request-Method"));
+//        response.setHeader("Access-Control-Max-Age", "3600");
+//        response.setHeader("Access-Control-Allow-Headers", reqs.getHeader("Access-Control-Request-Headers"));
+//        chain.doFilter(req, res);
+//    }
+//    public void init(FilterConfig filterConfig) {}
+//    public void destroy() {}
+//}

+ 13 - 13
src/main/java/cn/efunbox/audio/config/Config.java

@@ -49,18 +49,18 @@ public class Config extends WebMvcConfigurerAdapter{
                 .excludePathPatterns("/device/**", "/admin/login", "/audio/search");
     }
 
-//    /**
-//     * 允许跨域访问
-//     * @param registry
-//     */
-//    @Override
-//    public void addCorsMappings(CorsRegistry registry) {
-//        registry.addMapping("/**")
-//                .allowedOrigins("*")
-//                .allowCredentials(true)
-//                .allowedMethods("GET", "POST", "DELETE", "PUT", "OPTIONS")
-//                .allowedHeaders("*")
-//                .maxAge(3600);
-//    }
+    /**
+     * 允许跨域访问
+     * @param registry
+     */
+    @Override
+    public void addCorsMappings(CorsRegistry registry) {
+        registry.addMapping("/**")
+                .allowedOrigins("*")
+                .allowCredentials(true)
+                .allowedMethods("GET", "POST", "DELETE", "PUT", "OPTIONS")
+                .allowedHeaders("*")
+                .maxAge(3600);
+    }
 
 }