apache / dubbo-rpc-jsonrpc

The Json rpc module of Apache Dubbo project
https://dubbo.apache.org
Apache License 2.0
189 stars 101 forks source link

关于 跨域支持 #7

Closed shevawen closed 8 years ago

shevawen commented 8 years ago

想直接将服务抛给 浏览器 里的 JS,需要跨域支持,我试着这么做了

if (request.getMethod().equalsIgnoreCase("OPTIONS")) {
    response.setHeader(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
    response.setHeader(CrossOriginFilter.ACCESS_CONTROL_ALLOW_METHODS_HEADER, "POST");
    response.setHeader(CrossOriginFilter.ACCESS_CONTROL_ALLOW_HEADERS_HEADER, "Content-Type");
    response.setStatus(200);
} else if (request.getMethod().equalsIgnoreCase("POST")) {
    response.setHeader(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
    RpcContext.getContext().setRemoteAddress(request.getRemoteAddr(), request.getRemotePort());
    try {
        skeleton.handle(request.getInputStream(), response.getOutputStream());
    } catch (Throwable e) {
        throw new ServletException(e);
    }
} else {
    response.setStatus(500);
}

是可以的,但是我需要一个开关,可以放到protocol配置里么?像下面(我是用js做ioc启动的)

protocol : {
    type : 'com.alibaba.dubbo.config.ProtocolConfig',
    fields : {
        name : 'jsonrpc',
        port : 9090,
        server : 'jetty',
        cors: true
    }
}
binlaniua commented 8 years ago

我可以认为你是准备把dubbo接口直接暴露给前端JS吗,如果是这样,我还是建议,在dubbo的外面隔离一层,用Nginx或者apache,都可以

shevawen commented 8 years ago

@binlaniua 我确实是这么想的,如你所言,前面加个转发比较好。 但我还是想问下如果想加一个开关如何做,对dubbo的url机制还不太懂。

wuwen5 commented 8 years ago

com.alibaba.dubbo.common.Url 对象可以直接取参数吧

url.getParameter(Constants.XX_KEY, Constants.DEFAULT_XX) 是这样么?