Unable to set credentials when doing a local EJB call in JBoss EAP 6

Solution Verified - Updated -

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 6.x

Issue

  • Users are trying to call a secured EJB using the local interface, but they can not find out how to provide the credentials when instantiating the InitialContext.

Resolution

1) Define EJB3 Domain Security for refer article which describes How to configure EJB3 Domain Security in EAP 6
2) User can use this approach What is the replacement for SecurityAssociation.setPrincipal() on JBoss EAP 6.x to establish a security context for calling a secured local ejb.

Example Code

    public String callTestRole() throws NamingException, LoginException {
        // TODO Auto-generated method stub

        UsernamePasswordHandler handler = new UsernamePasswordHandler("admin", "admin123@");
        LoginContext lc = new LoginContext("MyDomain", handler);
        lc.login();     

        context = new InitialContext();
        System.out.println("\n\tGot initial Context: " + context);
        System.out.println("Performing Lookup .............................");

        ejb = (SecureEjbExampleLocal) context.lookup("java:global/CallingSecureEJBLocally/SecureEjbExample!com.redhat.secure.ejb.SecureEjbExampleLocal");
        System.out.println("Calling callNonSecure() method of SecureEjbExampleLocal");
        String nonSecured=ejb.callNonSecure();
        System.out.println("method non secured called "+nonSecured);
        System.out.println("Calling testRole() method of SecureEjbExampleLocal");
        String secured=ejb.testRole();
        System.out.println("method secured called "+secured);
        return null;

Additional Document
Problems calling Secured EJB remotely from a Standalone client in EAP 6

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments