How can I configure Simple Framework to require SSL client authentication?

你说的曾经没有我的故事 提交于 2019-12-21 05:33:07

问题


I am writing an HTTP server using the Simple Framework and would like to require clients to present a valid certificate signed by my certificate authority in order to establish a connection.

I have the following bare-bones server written. How can I modify this code to require client certificate authentication for all SSL connections?

public static void main(String[] args) throws Exception {
    Container container = createContainer();

    Server server = new ContainerServer(container);
    Connection connection = new SocketConnection(server);
    SocketAddress address = new InetSocketAddress(8443);

    KeyManagerFactory km = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    KeyStore serverKeystore = KeyStore.getInstance(KeyStore.getDefaultType());
    try(InputStream keystoreFile = new FileInputStream(SERVER_KEYSTORE_PATH)) {
        serverKeystore.load(keystoreFile, "asdfgh".toCharArray());
    }
    km.init(serverKeystore, SERVER_KS_PASSWORD.toCharArray());

    TrustManagerFactory tm = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    KeyStore caKeystore = KeyStore.getInstance(KeyStore.getDefaultType());
    try(InputStream caCertFile = new FileInputStream(CA_CERT_PATH)) {
        caKeystore.load(caCertFile, CA_KS_PASSWORD.toCharArray());
    }
    tm.init(caKeystore);

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(km.getKeyManagers(), tm.getTrustManagers(), null);

    connection.connect(address, sslContext);
}

回答1:


Try this

public class MyServer implements org.simpleframework.transport.Server {

   private Server delegateServer;

   public MyServer(Server delegateServer){
      this.delegateServer = delegateServer;
   }

   public void process(Socket socket){
     socket.getEngine().setNeedClientAuth(true);
     delegateServer.process(socket);
   }
}


来源:https://stackoverflow.com/questions/16927938/how-can-i-configure-simple-framework-to-require-ssl-client-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!