java websocket ssl,使用HTTPS localhost SSL证书保护WebSocket(WSS)?

I am a little new to this whole WebSocket and SSL certificate.

So I have created my own WebSocket server on Android side and the website is the client. I was able to make it work with regular WebSocket (ws://) but not secure WebSocket (wss://) due to the fact that it requires SSL certificate.

My question is how can I get a SSL certificate? From what I've read, SSL certificate is based on a domain. I need it for localhost. I need it for something like this address:

wss://localhost:8080/ws/main

How can I go about getting a SSL certificate that will work with localhost.

Thank you for your time!

====================== EDIT =====================

Reason why I am doing this:

I have a Bluetooth service in my Android application that will be getting data from connected health bluetooth devices like Weight Scale and Blood Pressure machine. I have this part implemented already and I want to take this data and pass it to a website. WebSocket seemed easier because the user will have my application open and when they do their weight, it would automatically fill the field on the website with the weight from the Weight Scale. I hope I am making this clear.

To do this, I need to have a way to pass the weight or blood pressure values from Java (Android) to the website that loads within a WebView. So I thought WebSocket would the easiest way.

Please tell me if you think there is an easier way.

Also, I've already tried self-signed certificate and I get the following error:

I/X509Util: Failed to validate the certificate chain, error: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

OR

Exception=javax.net.ssl.SSLException: Error occured in delegated task:javax.net.ssl.SSLException: Not trusted server certificate

Thank you!!!

解决方案

We faced a similar problem, our solution was to register a subdomain to one of our domains with an A record to 127.0.0.1 and get a certificate for that domain.

local.example.com -> A record to 127.0.0.1

SSL certificate requested for local.example.com

I'm afraid this answer is too late for you however, it can be helpful for others finding this article.

Java使用 WSS 协议与使用 WS 协议的代码基本相同,只需要在 WebSocket URI 中将 `ws` 替换为 `wss`,并且使用 SSLContext 来配置 SSL/TLS 加密。下面是一个简单的 WSS 协议的 WebSocket 服务器示例: ```java import java.net.InetSocketAddress; import java.security.KeyStore; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManagerFactory; import com.sun.net.httpserver.HttpContext; import com.sun.net.httpserver.HttpsConfigurator; import com.sun.net.httpserver.HttpsServer; import com.sun.net.httpserver.HttpsParameters; import com.sun.net.httpserver.HttpsServerProvider; import com.sun.net.httpserver.spi.HttpServerProvider; import org.java_websocket.server.DefaultSSLWebSocketServerFactory; import org.java_websocket.server.WebSocketServer; public class WssWebSocketServer extends WebSocketServer { public WssWebSocketServer(InetSocketAddress address) { super(address); } @Override public void onOpen(WebSocket conn, ClientHandshake handshake) { // 处理 WebSocket 连接建立事件 } @Override public void onClose(WebSocket conn, int code, String reason, boolean remote) { // 处理 WebSocket 连接关闭事件 } @Override public void onMessage(WebSocket conn, String message) { // 处理 WebSocket 收到消息事件 } @Override public void onError(WebSocket conn, Exception ex) { // 处理 WebSocket 错误事件 } public static void main(String[] args) { try { // 加载证书和密钥 char[] password = "password".toCharArray(); KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(WssWebSocketServer.class.getResourceAsStream("/keystore.jks"), password); KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(keyStore, password); TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(keyStore); // 配置 SSLContext SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null); // 创建 WSS 协议的 WebSocket 服务器 InetSocketAddress address = new InetSocketAddress("localhost", 8080); WssWebSocketServer server = new WssWebSocketServer(address); // 配置 SSL/TLS 加密 server.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(sslContext)); // 启动 WebSocket 服务器 server.start(); } catch (Exception ex) { ex.printStackTrace(); } } } ``` 在这个示例中,我们使用 `org.java_websocket` 库来实现 WebSocket 服务器,使用 `com.sun.net.httpserver` 库来创建 HttpsServer。在 `main` 方法中,我们首先加载证书和密钥,然后配置 SSLContext。接着,我们创建了一个 WSS 协议的 WebSocket 服务器,并使用 `DefaultSSLWebSocketServerFactory` 类将 SSLContext 配置到 WebSocketFactory 中。最后,我们启动了 WebSocket 服务器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值