Browse Source

Pull request #668: KERNEL-5056 WebSocket支持将用户名和token放在Header里

Merge in CORE/base-third from ~CLOUD.LIU/base-third:release/10.0 to release/10.0

* commit '13fdb6f2ae5464d04c4b0014619fbd9164eb1f2c':
  无JIRA任务 WebSocket支持将用户名和token放在Header里 代码修改
  无JIRA任务 WebSocket支持将用户名和token放在Header里
release/10.0
Cloud.Liu 4 years ago
parent
commit
54ca67557e
  1. 12
      fine-socketio/src/main/java/com/fr/third/socketio/Configuration.java
  2. 15
      fine-socketio/src/main/java/com/fr/third/socketio/handler/EncoderHandler.java

12
fine-socketio/src/main/java/com/fr/third/socketio/Configuration.java

@ -89,6 +89,8 @@ public class Configuration {
private boolean randomSession = false;
private String accessControlAllowHeaders = null;
public Configuration() {
}
@ -155,6 +157,8 @@ public class Configuration {
setHttpCompression(conf.isHttpCompression());
setWebsocketCompression(conf.isWebsocketCompression());
setRandomSession(conf.randomSession);
setAccessControlAllowHeaders(conf.getAccessControlAllowHeaders());
}
public JsonSupport getJsonSupport() {
@ -584,4 +588,12 @@ public class Configuration {
public void setRandomSession(boolean randomSession) {
this.randomSession = randomSession;
}
public String getAccessControlAllowHeaders() {
return accessControlAllowHeaders;
}
public void setAccessControlAllowHeaders(String accessControlAllowHeaders) {
this.accessControlAllowHeaders = accessControlAllowHeaders;
}
}

15
fine-socketio/src/main/java/com/fr/third/socketio/handler/EncoderHandler.java

@ -15,6 +15,7 @@
*/
package com.fr.third.socketio.handler;
import static com.fr.third.springframework.util.StringUtils.isEmpty;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
import java.io.IOException;
@ -121,7 +122,7 @@ public class EncoderHandler extends ChannelOutboundHandlerAdapter {
.add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, HttpHeaderNames.CONTENT_TYPE);
String origin = ctx.channel().attr(ORIGIN).get();
addOriginHeaders(origin, res);
addOriginAndAllowHeaders(origin, res);
ByteBuf out = encoder.allocateBuffer(ctx.alloc());
sendMessage(msg, ctx.channel(), out, res, promise);
@ -143,7 +144,7 @@ public class EncoderHandler extends ChannelOutboundHandlerAdapter {
}
String origin = channel.attr(ORIGIN).get();
addOriginHeaders(origin, res);
addOriginAndAllowHeaders(origin, res);
HttpUtil.setContentLength(res, out.readableBytes());
@ -185,19 +186,25 @@ public class EncoderHandler extends ChannelOutboundHandlerAdapter {
sendMessage(errorMsg, ctx.channel(), encBuf, "application/json", promise, HttpResponseStatus.BAD_REQUEST);
}
private void addOriginHeaders(String origin, HttpResponse res) {
private void addOriginAndAllowHeaders(String origin, HttpResponse res) {
if (version != null) {
res.headers().add(HttpHeaderNames.SERVER, version);
}
if (origin != null) {
String configOrigin = configuration.getOrigin();
if (configOrigin != null && !"".equals(configOrigin) && !configOrigin.contains(origin)) {
if (!isEmpty(configOrigin) && !configOrigin.contains(origin)) {
throw new IllegalArgumentException();
}
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, origin);
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.TRUE);
}
String allowHeaders = configuration.getAccessControlAllowHeaders();
if (!isEmpty(allowHeaders)) {
res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, configuration.getAccessControlAllowHeaders());
}
}
@Override

Loading…
Cancel
Save