Search code examples
jbosswildflyjboss-eap-7

JBOSS EAP 7 - EJB Client user data


I have migrated my EJB application from jboss 5.0.1 to JBOSS EAP 7.

I want to pass user data from EJB client to my EJB.

I'm using this code to pass custom attribute to ejb server but it does not work anymore.

Client:

public class CustomData extends SimplePrincipal{
String userData1;
public CustomData(String userData1){
this.userData1 = userData1;
}
SecurityClient client = SecurityClientFactory.getSecurityClient();
    client.setSimple(new CustomData("MyData"), credentials.getPass());
    client.login();

Server:

 @Resource 
 SessionContext ejbCtx;

 Principal data= ejbCtx.getCallerPrincipal();
 data.getName() --- anonymous

How to fix it on new JBOSS ?


Solution

  • I have done with this way:

    Client:

    Properties properties = new Properties();
     properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
     properties.put("org.jboss.ejb.client.scoped.context", "true");
     properties.put("remote.connection.default.username", "MyData");
    

    Server:

    public class MyContainerInterceptor{
    @AroundInvoke
    public Object intercept(InvocationContext ctx) throws Exception {
    
        Connection connection = RemotingContext.getConnection();
    
        if (connection != null) {
            for (Principal p : connection.getPrincipals()) {
            if (p instanceof UserPrincipal) {
                    if (p.getName() != null && !p.getName().startsWith("$"))
                        System.out.println(p.getName()); //MyData will be printed
                }
            }
    
        }
    
        return ctx.proceed();
    }
    

    }

    Don't forget to configure container interceptor in jboss-ejb3.xml (not in ejb-jar.xml)

    <?xml version="1.0" encoding="UTF-8"?>
    

    <jee:assembly-descriptor>
        <ci:container-interceptors>
            <jee:interceptor-binding>
                <ejb-name>*</ejb-name>
                <interceptor-class>package...MyContainerInterceptor</interceptor-class>
            </jee:interceptor-binding>
        </ci:container-interceptors>
    </jee:assembly-descriptor>