真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

SpringBoot實(shí)現(xiàn)跨域訪問實(shí)現(xiàn)代碼

當(dāng)前使用spring版本是4.3.9

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名與空間、網(wǎng)頁(yè)空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、德安網(wǎng)站維護(hù)、網(wǎng)站推廣。

import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
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 {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    response.setHeader("Access-Control-Allow-Origin", request.getHeader("origin"));
//    response.setHeader("Access-Control-Allow-Origin", "*");//允許跨域訪問的域
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");//允許使用的請(qǐng)求方法,以逗號(hào)隔開
    response.setHeader("Access-Control-Max-Age", "3600");// 緩存此次請(qǐng)求的秒數(shù)
    //允許使用的請(qǐng)求方法,以逗號(hào)隔開
    response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Cache-Control,Pragma,Content-Type,Token");
    response.setHeader("Access-Control-Allow-Credentials","true");//是否允許請(qǐng)求帶有驗(yàn)證信息
    filterChain.doFilter(servletRequest, servletResponse);
  }
  @Override
  public void destroy() {
  }
}

PS: spring boot 服務(wù)器端設(shè)置允許跨域訪問

 import org.springframework.stereotype.Component; 
import javax.servlet.*; 
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); 
  public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { 
    HttpServletResponse response = (HttpServletResponse) res; 
    response.setHeader("Access-Control-Allow-Origin", "*"); 
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); 
    response.setHeader("Access-Control-Max-Age", "3600"); 
    response.setHeader("Access-Control-Allow-Headers", "x-requested-with"); 
    System.out.println("*********************************過濾器被使用**************************"); 
    chain.doFilter(req, res); 
  } 
  public void init(FilterConfig filterConfig) {} 
  public void destroy() {} 
} 

2017-04-13更新:

第二種方法,在Appplication.java添加:

private CorsConfiguration buildConfig() { 
    CorsConfiguration corsConfiguration = new CorsConfiguration(); 
    corsConfiguration.addAllowedOrigin("*"); 
    corsConfiguration.addAllowedHeader("*"); 
    corsConfiguration.addAllowedMethod("*"); 
    return corsConfiguration; 
  } 
  /** 
   * 跨域過濾器 
   * @return 
   */ 
  @Bean 
  public CorsFilter corsFilter() { 
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); 
    source.registerCorsConfiguration("/**", buildConfig()); // 4 
    return new CorsFilter(source); 
  } 

總結(jié)

以上所述是小編給大家介紹的Spring Boot實(shí)現(xiàn)跨域訪問實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!


網(wǎng)站標(biāo)題:SpringBoot實(shí)現(xiàn)跨域訪問實(shí)現(xiàn)代碼
文章轉(zhuǎn)載:http://weahome.cn/article/jcodsp.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部