我有一个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操作的方法吗?
答案 0 :(得分:0)
我想出办法。
基本上我所做的是使用java的 keystore 来生成 keystore.jks 文件然后我使用SSLContext使我的密钥库证书有效并因此执行端口80上的SSL。
感谢您的帮助。
答案 1 :(得分:-1)
SSL正在端口443上运行。它是默认的,就像80一样,在地址栏上没有显示。