<!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;"> </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;"> </p><pre class="jive-pre"><code class="jive-code">TestEar.ear
   +---TestEjb.ejb
       +--mates.test.TestBean.class
          mates.test.TestBeanRemote.class
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </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;"> </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"><security-realm name="ApplicationRealm"></span>
     <span class="jive-xml-tag"><authentication></span>
          <span class="jive-xml-tag"><jaas name="bean-sec-domain"/></span>
     <span class="jive-xml-tag"></authentication></span>
<span class="jive-xml-tag"></security-realm></span>
</code></pre><p>and security domain</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-xml"><span class="jive-xml-tag"><security-domain name="bean-sec-domain" cache-type="default"></span>    
     <span class="jive-xml-tag"><authentication></span>
          <span class="jive-xml-tag"><login-module code="Remoting" flag="optional"></span>
               <span class="jive-xml-tag"><module-option name="password-stacking" value="useFirstPass"/></span>
          <span class="jive-xml-tag"></login-module></span>
          <span class="jive-xml-tag"><login-module code="org.jboss.security.auth.spi.UsersRolesLoginModule" flag="required"></span>
               <span class="jive-xml-tag"><module-option name="defaultUsersProperties" value="file:/${jboss.server.config.dir}/x-users.properties"/></span>
               <span class="jive-xml-tag"><module-option name="defaultRolesProperties" value="file:/${jboss.server.config.dir}/x-roles.properties"/></span>
               <span class="jive-xml-tag"><module-option name="usersProperties" value="file:/${jboss.server.config.dir}/x-users.properties"/></span>
               <span class="jive-xml-tag"><module-option name="rolesProperties" value="file:/${jboss.server.config.dir}/x-roles.properties"/></span>
               <span class="jive-xml-tag"><module-option name="password-stacking" value="useFirstPass"/></span>
          <span class="jive-xml-tag"></login-module></span>
     <span class="jive-xml-tag"></authentication></span>
<span class="jive-xml-tag"></security-domain></span>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>And now lets look at bean.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </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>
 
        @Resource
        <font color="navy"><b>private</b></font> EJBContext context;
 
        @Override
        @RolesAllowed(<font color="red">"bean"</font>)
        <font color="navy"><b>public</b></font> String getName ()
        <font color="navy">{</font>
                getNameFree();
        <font color="navy">}</font>
 
        @Override
        <font color="navy"><b>public</b></font> String getNameFree ()
        <font color="navy">{</font>
                String aName = <font color="red">""</font>;
                <font color="navy"><b>if</b></font> (context.getCallerPrincipal() != <font color="navy"><b>null</b></font>) <font color="navy">{</font>
                        aName = context.getCallerPrincipal().getName();
                <font color="navy">}</font>
                <font color="navy"><b>return</b></font> <font color="red">"name "</font> + aName + <font color="red">" "</font> + context.isCallerInRole(<font color="red">"bean"</font>);
        <font color="navy">}</font>
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </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;"> </p><pre class="jive-pre"><code class="jive-code"><?xml version="1.0" encoding="UTF-8"?>
<p:jboss-app xmlns:p="http://www.jboss.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee ../../xsd/jboss-app_7_0.xsd ">
    <security-domain>other</security-domain>
</p:jboss-app></code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </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;"> </p><pre class="jive-pre"><code class="jive-code jive-xml"><span class="jive-xml-tag"><?xml version="1.0" encoding="UTF-8"?></span>
<span class="jive-xml-tag"><span><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>"
    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>"
    xmlns:security="urn:security" version="3.1" impl-version="2.0"></span></span>
    <span class="jive-xml-tag"><span><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>"></span></span>
        <span class="jive-xml-tag"><security:security xmlns:security="urn:security"></span>
            <span class="jive-xml-tag"><security:security-domain></span>bean-sec-domain<span class="jive-xml-tag"></security:security-domain></span>
            <span class="jive-xml-tag"><ejb-name></span>TestBean<span class="jive-xml-tag"></ejb-name></span>
        <span class="jive-xml-tag"></security:security></span>
    <span class="jive-xml-tag"></assembly-descriptor></span>
<span class="jive-xml-tag"></jboss:ejb-jar></span>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Remote interface is realz simple with 2 methods.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Client:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">Hashtable<String, Object> p = <font color="navy"><b>new</b></font> Hashtable<String, Object>();
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>);
 
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());
 
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;"> </p><p> After this you can see </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </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;"> </p><p><strong>That's all.</strong></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </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;"> </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;"> </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 
</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;"> </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;"> </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;"> </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>