java解決請求跨域的兩種方法

ckxllf發表於2019-08-23

  java解決請求跨域問題,有以下兩種寫法

  1.使用攔截器,實現javax.servlet.Filter介面

  import javax.servlet.Filter;

  import javax.servlet.FilterChain;

  import javax.servlet.FilterConfig;

  import javax.servlet.ServletException;

  import javax.servlet.ServletRequest;

  import javax.servlet.ServletResponse;

  import javax.servlet.annotation.WebFilter;

  import javax.servlet.http.HttpServletResponse;

  @WebFilter(filterName="CorsFilter" , urlPatterns="*.do")

  public class CorsFilter implements Filter{

  @Override

  public void destroy() {

  }

  @Override

  public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)

  throws IOException, ServletException {

  HttpServletResponse response = (HttpServletResponse) resp;

  response.setHeader("Access-Control-Allow-Origin", "*"); //解決跨域訪問報錯

  response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");

  response.setHeader("Access-Control-Max-Age", "3600"); //設定過期時間

  response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, client_id, uuid, Authorization");

  response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // 支援HTTP 1.1.

  response.setHeader("Pragma", "no-cache"); // 支援HTTP 1.0. response.setHeader("Expires", "0");

  chain.doFilter(req, resp);

  }

  @Override

  public void init(FilterConfig arg0) throws ServletException {

  }

  }

  如果想用xml的形式配置攔截器,可以刪除@WebFilter(filterName="CorsFilter" , urlPatterns="*.do"),然後在web.xml中新增下面的配置

  CorsFilter

  com.huaming.filter.CorsFilter

  CorsFilter

  *.do

  2.使用註解的形式,配置org.springframework.web.filter.CorsFilter,通常springboot專案多數使用這種方式

  import org.springframework.web.filter.CorsFilter;

  import org.springframework.context.annotation.Bean;

  import org.springframework.context.annotation.Configuration;

  import org.springframework.web.cors.CorsConfiguration;

  import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

  /** 無錫婦科醫院

  * 解決跨域問題springboot所需配置

  */

  @Configuration

  public class CORSConfiguration {

  @Bean

  public CorsFilter corsFilter() {

  //1.新增CORS配置資訊

  CorsConfiguration config = new CorsConfiguration();

  //1) 允許的域,不要寫*,否則cookie就無法使用了

  config.addAllowedOrigin("*");

  config.addAllowedHeader("*"); // 允許任何頭

  config.addAllowedMethod("*"); // 允許任何方法(post、get等)

  //2) 是否傳送Cookie資訊

  config.setAllowCredentials(true);

  //3) 允許的請求方式

  config.addAllowedMethod("OPTIONS");

  config.addAllowedMethod("HEAD");

  config.addAllowedMethod("GET");

  config.addAllowedMethod("PUT");

  config.addAllowedMethod("POST");

  config.addAllowedMethod("DELETE");

  config.addAllowedMethod("PATCH");

  // 4)允許的頭資訊

  config.addAllowedHeader("*");

  // 5)配置有效時長

  config.setMaxAge(3600*24L);

  //2.新增對映路徑,我們攔截一切請求

  UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();

  configSource.registerCorsConfiguration("/**", config);

  //3.返回新的CorsFilter.

  return new CorsFilter(configSource);

  }

  }


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69945560/viewspace-2654704/,如需轉載,請註明出處,否則將追究法律責任。

相關文章