简体   繁体   English

Java服务器上具有Socket.IO的“访问控制允许来源”

[英]'Access-Control-Allow-Origin' with Socket.IO on Java Server

I've got the following problem: I'm running a JAVA Server with socket.io (netty socket.io - https://github.com/mrniko/netty-socketio ) - I'm trying to access this server from a different web-server through javascript. 我遇到以下问题:我正在使用socket.io(netty socket.io- https://github.com/mrniko/netty-socketio )运行JAVA服务器-我正在尝试从通过javascript来访问其他Web服务器。

For a test I'm trying to get the Demo Chat running ( https://github.com/mrniko/netty-socketio-demo ). 为了进行测试,我正在尝试运行演示聊天窗口( https://github.com/mrniko/netty-socketio-demo )。

The Problem now is that i keep getting the following 现在的问题是,我不断得到以下

XMLHttpRequest cannot load http://myserver/socket.io/1/?t=1400445162388. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'myclient.com' is therefore not allowed access. 

So the big question is: How do i allow access to my java server? 因此,最大的问题是:如何允许访问我的Java服务器? I've found a few solutions saying to add the 'header' to it, but i have no idea where to put that. 我找到了一些解决方案,说要在其中添加“标头”,但是我不知道该在哪里放置。 Does that go into the socket.io server code? 那会进入socket.io服务器代码吗?

I think i need something like this, placed somewhere on the JAVA server: 我想我需要像这样的东西,放在JAVA服务器上:

response.setHeader("Access-Control-Allow-Origin", "*");

This is the code which starts the server: 这是启动服务器的代码:

Configuration config = new Configuration();
    config.setHostname("localhost");
    config.setPort(80);
    final SocketIOServer server = new SocketIOServer(config);
    //ChatObject wurde extra implementiert, ggf. loeschen
    server.addJsonObjectListener(ChatObject.class, new DataListener<ChatObject>() {
        @Override
        public void onData(SocketIOClient client, ChatObject data, AckRequest ackRequest) throws Exception {
            // broadcast messages to all clients
            server.getBroadcastOperations().sendJsonObject(data);
        }
    });
    server.start();

and this is the output im getting from the server: 这是从服务器获取的输出即时消息:

error: The specified resource was not found: /static/flashsocket/WebSocketMain.swf
error: The specified resource was not found: /static/flashsocket/WebSocketMainInsecure.swf
info: Session store / pubsub factory used: {}MemoryStoreFactory (local session store only)
info: SocketIO server started at port: {}80

Any help is greatly appreciated ! 任何帮助是极大的赞赏 ! Thank you 谢谢

I've confronted the similar problem yesterday and solved it mostly. 昨天我遇到了类似的问题,并且解决了大多数问题。 I'd like to share my solution here. 我想在这里分享我的解决方案。
Firstly, we all know it is so called CORS standard (cross-origin resource sharing) which means one resource we opened up from website A request another resource from website B ( reference ). 首先,我们都知道这就是所谓的CORS标准(跨域资源共享),这意味着我们从网站A打开的一种资源从网站B请求另一种资源( 参考 )。 Therefore, in your case, assume the original web server (with domain AAA.com) and your netty server (domain BBB.com), then append this line to your netty server to get permission: 因此,在您的情况下,假定原始Web服务器(域AAA.com)和您的净值服务器(域BBB.com),然后将此行附加到净值服务器以获取许可:

config.setOrigin("http://AAA.com");

Note that you cannot use wildcard (*) for Access-Control-Allow-Origin in netty-socketio as it set credentials (ie, Access-Control-Allow-Credentials ) TRUE by default ( see here ). 请注意,您不能在netty-socketio中的通配符(*)中使用Access-Control-Allow-Origin ,因为它默认情况下将凭据(即Access-Control-Allow-Credentials )设置为TRUE( 请参阅此处 )。 Keep in mind that the browser will also reject any response that does not have the Access-Control-Allow-Credentials: true header, and not make the response available to the invoking web content . 请记住, 浏览器还将拒绝任何没有Access-Control-Allow-Credentials:true标头的响应,并且不会使响应可用于调用Web内容 Hope these help. 希望这些帮助。

The followings are my library versions: 以下是我的库版本:
netty-socketio version netty-socketio版本

<dependency>
    <groupId>com.corundumstudio.socketio</groupId>
    <artifactId>netty-socketio</artifactId>
    <version>1.7.6</version>
</dependency>

socket.io.javascript socket.io.javascript

script(type="text/javascript", src="https://cdn.socket.io/socket.io-1.3.5.js")    

Use netty-socketio 1.7.1 or 1.6.6 version. 使用netty-socketio 1.7.1或1.6.6版本。 Ability to set custom Access-Control-Allow-Origin via Configuration.origin parameter was added in this version. 此版本中添加了通过Configuration.origin参数设置自定义Access-Control-Allow-Origin的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM