From e90071e8cfe24dc237c3878fd0afe78c7f0cca00 Mon Sep 17 00:00:00 2001 From: zd925 <50254279+zd925@users.noreply.github.com> Date: Fri, 22 Mar 2024 10:31:52 +0800 Subject: [PATCH] update Configuration cors --- .../socketio/Configuration.java | 27 +++++++++++++++--- .../socketio/handler/EncoderHandler.java | 28 ++++++++++--------- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/corundumstudio/socketio/Configuration.java b/src/main/java/com/corundumstudio/socketio/Configuration.java index 601f2207..b68fc84d 100644 --- a/src/main/java/com/corundumstudio/socketio/Configuration.java +++ b/src/main/java/com/corundumstudio/socketio/Configuration.java @@ -15,10 +15,6 @@ */ package com.corundumstudio.socketio; -import java.io.InputStream; -import java.util.Arrays; -import java.util.List; - import com.corundumstudio.socketio.handler.SuccessAuthorizationListener; import com.corundumstudio.socketio.listener.DefaultExceptionListener; import com.corundumstudio.socketio.listener.ExceptionListener; @@ -27,6 +23,9 @@ import com.corundumstudio.socketio.store.StoreFactory; import javax.net.ssl.KeyManagerFactory; +import java.io.InputStream; +import java.util.Arrays; +import java.util.List; public class Configuration { @@ -84,6 +83,8 @@ public class Configuration { private String origin; + private boolean enableCors = true; + private boolean httpCompression = true; private boolean websocketCompression = true; @@ -154,6 +155,7 @@ public Configuration() { setAddVersionHeader(conf.isAddVersionHeader()); setOrigin(conf.getOrigin()); + setEnableCors(conf.isEnableCors()); setAllowHeaders(conf.getAllowHeaders()); setSSLProtocol(conf.getSSLProtocol()); @@ -517,13 +519,30 @@ public boolean isAddVersionHeader() { public void setOrigin(String origin) { this.origin = origin; } + public String getOrigin() { return origin; } + /** + * cors dispose + *
+ * Default is true
+ *
+ * @param enableCors enableCors
+ */
+ public void setEnableCors(boolean enableCors) {
+ this.enableCors = enableCors;
+ }
+
+ public boolean isEnableCors() {
+ return enableCors;
+ }
+
public boolean isUseLinuxNativeEpoll() {
return useLinuxNativeEpoll;
}
+
public void setUseLinuxNativeEpoll(boolean useLinuxNativeEpoll) {
this.useLinuxNativeEpoll = useLinuxNativeEpoll;
}
diff --git a/src/main/java/com/corundumstudio/socketio/handler/EncoderHandler.java b/src/main/java/com/corundumstudio/socketio/handler/EncoderHandler.java
index 53b9abbc..4d538a71 100644
--- a/src/main/java/com/corundumstudio/socketio/handler/EncoderHandler.java
+++ b/src/main/java/com/corundumstudio/socketio/handler/EncoderHandler.java
@@ -116,8 +116,8 @@ private void write(XHROptionsMessage msg, ChannelHandlerContext ctx, ChannelProm
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK);
res.headers().add(HttpHeaderNames.SET_COOKIE, "io=" + msg.getSessionId())
- .add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE)
- .add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, HttpHeaderNames.CONTENT_TYPE);
+ .add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE)
+ .add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, HttpHeaderNames.CONTENT_TYPE);
String origin = ctx.channel().attr(ORIGIN).get();
addOriginHeaders(origin, res);
@@ -136,7 +136,7 @@ private void sendMessage(HttpMessage msg, Channel channel, ByteBuf out, String t
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, status);
res.headers().add(HttpHeaderNames.CONTENT_TYPE, type)
- .add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
+ .add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
if (msg.getSessionId() != null) {
res.headers().add(HttpHeaderNames.SET_COOKIE, "io=" + msg.getSessionId());
}
@@ -188,19 +188,21 @@ private void addOriginHeaders(String origin, HttpResponse res) {
res.headers().add(HttpHeaderNames.SERVER, version);
}
- if (configuration.getOrigin() != null) {
- res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, configuration.getOrigin());
- res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.TRUE);
- } else {
- if (origin != null) {
- res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, origin);
+ if (configuration.isEnableCors()) {
+ if (configuration.getOrigin() != null) {
+ res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, configuration.getOrigin());
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.TRUE);
} else {
- res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
+ if (origin != null) {
+ res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, origin);
+ res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.TRUE);
+ } else {
+ res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
+ }
+ }
+ if (configuration.getAllowHeaders() != null) {
+ res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, configuration.getAllowHeaders());
}
- }
- if(configuration.getAllowHeaders() != null){
- res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, configuration.getAllowHeaders());
}
}