Created
April 6, 2015 17:29
-
-
Save nputnam/e96be6fd83f266de0b3a to your computer and use it in GitHub Desktop.
Security Handler for Nifty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
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