我在Android上创建了NanoHTTPD服务器。我可以在以前的工作中创建一个https服务器,但是它并不安全。我希望它增加了带有绿色挂锁的安全性,因为我在连接到iOS上的服务器时遇到问题,因此我做了一些研究并修改了我的旧代码,然后这样做,但是浏览器显示“该网站无法提供安全连接”。我该怎么解决?
try {
if (!isServerConnected) {
KeyStore keyStore = KeyStore.getInstance("BKS");
keyStore.load(Objects.requireNonNull(getClass().getClassLoader()).getResourceAsStream("newselfsigned.bks"), "mypassword".toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
SSLContext sslctx = SSLContext.getInstance("TLS");
sslctx.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
SSLServerSocketFactory factory = sslctx.getServerSocketFactory();
server.makeSecure(factory, new String [] { "TLSv1", "TLSv1.1","SSLv3"});
server.start(10000);
WifiManager wifiManager = (WifiManager) v.getContext().getApplicationContext().getSystemService(WIFI_SERVICE);
int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
@SuppressLint("DefaultLocale") final String formatedIpAddress = String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
+(ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
isServerConnected = true;
Toast.makeText(v.getContext(), "Connected : " + "Please access! https://" + formatedIpAddress + ":" + server.getListeningPort() + " From a web browser", Toast.LENGTH_SHORT).show();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://" + formatedIpAddress + ":" + server.getListeningPort()));
startActivity(browserIntent);
}
} catch (IOException | KeyStoreException | NoSuchAlgorithmException | KeyManagementException | CertificateException e) {
Toast.makeText(v.getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}