java B2B2C Springcloud電子商城系統-springCloud跨域訪問

什麼是跨域?前端

須要JAVA Spring Cloud大型企業分佈式微服務雲構建的B2B2C電子商務平臺源碼 一零三八七七四六二六java

假設你在http://xxx.com/test/下有一個js文件,從這個js裏發出一個ajax請求請求後端服務,按照以下狀況斷定:nginx

解決方案:ajax

1) JSONP :後端

動態添加一個跨域

2) NGINX代理 :瀏覽器

經過一個代理服務器,將跨域的請求轉發,如:前端JS在http://www.demo.com/a.js,後端是http://www.abc.com/app/action,經過代理可將後端的地址轉換成http://www.demo/app/action,這樣,從前端發起的請求,就不存在跨域的狀況了bash

3)CORS服務器

而後CORS是支持全部類型的HTTP請求,而且也只是服務端進行設置便可,可是缺點就是老的瀏覽器不支持CORS(如:IE7,7,8,等)app

什麼是CORS?

Cross-origin resource sharing(跨域資源共享),是一個W3C標準,它容許你向一個不一樣源的服務器發出XMLHttpRequest請求,從而克服了ajax只能請求同源服務的限制.而且也能夠經過靈活的設置,來指定什麼樣的請求是能夠被受權的.

CORS的響應頭

Access-Control-Allow-Origin : 必須的,容許的域名,若是設置*,則表示接受任何域名

Access-Control-Allow-Credentials : 非必須的,表示是否容許發送Cookie,注意,當設置爲true的時候,客戶端的ajax請求,也須要將withCredentials屬性設置爲true

Access-Control-Expose-Headers : 非必須的,表示客戶端能拿到的header,默認狀況下XMLHttpRequest的getResponseHeader方法只能拿到幾個基本的header,若是有自定義的header要獲取的話,則須要設置此值

Access-Control-Request-Method : 必須的,表示CORS上會使用到那些HTTP方法

Access-Control-Request-Headers : 必須的,表示CORS上會有那些額外的的有信息

代碼實現:

@Component

public class SimpleCORSFilter implements Filter {

   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");

       chain.doFilter(req, res);

   }

   public void init(FilterConfig filterConfig) {}

   public void destroy() {}

}
複製代碼

也能夠經過nginx配置CORS:

#

# Wide-open CORS config for nginx

#

location / {

     if ($request_method = 'OPTIONS') {

        add_header 'Access-Control-Allow-Origin' '*';

        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

        #

        # Custom headers and headers various browsers *should* be OK with but aren't

        #

        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';

        #

        # Tell client that this pre-flight info is valid for 20 days

        #

        add_header 'Access-Control-Max-Age' 1728000;

        add_header 'Content-Type' 'text/plain; charset=utf-8';

        add_header 'Content-Length' 0;

        return 204;

     }

     if ($request_method = 'POST') {

        add_header 'Access-Control-Allow-Origin' '*';

        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';

        add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';

     }

     if ($request_method = 'GET') {

        add_header 'Access-Control-Allow-Origin' '*';

        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';

        add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';

     }

}
複製代碼

java B2B2C Springcloud電子商城系統

相關文章
相關標籤/搜索