【问题标题】:How to expose WSS 'secure websocket' with Java如何使用 Java 公开 WSS 的“安全 websocket”
【发布时间】:2019-08-16 19:01:49
【问题描述】:

我正在使用 Java 设置一个新的 webSocket 服务器,并希望在 WSS 中公开它,因为我的客户端应用程序使用 HTTPS。你能帮帮我吗

我尝试修改我的 web.xml,但不起作用

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Protected resource</web-resource-name>
      <url-pattern>/*</url-pattern>
      <http-method>GET</http-method>
    </web-resource-collection>
    <!-- https -->
    <user-data-constraint>
      <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
  </security-constraint>
</web-app>

这是我的端点类

@ServerEndpoint(value = "/signalisation/{username}", decoders = MessageDecoder.class, encoders = MessageEncoder.class)
public class ChatEndpoint {
    private Session session;
    private static final Set<ChatEndpoint> chatEndpoints = new CopyOnWriteArraySet<>();
    private static HashMap<String, String> users = new HashMap<>();

    @OnOpen
    public void onOpen(Session session, @PathParam("username") String username) throws IOException, EncodeException {
        System.out.println("connected");
        this.session = session;
        chatEndpoints.add(this);
        users.put(session.getId(), username);

        Message message = new Message();
        message.setFrom(username);
        message.setContent("Connected!");
        broadcast(message, message.getTo());
    }

    @OnMessage
    public void onMessage(Session session, Message message) throws IOException, EncodeException {
        message.setFrom(users.get(session.getId()));
        System.out.println("messaged");

    }

    @OnClose
    public void onClose(Session session) throws IOException, EncodeException {
        System.out.println("out");
    }

    @OnError
    public void onError(Session session, Throwable throwable) {
        // Do error handling here
    }

    private static void broadcast(Message message, String id) throws IOException, EncodeException {
        chatEndpoints.forEach(endpoint -> {
            if (endpoint.session.getId().equals(id)){
                synchronized (endpoint) {
                    try {
                        endpoint.session.getBasicRemote()
                                .sendObject(message);
                    } catch (IOException | EncodeException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    public String getKeyFromMap(HashMap map, String value){

        for (Object o : map.keySet()) {
            if (map.get(o).equals(value))
                return o.toString();
        }
        return null;
    }

}

我的客户端应用程序

var conn = new WebSocket('ws://<IP_ADRESSE>:8080/<PATH>/signalisation/'+c);
conn.onopen = function () {
      console.log("Connected to the server");
};

我的实际结果是:

client.js:11 混合内容:“https://IP_ADRESSE:PORT/?user=blabla”处的页面通过 HTTPS 加载,但尝试连接到不安全的 WebSocket 端点“ws://IP_ADRESSE:8080/PATH/signalisation/blabla”。此请求已被阻止;此端点必须在 WSS 上可用。

【问题讨论】:

    标签: java websocket jboss java-websocket


    【解决方案1】:

    这是几个月前的事了,但希望它可以帮助遇到同样错误的人。 在客户端应用程序上将ws:// 更改为wss://,就在IP_ADRESSE 之前。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-03
      • 2020-03-04
      • 1970-01-01
      • 1970-01-01
      • 2014-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多