<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="https://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    JBoss 7 and Ejb remote call with security
</h3>
<span style="margin-bottom: 10px;">
    modified by <a href="https://community.jboss.org/people/mates1234">Petr Mates</a> in <i>JBoss AS 7 Development</i> - <a href="https://community.jboss.org/docs/DOC-17581">View the full document</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p><span>This article describes my tests with <span>ejb</span> and JBoss7.</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><span>For my test a have ear (TestEar) with one <span>ejb</span> module (TestEjb) and one bean (TestBean).</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code">TestEar.ear
&#160;&#160; +---TestEjb.ejb
&#160;&#160;&#160;&#160;&#160;&#160; +--mates.test.TestBean.class
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mates.test.TestBeanRemote.class
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>As security I use RealmUsersRoles with </p><p><strong><em>x-users.properties</em></strong></p><pre class="jive-pre"><code class="jive-code">testX=test1234
</code></pre><p><strong><em>x-users.properties</em></strong></p><pre class="jive-pre"><code class="jive-code">testX=bean
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>And now standalone.xml</p><p>and I changed ApplicationRealm</p><pre class="jive-pre"><code class="jive-code jive-xml"><span class="jive-xml-tag">&lt;security-realm name="ApplicationRealm"&gt;</span>
&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;authentication&gt;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;jaas name="bean-sec-domain"/&gt;</span>
&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;/authentication&gt;</span>
<span class="jive-xml-tag">&lt;/security-realm&gt;</span>
</code></pre><p>and security domain</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-xml"><span class="jive-xml-tag">&lt;security-domain name="bean-sec-domain" cache-type="default"&gt;</span>&#160;&#160;&#160;&#160; 
&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;authentication&gt;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;login-module code="Remoting" flag="optional"&gt;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;module-option name="password-stacking" value="useFirstPass"/&gt;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;/login-module&gt;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;login-module code="org.jboss.security.auth.spi.UsersRolesLoginModule" flag="required"&gt;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;module-option name="defaultUsersProperties" value="file:/${jboss.server.config.dir}/x-users.properties"/&gt;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;module-option name="defaultRolesProperties" value="file:/${jboss.server.config.dir}/x-roles.properties"/&gt;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;module-option name="usersProperties" value="file:/${jboss.server.config.dir}/x-users.properties"/&gt;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;module-option name="rolesProperties" value="file:/${jboss.server.config.dir}/x-roles.properties"/&gt;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;module-option name="password-stacking" value="useFirstPass"/&gt;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;/login-module&gt;</span>
&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;/authentication&gt;</span>
<span class="jive-xml-tag">&lt;/security-domain&gt;</span>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>And now lets look at bean.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">@Stateless
@DeclareRoles(<font color="red">"bean"</font>)
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> TestBean <font color="navy"><b>implements</b></font> TestBeanRemote <font color="navy">{</font>
&#160;
&#160;&#160;&#160;&#160;&#160;&#160;&#160; @Resource
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>private</b></font> EJBContext context;
&#160;
&#160;&#160;&#160;&#160;&#160;&#160;&#160; @Override
&#160;&#160;&#160;&#160;&#160;&#160;&#160; @RolesAllowed(<font color="red">"bean"</font>)
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>public</b></font> String getName ()
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; getNameFree();
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy">}</font>
&#160;
&#160;&#160;&#160;&#160;&#160;&#160;&#160; @Override
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>public</b></font> String getNameFree ()
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; String aName = <font color="red">""</font>;
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>if</b></font> (context.getCallerPrincipal() != <font color="navy"><b>null</b></font>) <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; aName = context.getCallerPrincipal().getName();
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy">}</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>return</b></font> <font color="red">"name "</font> + aName + <font color="red">" "</font> + context.isCallerInRole(<font color="red">"bean"</font>);
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy">}</font>
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Let's secure EJB</p><p>add <strong><em>jboss-app.xml</em></strong> to TestEar.ear\META-INF. I use security domain other to ensure that i secure all beans.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;p:jboss-app xmlns:p="http://www.jboss.com/xml/ns/javaee"
&#160;&#160;&#160; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
&#160;&#160;&#160; xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee ../../xsd/jboss-app_7_0.xsd "&gt;
&#160;&#160;&#160; &lt;security-domain&gt;other&lt;/security-domain&gt;
&lt;/p:jboss-app&gt;</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>And I want to secure TestBean. I have to add <strong><em>jboss-ejb3.xml</em></strong> to TestEjb.jar\META-INF\. TestBean is secured by <strong>bean-sec-domain</strong>.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-xml"><span class="jive-xml-tag">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span>
<span class="jive-xml-tag"><span>&lt;jboss:ejb-jar xmlns:jboss="</span><a class="jive-link-external-small" href="http://www.jboss.com/xml/ns/javaee" target="_blank">http://www.jboss.com/xml/ns/javaee</a><span>"
&#160;&#160;&#160; xmlns="</span><a class="jive-link-external-small" href="http://java.sun.com/xml/ns/javaee" target="_blank">http://java.sun.com/xml/ns/javaee</a><span>" xmlns:xsi="</span><a class="jive-link-external-small" href="http://www.w3.org/2001/XMLSchema-instance" target="_blank">http://www.w3.org/2001/XMLSchema-instance</a><span>"
&#160;&#160;&#160; xmlns:security="urn:security" version="3.1" impl-version="2.0"&gt;</span></span>
&#160;&#160;&#160; <span class="jive-xml-tag"><span>&lt;assembly-descriptor xmlns="</span><a class="jive-link-external-small" href="http://java.sun.com/xml/ns/javaee" target="_blank">http://java.sun.com/xml/ns/javaee</a><span>"&gt;</span></span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;security:security xmlns:security="urn:security"&gt;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;security:security-domain&gt;</span>bean-sec-domain<span class="jive-xml-tag">&lt;/security:security-domain&gt;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;ejb-name&gt;</span>TestBean<span class="jive-xml-tag">&lt;/ejb-name&gt;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;/security:security&gt;</span>
&#160;&#160;&#160; <span class="jive-xml-tag">&lt;/assembly-descriptor&gt;</span>
<span class="jive-xml-tag">&lt;/jboss:ejb-jar&gt;</span>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Remote interface is realz simple with 2 methods.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Client:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">Hashtable&lt;String, Object&gt; p = <font color="navy"><b>new</b></font> Hashtable&lt;String, Object&gt;();
p.put(Context.INITIAL_CONTEXT_FACTORY, InitialContextFactory.class.getName());
p.put(Context.PROVIDER_URL, <font color="red">"remote://127.0.0.1:4447/"</font>);
p.put(InitialContext.SECURITY_PRINCIPAL, <font color="red">"testX"</font>);
p.put(InitialContext.SECURITY_CREDENTIALS, <font color="red">"test1234"</font>);
p.put(<font color="red">"jboss.naming.client.ejb.context"</font>, <font color="navy"><b>true</b></font>);
p.put(<font color="red">"jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT"</font>, <font color="red">"false"</font>);
&#160;
InitialContext c = <font color="navy"><b>new</b></font> InitialContext(p);
TestBeanRemote vLookup = (TestBeanRemote) c.lookup(<font color="red">"java:/TestEar/TestEjb/TestBean!"</font>+ TestBeanRemote.class.getName());
&#160;
System.out.println(<font color="red">"x"</font> + vLookup.getNameFree());
System.out.println(<font color="red">"x"</font> + vLookup.getName());
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p> After this you can see </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code">xname testX true
xsecured name testX true
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><strong>That's all.</strong></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><strong>And now some other tests:</strong></p><p>Most important mart is </p><p><em><strong style="font-size: 8pt; font-family: courier new,courier;">p.put("jboss.naming.client.ejb.context", true);</strong></em></p><p><span style="font-family: courier new,courier; font-size: 8pt;">witout this property you wil see "</span><em>No EJB receiver available for handling [appName:TestEar,modulename:TestEjb,distinctname:] combination</em><span style="font-family: courier new,courier; font-size: 8pt;">"</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>when you put in class path file <strong><em>jboss-ejb-client.properties</em></strong> with standard</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code">remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false

remote.connections=x1

remote.connection.x1.host=127.0.0.1
remote.connection.x1.port = 4447
remote.connection.x1.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
remote.connection.x1.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT=false&#160; 
</code></pre><p><span style="font-family: courier new,courier; font-size: 8pt;"><br/></span></p><p>and comment <em><strong style="font-size: 8pt; font-family: courier new,courier;">p.put("jboss.naming.client.ejb.context", true);</strong></em></p><p>and in console is now</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code">xname $local false
Exception in thread "main" javax.ejb.EJBAccessException: JBAS014502: Invocation on method: public abstract java.lang.String mates.test.TestBeanRemote.getName() of bean: TestBean is not allowed
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><em><strong style="font-size: 8pt; font-family: courier new,courier;">jboss.naming.client.ejb.context </strong></em><span style="font-family: courier new,courier; font-size: 8pt;">setup EJBContext on client side. See </span><strong><em>org.jboss.naming.remote.client.InitialContextFactory</em></strong></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><em><strong style="font-size: 8pt; font-family: courier new,courier;"><br/></strong></em></p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Comment by <a href="https://community.jboss.org/docs/DOC-17581">going to Community</a></p>

        <p style="margin: 0;">Create a new document in JBoss AS 7 Development at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2225">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>