<!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;">
    Re: Every single remote ejb call starts full authentication process with SecurityDomain cache-type="default"
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="https://community.jboss.org/people/jason.greene">Jason Greene</a> in <i>JBoss AS 7 Development</i> - <a href="https://community.jboss.org/message/724714#724714">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>In remoting authentication is PER connection. Inside of a connection you have have multiple channels which are intended for multiple services (e.g. ejb, jmx, etc). All are intended to share the same credentials so that auth is only done once on initial connect. If you need to dynamically handle different users, then the best way is to manage all of the connections yourself (this will also address the close problem you mention in the other thread). So basically all you do is setup your connections lazily and per user, and then associate them to the ejb client context before a proxy is invoked (e.g. using ThreadLocalContextSelector like you are already doing). If you prefer you could also write your own context selector that uses the username as an entry in a global index.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Something like this could be done for a connection per-thread model (although ideally you don't want duplicate connections for the same user):</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Global Shared Stuff for the entire VM</p><pre class="jive-pre"><code class="jive-code jive-java">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">// Gloabl Shared Stuff</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">// create the endpoint</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>final</b></font> Endpoint endpoint = Remoting.createEndpoint(<font color="red">"my-client"</font>, OptionMap.create(Options.THREAD_DAEMON, <font color="navy"><b>true</b></font>));
&#160;
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">// Have the provider use non-ssl connections</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; endpoint.addConnectionProvider(<font color="red">"remote"</font>, <font color="navy"><b>new</b></font> RemoteConnectionProviderFactory(), OptionMap.create(Options.SSL_ENABLED, <font color="navy"><b>false</b></font>));
&#160;
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">// Setup a global thread-local selector, which willl allow you to have a different connection per-thread</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.selector = <font color="navy"><b>new</b></font> ThreadLocalContextSelector&lt;EJBClientContext&gt;(<font color="navy"><b>new</b></font> ThreadLocal&lt;EJBClientContext&gt;()); 
&#160;&#160;&#160;&#160;&#160;&#160;&#160; EJBClientContext.setSelector(this.selector);
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Per-connection setup</p><pre class="jive-pre"><code class="jive-code jive-java">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">// Where to connect</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>final</b></font> URI connectionURI = <font color="navy"><b>new</b></font> URI(<font color="red">"remote://localhost:4447"</font>);
&#160;&#160;&#160;&#160;&#160;&#160;&#160; 
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">// Disable local auth, and allow plain text passwords over the wire (clear text is needed for JAAS / security domains)</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; OptionMap.Builder builder = OptionMap.builder().set(Options.SASL_POLICY_NOANONYMOUS, <font color="navy"><b>true</b></font>);
&#160;&#160;&#160;&#160;&#160;&#160;&#160; builder.set(Options.SASL_POLICY_NOPLAINTEXT, <font color="navy"><b>false</b></font>);
&#160;&#160;&#160;&#160;&#160;&#160;&#160; builder.set(Options.SASL_DISALLOWED_MECHANISMS, Sequence.of(<font color="red">"JBOSS-LOCAL-USER"</font>));
&#160;&#160;&#160;&#160;&#160;&#160;&#160; 
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">// Create the connection</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>final</b></font> IoFuture&lt;Connection&gt; futureConnection = endpoint.connect(connectionURI, builder.getMap(), <font color="navy"><b>new</b></font> AuthenticationCallbackHandler(username, password));
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">// wait for the connection to be established</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>final</b></font> Connection connection = IoFutureHelper.get(futureConnection, 5000, TimeUnit.MILLISECONDS);
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">// create a remoting EJB receiver for this connection</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>final</b></font> EJBReceiver receiver = <font color="navy"><b>new</b></font> RemotingConnectionEJBReceiver(connection);
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">// associate it with a new client context</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; EJBClientContext context = EJBClientContext.create();
&#160;&#160;&#160;&#160;&#160;&#160;&#160; context.registerEJBReceiver(receiver);
&#160;&#160;&#160;&#160;&#160;&#160;&#160; 
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="darkgreen">// Set this thread to use this context</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; this.selector.setCurrent(context);
</code></pre></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="https://community.jboss.org/message/724714#724714">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in JBoss AS 7 Development at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2225">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


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

</div>

</body>
</html>