使用JBoss AS 7进行SSL加密的EJB调用
加密客戶端和服務(wù)器之間的通信可為您的系統(tǒng)提供改進(jìn)的安全性和隱私保護(hù)。 這可能是客戶的一項重要要求,尤其是在客戶端或服務(wù)器需要在不受保護(hù)的網(wǎng)絡(luò)中工作時。
本文向您展示了如何在JBoss AS 7中設(shè)置SSL加密的EJB調(diào)用。
服務(wù)器
在服務(wù)器端只需完成兩件事:
無論是否加密,應(yīng)用程序的源代碼都保持不變。
創(chuàng)建密鑰
Java提供了工具keytool ,我們將使用它來管理密鑰庫和創(chuàng)建??私鑰/公鑰對。 下面的示例使用RSA算法創(chuàng)建一對1024位密鑰,并將它們添加到密鑰存儲server.keystore中 。 如果密鑰庫不存在,則將創(chuàng)建它。
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應(yīng)用服務(wù)器。 因此,我更喜歡將其存儲在JBoss配置目錄中。 但是,只要JBoss服務(wù)器可以訪問它,就可以將其存儲在所需的任何位置。
服務(wù)器配置
現(xiàn)在,我們必須在JBoss配置中引用密鑰庫。 因此,我們在應(yīng)用程序領(lǐng)域的安全領(lǐng)域配置中添加了一個服務(wù)器標(biāo)識元素。
以下代碼片段顯示了使用標(biāo)準(zhǔn)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>...這就是需要在服務(wù)器端完成的所有工作。
客戶
在客戶端,我們需要執(zhí)行以下操作:
導(dǎo)入密鑰
首先,我們需要導(dǎo)出添加到服務(wù)器密鑰存儲中的密鑰對的公共密鑰。 也可以使用keytool來完成:
keytool -export -keystore server.keystore -alias jboss -file server.cer -keypass 123456 -storepass 123456如果密鑰庫不存在,則將創(chuàng)建它。
好的,現(xiàn)在我們可以將密鑰添加到客戶端密鑰庫中:
keytool -import -trustcacerts -alias jboss -file server.cer -keystore client.keystore -keypass 123456 -storepass 123456EJBClientProperties
EJBClientProperties中沒有太大的區(qū)別。 需要將屬性remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED和remote.connection.default.connect.options.org.xnio.Options.SSL_STARTTLS設(shè)置為true 。 其余的保持不變。
以下代碼段顯示了到服務(wù)器的SSL加密連接的創(chuàng)建以及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參數(shù)
好的,現(xiàn)在我們快完成了。 唯一缺少的是對客戶端密鑰存儲的引用。 可以使用JVM參數(shù)javax.net.ssl.trustStore作為位置,并使用javax.net.ssl.trustStorePassword作為密鑰存儲區(qū)的密碼來完成此操作,例如:
-Djavax.net.ssl.trustStore=src\test\resources\client.keystore -Djavax.net.ssl.trustStorePassword=123456使用JBoss AS 7設(shè)置SSL加密的EJB調(diào)用需要完成所有這些工作。
故障排除
如果存在任何通信問題,可以設(shè)置-Djavax.net.debug = true以啟用調(diào)試消息。
結(jié)論
在本文中,我們研究了使用JBoss AS 7設(shè)置加密的EJB調(diào)用的配置和代碼更改,這可以在幾分鐘內(nèi)完成,并為您的通信提供了改進(jìn)的安全性和隱私保護(hù)。
翻譯自: https://www.javacodegeeks.com/2014/05/ssl-encrypted-ejb-calls-with-jboss-as-7.html
總結(jié)
以上是生活随笔為你收集整理的使用JBoss AS 7进行SSL加密的EJB调用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: etc余额不足怎么出收费站?
- 下一篇: 编写干净的测试–分而治之