Skip to content

Instantly share code, notes, and snippets.

@nputnam
Created April 6, 2015 17:29
Show Gist options
  • Save nputnam/e96be6fd83f266de0b3a to your computer and use it in GitHub Desktop.
Save nputnam/e96be6fd83f266de0b3a to your computer and use it in GitHub Desktop.
Security Handler for Nifty
...
NiftySecurityHandlers niftySecurityHandlers = new NiftySecurityHandlers() {
@Override
public ChannelHandler getAuthenticationHandler() {
return noOpHandler;
}
@Override
public ChannelHandler getEncryptionHandler() {
try {
SSLContext tlsContext = null;
char[] passphrase = configuration.getKeystorePassword().toCharArray();
// First initialize the key and trust material.
KeyStore ks = KeyStore.getInstance("JKS");
// KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(new FileInputStream(configuration.getKeystore()), passphrase);
tlsContext = SSLContext.getInstance("TLS");
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, passphrase);
tlsContext.init(kmf.getKeyManagers(), null, null);
SSLEngine sslEngine = tlsContext.createSSLEngine();
sslEngine.setUseClientMode(false);
SslHandler sslHandler = new SslHandler(sslEngine, false);
return sslHandler;
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
};
NiftySecurityFactory niftySecurityFactory = new NiftySecurityFactory() {
@Override
public NiftySecurityHandlers getSecurityHandlers(ThriftServerDef thriftServerDef, NettyServerConfig nettyServerConfig) {
return niftySecurityHandlers;
}
};
ThriftServerDef serverDef = new ThriftServerDefBuilder()
.clientIdleTimeout(new Duration(60, TimeUnit.SECONDS))
.withProcessor(processor)
.listen(port)
.withSecurityFactory(niftySecurityFactory)
.build();
server = new NettyServerTransport(serverDef);
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment