Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
Server config for rest cors. (#155)
Browse files Browse the repository at this point in the history
* server config for rest cors

* add test case for cors
  • Loading branch information
leizhiyuan committed Feb 22, 2019
1 parent 1951dd4 commit 70c2d99
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public class SofaBootRpcProperties {
private String restPort;
private String restIoThreadSize;
private String restContextPath;

/**
* cors settings
*/
private String restAllowedOrigins;
// has no use
private String restThreadPoolCoreSize;
private String restThreadPoolMaxSize;
Expand Down Expand Up @@ -553,6 +558,15 @@ public void setHystrixEnable(String hystrixEnable) {
this.hystrixEnable = hystrixEnable;
}

public String getRestAllowedOrigins() {
return StringUtils.isEmpty(restAllowedOrigins) ? getDotString(new Object() {
}.getClass().getEnclosingMethod().getName()) : restAllowedOrigins;
}

public void setRestAllowedOrigins(String restAllowedOrigins) {
this.restAllowedOrigins = restAllowedOrigins;
}

private String getDotString(String enclosingMethodName) {
if (environment == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
import com.alipay.sofa.rpc.boot.config.SofaBootRpcConfigConstants;
import com.alipay.sofa.rpc.boot.config.SofaBootRpcProperties;
import com.alipay.sofa.rpc.boot.log.SofaBootRpcLoggerFactory;
import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.config.ServerConfig;
import com.alipay.sofa.rpc.server.bolt.BoltServer;
import org.slf4j.Logger;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;
Expand Down Expand Up @@ -298,6 +300,7 @@ ServerConfig createRestServerConfig() {
String telnetStr = sofaBootRpcProperties.getRestTelnet();
String daemonStr = sofaBootRpcProperties.getRestDaemon();

String allowedOrigins = sofaBootRpcProperties.getRestAllowedOrigins();
int port;
int ioThreadCount;
int restThreadPoolMaxSize;
Expand Down Expand Up @@ -345,13 +348,20 @@ ServerConfig createRestServerConfig() {
daemon = Boolean.parseBoolean(daemonStr);
}

Map<String, String> parameters = new HashMap<String, String>();

if (StringUtils.hasText(allowedOrigins)) {
parameters.put(RpcConstants.ALLOWED_ORIGINS, allowedOrigins);
}

ServerConfig serverConfig = new ServerConfig()
.setPort(port)
.setIoThreads(ioThreadCount)
.setMaxThreads(restThreadPoolMaxSize)
.setPayload(maxRequestSize)
.setTelnet(telnet)
.setDaemon(daemon);
.setDaemon(daemon)
.setParameters(parameters);

if (!StringUtils.isEmpty(contextPath)) {
serverConfig.setContextPath(contextPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.alipay.sofa.rpc.boot.config;

import com.alipay.sofa.rpc.boot.container.ConsumerConfigContainer;
import com.alipay.sofa.rpc.common.RpcOptions;
import com.alipay.sofa.rpc.config.ConsumerConfig;
import com.alipay.sofa.rpc.test.bean.SampleFacade;
import com.alipay.sofa.runtime.api.annotation.SofaReference;
Expand All @@ -29,7 +28,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

import java.lang.reflect.Field;
Expand All @@ -41,7 +39,8 @@
SofaBootRpcProperties.PREFIX + ".bolt.port=5000",
"com_alipay_sofa_rpc_bolt_thread_pool_max_size=600",
SofaBootRpcProperties.PREFIX + ".registries.zk1=zookeeper://xxxx",
SofaBootRpcProperties.PREFIX + ".consumer.repeated.reference.limit=10"
SofaBootRpcProperties.PREFIX + ".consumer.repeated.reference.limit=10",
SofaBootRpcProperties.PREFIX + ".rest.allowed.origins=a.com"
})
public class SofaBootRpcPropertiesTest {
@Autowired
Expand Down Expand Up @@ -97,4 +96,10 @@ public void testCustoMapConfig() {
Assert.assertEquals("zookeeper://xxxx", map.get("zk1"));
}

@Test
public void testAllowedOriginis() {
String result = sofaBootRpcProperties.getRestAllowedOrigins();
Assert.assertEquals("a.com", result);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException;
import com.alipay.sofa.rpc.boot.config.SofaBootRpcConfigConstants;
import com.alipay.sofa.rpc.boot.config.SofaBootRpcProperties;
import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.config.ServerConfig;
import com.alipay.sofa.rpc.test.base.ActivelyDestroyTest;
import org.junit.Assert;
Expand Down Expand Up @@ -102,7 +103,7 @@ public void testRestServerConfiguration() {
sofaBootRpcProperties.setRestMaxRequestSize("1000");
sofaBootRpcProperties.setRestTelnet("true");
sofaBootRpcProperties.setRestDaemon("true");

sofaBootRpcProperties.setRestAllowedOrigins("a.com");
ServerConfig serverConfig = serverConfigContainer
.createRestServerConfig();

Expand All @@ -114,6 +115,8 @@ public void testRestServerConfiguration() {
Assert.assertEquals(1000, serverConfig.getPayload());
Assert.assertTrue(serverConfig.isTelnet());
Assert.assertTrue(serverConfig.isDaemon());
Assert.assertEquals("a.com", serverConfig.getParameters().get(RpcConstants.ALLOWED_ORIGINS));

}

@Test
Expand Down

0 comments on commit 70c2d99

Please sign in to comment.