Skip to content

Commit a04c3b2

Browse files
committed
Add ACAH conf.
1 parent ff89f87 commit a04c3b2

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

EasyRest/src/main/java/tech/dbgsoftware/easyrest/network/NettyInit.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
import javax.net.ssl.SSLContext;
2424
import javax.net.ssl.SSLEngine;
2525
import java.lang.annotation.Annotation;
26-
import java.util.HashMap;
27-
import java.util.LinkedHashMap;
28-
import java.util.Map;
26+
import java.util.*;
2927
import java.util.function.Function;
3028

3129
public class NettyInit implements BaseConfiguration {
@@ -40,7 +38,8 @@ public class NettyInit implements BaseConfiguration {
4038
private SSLContext sslContext;
4139
private CustomizeChannel customizeChannel = new CustomizeChannel();
4240
private ChannelOptionBuilder channelOptionBuilder = new ChannelOptionBuilder().buildWithDefaultOptions();
43-
private Map<String, Object> customerProperties = new HashMap<>();
41+
private Map<String, Object> properties = new HashMap<>();
42+
private List<String> AccessControlAllowHeaders = Arrays.asList("DNT","X-Mx-ReqToken","Keep-Alive","User-Agent","X-Requested-With","If-Modified-Since","Cache-Control","Content-Type","Authorization");
4443
private ChannelFuture channelFuture;
4544
private static final Logger LOGGER = LoggerFactory.getLogger(NettyInit.class);
4645

@@ -127,7 +126,7 @@ public NettyInit setMaxContentLength(int maxContentLength) {
127126
}
128127

129128
public NettyInit addConfigurations(String key, Object value){
130-
customerProperties.put(key, value);
129+
properties.put(key, value);
131130
return this;
132131
}
133132

@@ -152,8 +151,13 @@ protected ChannelFuture bindChannelFuture(ChannelFuture channelFuture){
152151
}
153152

154153
@Override
155-
public Map<String, Object> getCustomerProperties() {
156-
return customerProperties;
154+
public Map<String, Object> getProperties() {
155+
return properties;
156+
}
157+
158+
@Override
159+
public List<String> getAccessControlAllowHeaders() {
160+
return AccessControlAllowHeaders;
157161
}
158162

159163
public int getPort() {

EasyRest/src/main/java/tech/dbgsoftware/easyrest/network/core/api/BaseConfiguration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package tech.dbgsoftware.easyrest.network.core.api;
22

3+
import java.util.List;
34
import java.util.Map;
45

56
public interface BaseConfiguration {
@@ -14,6 +15,8 @@ public interface BaseConfiguration {
1415

1516
int getMaxContentLength();
1617

17-
Map<String, Object> getCustomerProperties();
18+
Map<String, Object> getProperties();
19+
20+
List<String> getAccessControlAllowHeaders();
1821

1922
}

EasyRest/src/main/java/tech/dbgsoftware/easyrest/network/core/pipeline/in/RequestProcessHandler.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package tech.dbgsoftware.easyrest.network.core.pipeline.in;
22

33
import akka.actor.ActorRef;
4+
import io.netty.channel.ChannelHandler;
5+
import io.netty.channel.ChannelHandlerContext;
6+
import io.netty.channel.SimpleChannelInboundHandler;
7+
import io.netty.handler.codec.http.FullHttpRequest;
48
import tech.dbgsoftware.easyrest.actors.ActorFactory;
59
import tech.dbgsoftware.easyrest.actors.request.RequestProcessActor;
610
import tech.dbgsoftware.easyrest.model.HttpEntity;
711
import tech.dbgsoftware.easyrest.model.Response;
812
import tech.dbgsoftware.easyrest.model.request.Request;
913
import tech.dbgsoftware.easyrest.network.core.api.BaseConfiguration;
10-
import io.netty.channel.ChannelHandler;
11-
import io.netty.channel.ChannelHandlerContext;
12-
import io.netty.channel.SimpleChannelInboundHandler;
13-
import io.netty.handler.codec.http.FullHttpRequest;
1414

1515
/**
1616
* The rest entrance
@@ -42,10 +42,13 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
4242
private HttpEntity createNewRequestEntity(ChannelHandlerContext channelHandlerContext, FullHttpRequest fullHttpRequest){
4343
Request request = new Request(fullHttpRequest);
4444
Response response = new Response();
45+
StringBuilder headers = new StringBuilder();
46+
baseConfiguration.getAccessControlAllowHeaders().forEach((header) -> headers.append(header).append(","));
47+
headers.deleteCharAt(headers.length() - 1);
4548
HttpEntity httpEntity = new HttpEntity(request, response, channelHandlerContext);
4649
httpEntity.getResponse().getResponseHeaders().put("Access-Control-Allow-Origin", baseConfiguration.getHost().equalsIgnoreCase("*") ? "*" : (baseConfiguration.getHost() + ":" + baseConfiguration.getPort()));
4750
httpEntity.getResponse().getResponseHeaders().put("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE");
48-
httpEntity.getResponse().getResponseHeaders().put("Access-Control-Allow-Headers", "DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization");
51+
httpEntity.getResponse().getResponseHeaders().put("Access-Control-Allow-Headers", headers.toString());
4952
return httpEntity;
5053
}
5154

Example/Example-Quick-Start/src/main/java/com/example/rest/TestRestService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import tech.dbgsoftware.easyrest.annotations.method.BindURL;
44
import tech.dbgsoftware.easyrest.annotations.method.Get;
55
import tech.dbgsoftware.easyrest.annotations.method.Post;
6+
import tech.dbgsoftware.easyrest.annotations.method.SkipCustomerInject;
67
import tech.dbgsoftware.easyrest.annotations.parameter.AllDefined;
78
import tech.dbgsoftware.easyrest.aop.customer.CustomInjection;
89
import tech.dbgsoftware.easyrest.exception.PermissionException;
@@ -12,6 +13,7 @@
1213
public interface TestRestService extends CustomInjection {
1314

1415
@Get
16+
@SkipCustomerInject
1517
String ping();
1618

1719
@Post

0 commit comments

Comments
 (0)