sslsocket-https
3630 단어 자바
private static final String defaultProtocol = "TLS";
private static final int defaultSessionCacheSize = 0;
private static final int defaultSessionTimeout = 86400;
public static void main(String[] args) throws Exception {
/***
* SSLSocket
*/
final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream keyFile=null;
keyStore.load(keyFile, "password".toCharArray());
final KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keyStore, "password".toCharArray());
final SSLContext context = SSLContext.getInstance(defaultProtocol);
final KeyManager[] kms = kmf.getKeyManagers();
context.init(kms,null,null);
final Socket socket = context.getSocketFactory().createSocket(SocketFactory.getDefault().createSocket(),"host",8999,true);
final SSLSocket sslSocket = SSLSocket.class.cast(socket);
sslSocket.startHandshake();
/***
* SSLSocket
*/
final KeyStore serverKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream serverInputStream=null;
serverKeyStore.load(serverInputStream, "changeit".toCharArray());
final KeyManagerFactory serverKmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
serverKmf.init(serverKeyStore, "changeit".toCharArray());
final SSLContext serverSSLContext = SSLContext.getInstance(defaultProtocol);
serverSSLContext.init(serverKmf.getKeyManagers(), null, null);
final SSLSessionContext serverSessionContext = serverSSLContext.getServerSessionContext();
serverSessionContext.setSessionCacheSize(defaultSessionCacheSize);
serverSessionContext.setSessionTimeout(defaultSessionTimeout);
final SSLServerSocketFactory sslProxy = serverSSLContext.getServerSocketFactory();
final ServerSocket serverSocket = sslProxy.createServerSocket(8999, 200);
final SSLServerSocket sslServerSocket = SSLServerSocket.class.cast(serverSocket);
sslServerSocket.setEnabledCipherSuites(new String[]{"enabledCiphers"});
sslServerSocket.setEnabledProtocols(new String[]{"protocols"});
}
private void checkConfig() throws IOException {
// Create an unbound server socket
ServerSocket socket = sslProxy.createServerSocket();
initServerSocket(socket);
try {
// Set the timeout to 1ms as all we care about is if it throws an
// SSLException on accept.
socket.setSoTimeout(1);
socket.accept();
// Will never get here - no client can connect to an unbound port
} catch (SSLException ssle) {
// SSL configuration is invalid. Possibly cert doesn't match ciphers
IOException ioe = new IOException(sm.getString(
"jsse.invalid_ssl_conf", ssle.getMessage()));
ioe.initCause(ssle);
throw ioe;
} catch (Exception e) {
/*
* Possible ways of getting here
* socket.accept() throws a SecurityException
* socket.setSoTimeout() throws a SocketException
* socket.accept() throws some other exception (after a JDK change)
* In these cases the test won't work so carry on - essentially
* the behaviour before this patch
* socket.accept() throws a SocketTimeoutException
* In this case all is well so carry on
*/
} finally {
// Should be open here but just in case
if (!socket.isClosed()) {
socket.close();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.