gpt4 book ai didi

java - 如何对80端口进行SSL操作

转载 作者:太空宇宙 更新时间:2023-11-03 15:08:41 24 4
gpt4 key购买 nike

我有一个 server.java 文件:

import javax.net.ssl.*;
import java.io.*;
//*****************
public class Server
{
public static void main(String[] args)
{
try {
SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
SSLServerSocket sslserversocket =(SSLServerSocket)sslserversocketfactory.createServerSocket(80);

SSLSocket sslsocket = (SSLSocket)
sslserversocket.accept();

InputStream is = sslsocket.getInputStream();
InputStreamReader isr = new
InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);
String string = null;

while ((string = br.readLine()) != null)
{
System.out.println(string);
System.out.flush();
}
}

catch (Exception e){
e.printStackTrace();
}
}
}

和一个 Client.java 文件:

import javax.net.ssl.*;
import java.io.*;
//*****************
public class Client
{
public static void main(String[] args)
{
try {
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("127.0.0.1", 80);

InputStream is = System.in;
InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);
OutputStream os = sslsocket.getOutputStream();

OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);

String string = null;
while ((string = br.readLine()) != null)
{
bw.write(string + '\n');
bw.flush();
}
}
catch (Exception e){
e.printStackTrace();
}
}
}

我也像这样创建了自己的证书:

C:\Temp>keytool -genkey -keystore myCertificate -keyalg RSA
Enter keystore password: abcdefg
Re-enter new password: abcdefg
What is your first and last name?
[Unknown]: first last
What is the name of your organizational unit?
[Unknown]: cs
What is the name of your organization?
[Unknown]: stackoverflow
What is the name of your City or Locality?
[Unknown]: NYC
What is the name of your State or Province?
[Unknown]: NY
What is the two-letter country code for this unit?
[Unknown]: us
Is CN=first last, OU=cs, O=stackoverflow, L=NYC, ST=NY, C=us correct?
[no]: yes
Enter key password for <mykey>
(RETURN if same as keystore password):

但无论我做什么,我都会不断收到错误:

main, handling exception: javax.net.ssl.SSLHandshakeException: Remote host closed connection during
handshake
main, SEND TLSv1 ALERT: fatal, description = handshake_failure
main, WRITE: TLSv1 Alert, length = 2
main, called closeSocket()
javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.writeRecord(Unknown Source)
at sun.security.ssl.AppOutputStream.write(Unknown Source)
at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)
at sun.nio.cs.StreamEncoder.implFlushBuffer(Unknown Source)
at sun.nio.cs.StreamEncoder.implFlush(Unknown Source)
at sun.nio.cs.StreamEncoder.flush(Unknown Source)
at java.io.OutputStreamWriter.flush(Unknown Source)
at java.io.BufferedWriter.flush(Unknown Source)
at Client.main(Client.java:25)
Caused by: java.io.EOFException: SSL peer shut down incorrectly
at sun.security.ssl.InputRecord.read(Unknown Source)
... 11 more

我担心的是,如果我将端口从 80 更改为其他端口,代码可以正常工作,但我必须专门处理此端口本身。证书和两个 java 文件位于同一文件夹。你们能帮我想出一种在端口80上执行SSL操作的方法吗?

最佳答案

我想出了一个办法。

基本上我所做的是使用 java 的 keystore 生成一个 keystore.jks 文件,然后我使用 SSLContext 使我的 keystore 证书有效,因此在端口 80 上执行 SSL .

谢谢大家的帮助。

关于java - 如何对80端口进行SSL操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23333083/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com