servlet解决跨域


底层使用Java代码解决跨域问题,你可以通过配置CorsFilter或者自定义Filter来实现。

下面是使用Java代码解决跨域问题的示例:

  1. 创建一个名为CorsFilter的Java类,并实现javax.servlet.Filter接口。

    import javax.servlet.*;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class CorsFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            // 初始化操作(可选)
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            HttpServletResponse response = (HttpServletResponse) servletResponse;
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
            response.setHeader("Access-Control-Allow-Headers", "Content-Type,Authorization");
            response.setHeader("Access-Control-Allow-Credentials", "true");
            response.setHeader("Access-Control-Max-Age", "3600");
    
            filterChain.doFilter(servletRequest, servletResponse);
        }
    
        @Override
        public void destroy() {
            // 销毁操作(可选)
        }
    }

    在上述示例中,我们在doFilter方法中设置了一些常见的跨域请求头,如Access-Control-Allow-OriginAccess-Control-Allow-MethodsAccess-Control-Allow-Headers等。你可以根据需求进行自定义。

  2. 在你的Spring Boot应用中注册该过滤器。

    • 如果你使用的是基于Servlet的容器(如Tomcat),可以在ServletInitializer类的onStartup方法中进行注册:

      import org.springframework.boot.web.servlet.ServletContextInitializer;
      import org.springframework.stereotype.Component;
      
      import javax.servlet.DispatcherType;
      import javax.servlet.FilterRegistration;
      import javax.servlet.ServletContext;
      import java.util.EnumSet;
      
      @Component
      public class ServletInitializer implements ServletContextInitializer {
          @Override
          public void onStartup(ServletContext servletContext) throws ServletException {
              FilterRegistration.Dynamic corsFilter = servletContext.addFilter("corsFilter", CorsFilter.class);
              corsFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
          }
      }
    • 如果你使用的是基于Spring Web的容器(如Spring Boot内嵌的Tomcat),可以在WebMvcConfigurer的实现类中进行注册:

      import org.springframework.context.annotation.Configuration;
      import org.springframework.web.servlet.config.annotation.CorsRegistry;
      import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
      
      @Configuration
      public class WebConfig implements WebMvcConfigurer {
          @Override
          public void addCorsMappings(CorsRegistry registry) {
              registry.addMapping("/**")
                      .allowedOrigins("*")
                      .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                      .allowedHeaders("Content-Type", "Authorization")
                      .allowCredentials(true)
                      .maxAge(3600);
          }
      }

      在上述示例中,我们通过重写addCorsMappings方法来配置跨域请求。

通过以上的方式,你可以在底层使用Java代码解决Spring Boot应用的跨域问题。这样,无论是基于Servlet的容器还是基于Spring Web的容器,都能够正确处理跨域请求。


Author: Acaibird
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Acaibird !
  TOC