jboss ejb
加密客户端和服务器之间的通信可为您的系统提供改进的安全性和隐私保护。 这可能是客户的一项重要要求,尤其是在客户端或服务器需要在不受保护的网络中工作时。
本文向您展示了如何在JBoss AS 7中设置SSL加密的EJB调用。
服务器
在服务器端只需完成两件事:
- 使用专用/公用密钥对创建密钥库以进行加密和
- 在服务器配置中引用密钥库。
无论是否加密,应用程序的源代码都保持不变。
创建密钥
Java提供了工具keytool ,我们将使用它来管理密钥库和创建私钥/公钥对。 下面的示例使用RSA算法创建一对1024位密钥,并将它们添加到密钥存储server.keystore中 。 如果密钥库不存在,则将创建它。
keytool -genkey -alias jboss -keyalg RSA -keysize 1024 -keystore server.keystore -validity 365 -keypass 123456 -storepass 123456 -dname "CN=localhost, O=thoughts-on-java.org"
我们将需要将此密钥存储提供给JBoss应用服务器。 因此,我更喜欢将其存储在JBoss配置目录中。 但是,只要JBoss服务器可以访问它,就可以将其存储在所需的任何位置。
服务器配置
现在,我们必须在JBoss配置中引用密钥库。 因此,我们在应用程序领域的安全领域配置中添加了一个服务器标识元素。
以下代码片段显示了使用标准ApplicationRealm配置和位于JBoss配置目录中的server.keystore文件的示例配置:
<management><security-realms><security-realm name="ManagementRealm"><authentication><properties path="mgmt-users.properties" relative-to="jboss.server.config.dir"/></authentication></security-realm><security-realm name="ApplicationRealm"><server-identities><ssl><keystore path="server.keystore" relative-to="jboss.server.config.dir" password="123456"/></ssl></server-identities><authentication><properties path="application-users.properties" relative-to="jboss.server.config.dir"/></authentication></security-realm></security-realms>...
这就是需要在服务器端完成的所有工作。
客户
在客户端,我们需要执行以下操作:
- 将服务器的公钥导入客户端密钥库,
- 在EJBClientProperties中定义SSL加密,并
- 提供带有公用密钥JVM参数的密钥存储区的位置和密码。
导入密钥
首先,我们需要导出添加到服务器密钥库中的密钥对的公钥。 也可以使用keytool来完成:
keytool -export -keystore server.keystore -alias jboss -file server.cer -keypass 123456 -storepass 123456
如果密钥库不存在,则将创建它。
好的,现在我们可以将密钥添加到客户端密钥库中:
keytool -import -trustcacerts -alias jboss -file server.cer -keystore client.keystore -keypass 123456 -storepass 123456
EJBClientProperties
EJBClientProperties中没有太大的区别。 需要将属性remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED和remote.connection.default.connect.options.org.xnio.Options.SSL_STARTTLS设置为true 。 其余的保持不变。
以下代码段显示了到服务器的SSL加密连接的创建以及SLSB的查找。
// define EJB client properties
final Properties props = new Properties();
// define SSL encryption
props.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED","true");
props.put("remote.connection.default.connect.options.org.xnio.Options.SSL_STARTTLS","true");
// connection properties
props.put("remote.connections", "default");
props.put("remote.connection.default.host", "localhost");
props.put("remote.connection.default.port", "4447");
// user credentials
props.put("remote.connection.default.username", "test");
props.put("remote.connection.default.password", "1234");props.put("remote.connection.default.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS","JBOSS-LOCAL-USER");
props.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT","false");
props.put("remote.connection.default.connect.options.org.jboss.remoting3.RemotingOptions.HEARTBEAT_INTERVAL","600000");// create EJB client configuration
final EJBClientConfiguration clientConfiguration = new PropertiesBasedEJBClientConfiguration(props);// create and set a context selector
final ContextSelector<EJBClientContext> contextSelector = new ConfigBasedEJBClientContextSelector(clientConfiguration);
EJBClientContext.setSelector(contextSelector);// create InitialContext
final Hashtable<Object, Object> contextProperties = new Hashtable<>();
ejbURLContextFactory.class.getName();
contextProperties.put(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming");
InitialContext initialContext = new InitialContext(contextProperties);// lookup SLSB
GreeterRemote greeter = (GreeterRemote) initialContext.lookup("ejb:/test/Greeter!blog.thoughts.on.java.ssl.remote.GreeterRemote");
Assert.assertEquals("Hello World!", greeter.greet("World"));
JVM参数
好的,现在我们快完成了。 唯一缺少的是对客户端密钥存储的引用。 可以使用JVM参数javax.net.ssl.trustStore作为位置,并使用javax.net.ssl.trustStorePassword作为密钥存储区的密码来完成,例如:
-Djavax.net.ssl.trustStore=src\test\resources\client.keystore -Djavax.net.ssl.trustStorePassword=123456
使用JBoss AS 7设置SSL加密的EJB调用需要完成所有这些工作。
故障排除
如果存在任何通信问题,则可以设置-Djavax.net.debug = true以启用调试消息。
结论
在本文中,我们研究了使用JBoss AS 7设置加密的EJB调用的配置和代码更改,这可以在几分钟内完成,并为您的通信提供了改进的安全性和隐私保护。
翻译自: https://www.javacodegeeks.com/2014/05/ssl-encrypted-ejb-calls-with-jboss-as-7.html
jboss ejb