NutzCN Logo
问答 跨域设置问题~~~~~~~~~~~~~
发布于 381天前 作者 qq_868910ac 424 次浏览 复制 上一个帖子 下一个帖子
标签:
web.xml
<filter>
        <filter-name>CrossOrigin</filter-name>
        <filter-class>cn.wizzer.common.filter.CrossOriginFilter</filter-class>
        <init-param>
            <param-name>AccessControlAllowOrigin</param-name>
            <param-value>*</param-value>
        </init-param>
        <init-param>
            <param-name>AccessControlAllowMethods</param-name>
            <param-value>POST, GET, DELETE, PUT</param-value>
        </init-param>
        <init-param>
            <param-name>AccessControlMaxAge</param-name>
            <param-value>3628800</param-value>
        </init-param>
        <init-param>
            <param-name>AccessControlAllowHeaders</param-name>
            <param-value>x-requested-with</param-value>
        </init-param>
    </filter>

package cn.wizzer.common.filter;

import java.io.IOException;

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.http.HttpServletResponse;

/**
 * 
 * @author wwhhf
 * @since 2016年5月30日
 * @comment 跨域过滤器
 */
public class CrossOriginFilter implements Filter {

    private FilterConfig config = null;

    @Override
    public void init(FilterConfig config) throws ServletException {
        this.config = config;
    }

    @Override
    public void destroy() {
        this.config = null;
    }

    /**
     * 
     * @author wwhhf
     * @since 2016/5/30
     * @comment 跨域的设置
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        // 表明它允许"http://xxx"发起跨域请求
        httpResponse.setHeader("Access-Control-Allow-Origin",
        		config.getInitParameter("AccessControlAllowOrigin"));
        // 表明在xxx秒内,不需要再发送预检验请求,可以缓存该结果
        httpResponse.setHeader("Access-Control-Allow-Methods",
                config.getInitParameter("AccessControlAllowMethods"));
        // 表明它允许xxx的外域请求
        httpResponse.setHeader("Access-Control-Max-Age",
                config.getInitParameter("AccessControlMaxAge"));
        // 表明它允许跨域请求包含xxx头
        httpResponse.setHeader("Access-Control-Allow-Headers",
                config.getInitParameter("AccessControlAllowHeaders"));
        chain.doFilter(request, response);
    }

}
但是请求的时候还是会报 已拦截跨源请求:同源策略禁止读取位于 http://127.0.0.1:8080/NutzWk-bootstrap-3.3.x/platform/cms/article/sendPropertyMessages 的远程资源。(原因:CORS 头 'Access-Control-Allow-Origin' 不匹配 '*, *')
3 回复

用域名测试

@wendal
$.ajax({
url: "http://127.0.0.1:8080/NutzWk-bootstrap-3.3.x/platform/cms/article/sendPropertyMessages",
contentType: "application/json;charset=utf-8",
type: "POST",
data: JSON.stringify({

                 "cms_article": {
                   "title": "test"
                 },

              }),
         dataType: "json",
         cache: false,
         success: function(message) {

         }
       });

httpResponse.setHeader("Access-Control-Allow-Origin","http://127.0.0.1:8080"); 这样吗
还是报
已拦截跨源请求:同源策略禁止读取位于 http://127.0.0.1:8080/NutzWk-bootstrap-3.3.x/platform/cms/article/sendPropertyMessages 的远程资源。(原因:CORS 头 'Access-Control-Allow-Origin' 不匹配 'http://127.0.0.1:8080, *')。

nutz有CrossOriginFilter

我说的是, 不要用127.0.0.1这样的地址测试

添加回复
请先登陆
回到顶部