簡體   English   中英

如何使用 Java 公開 WSS“安全 websocket”

[英]How to expose WSS 'secure websocket' with Java

我正在使用 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/”信號化/blabla'。 此請求已被阻止; 此端點必須可通過 WSS 使用。

這是一個月前,但希望它可以幫助有同樣錯誤的人。 在客戶端應用程序IP_ADRESSE ws://更改為wss:// ,就在IP_ADRESSE之前。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM