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电子商城系统

相关文章
相关标签/搜索