添加 CORS 跨源支持,用于 浏览器 直接访问 发布的服务 11/head
authorshevawen <sheva.wen@gmail.com>
Fri, 17 Jun 2016 04:07:42 +0000 (12:07 +0800)
committershevawen <sheva.wen@gmail.com>
Fri, 17 Jun 2016 04:07:42 +0000 (12:07 +0800)
README.md
src/main/java/com/ofpay/dubbo/rpc/protocol/jsonrpc/JsonRpcProtocol.java

index cb89548..9b3f37f 100644 (file)
--- a/README.md
+++ b/README.md
@@ -103,6 +103,12 @@ web.xml:
 协议的端口```<dubbo:protocol port="8080" />```必须与servlet容器的端口相同,
 协议的上下文路径```<dubbo:protocol contextpath="foo" />```必须与servlet应用的上下文路径相同。
 
+CORS跨源支持:
+```xml
+<dubbo:protocol name="jsonrpc" ...  />
+       <dubbo:parameter key="cors" value="true" />
+</dubbo:protocol>
+```
 --------------
 ## Example
 
@@ -161,6 +167,8 @@ https://github.com/ofpay/dubbo-node-client
 https://github.com/JoeCao/dubbo_jsonrpc_example  
 使用docker运行
 
+## 浏览器调用
+需按前述开启CORS支持, 可使用 https://github.com/datagraph/jquery-jsonrpc
 
 ## 文档资料
 
index bda7d1d..fe9b0f0 100644 (file)
@@ -1,5 +1,17 @@
 package com.ofpay.dubbo.rpc.protocol.jsonrpc;
 
+import java.io.IOException;
+import java.net.SocketTimeoutException;
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.springframework.remoting.RemoteAccessException;
+
 import com.alibaba.dubbo.common.URL;
 import com.alibaba.dubbo.remoting.http.HttpBinder;
 import com.alibaba.dubbo.remoting.http.HttpHandler;
@@ -11,21 +23,15 @@ import com.googlecode.jsonrpc4j.HttpException;
 import com.googlecode.jsonrpc4j.JsonRpcClientException;
 import com.googlecode.jsonrpc4j.JsonRpcServer;
 import com.googlecode.jsonrpc4j.spring.JsonProxyFactoryBean;
-import org.springframework.remoting.RemoteAccessException;
-
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-import java.net.SocketTimeoutException;
-import java.util.ArrayList;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * Created by wuwen on 15/4/1.
  */
 public class JsonRpcProtocol extends AbstractProxyProtocol {
+    
+    public static final String ACCESS_CONTROL_ALLOW_ORIGIN_HEADER = "Access-Control-Allow-Origin";
+    public static final String ACCESS_CONTROL_ALLOW_METHODS_HEADER = "Access-Control-Allow-Methods";
+    public static final String ACCESS_CONTROL_ALLOW_HEADERS_HEADER = "Access-Control-Allow-Headers";
 
     private final Map<String, HttpServer> serverMap = new ConcurrentHashMap<>();
 
@@ -33,6 +39,8 @@ public class JsonRpcProtocol extends AbstractProxyProtocol {
 
     private HttpBinder httpBinder;
 
+    private boolean cors;
+
     public JsonRpcProtocol() {
         super(HttpException.class, JsonRpcClientException.class);
     }
@@ -51,15 +59,23 @@ public class JsonRpcProtocol extends AbstractProxyProtocol {
                 throws IOException, ServletException {
             String uri = request.getRequestURI();
             JsonRpcServer skeleton = skeletonMap.get(uri);
-            if (! request.getMethod().equalsIgnoreCase("POST")) {
-                response.setStatus(500);
-            } else {
+            if (cors) {
+                response.setHeader(ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
+                response.setHeader(ACCESS_CONTROL_ALLOW_METHODS_HEADER, "POST");
+                response.setHeader(ACCESS_CONTROL_ALLOW_HEADERS_HEADER, "*");
+            }
+            if (request.getMethod().equalsIgnoreCase("OPTIONS")) {
+                response.setStatus(200);
+            } else if (request.getMethod().equalsIgnoreCase("POST")) {
+
                 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);
             }
         }
 
@@ -72,6 +88,7 @@ public class JsonRpcProtocol extends AbstractProxyProtocol {
             server = httpBinder.bind(url, new InternalHandler());
             serverMap.put(addr, server);
         }
+        cors = url.getParameter("cors", false);
         final String path = url.getAbsolutePath();
         JsonRpcServer skeleton = new JsonRpcServer(impl, type);
         skeletonMap.put(path, skeleton);