2013-06-04 26 views
3

我正在使用Simple Framework來編寫HTTP服務器,並希望客戶端提供由我的證書頒發機構簽署的有效證書以建立連接。如何配置Simple Framework以要求SSL客戶端身份驗證?

我寫下了以下裸機服務器。如何修改此代碼以要求所有SSL連接的客戶端證書身份驗證?

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); 
} 

回答

3

試試這個

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); 
    } 
} 
+0

當然!我正在讓這種方式比需要的更難。謝謝! –

+0

這個代碼替換了問題代碼中的容器服務器嗎? – Joe