This content has been marked as final. 
    
Show                 1 reply
    
- 
        1. Re: How to login programmatically from inside the JBoss 7 server?nmoelholm Oct 8, 2013 9:37 AM (in response to nmoelholm)I have something working now. Not quite sure if it is "best practice". What do you say (broadcast question ) ? So ... this is how I run an EJB method under another principal: runAs("other", "duke", "duke123!", new PrivilegedAction<Void>() { @Override public Void run() { friendService.ping(); // EJB invocation ( Inside it it knows about the correct duke principal ) return null; } });The ping() method is secured with @RolesAllowed("ONLY_JAVA_LOVERS"). It ensures that the EJB cannot be invoked with the anonymous principal etc. …and a peak into the runAs method: private void runAs( String securityDomain, String userName, String password, PrivilegedAction<Void> action) { SecurityContext originalSecurityContext = SecurityContextAssociation.getSecurityContext(); try { // Perform programmatic login LoginContext ctx = new LoginContext(securityDomain, new UsernamePasswordHandler(userName, password.toCharArray())); ctx.login(); // Bind principal/subject to the current thread SecurityContext sc = SecurityContextFactory.createSecurityContext(securityDomain); sc.getUtil().createSubjectInfo(ctx.getSubject().getPrincipals().iterator().next(), password, ctx.getSubject()); SecurityContextAssociation.setSecurityContext(sc); // Run the action with the new login details Subject.doAs(ctx.getSubject(), action); } catch (Exception e) { throw new RuntimeException(e); } finally { SecurityContextAssociation.setSecurityContext(originalSecurityContext); } }The code doesn't work without lines 9+10+11. So this is the nasty part I guess: manually binding stuff to the thread. Lines 02+18 is there just to compensate..... Also, without Subject.doAs (line 14) the EJB container rejects to invoke the method due to authorization issues. Community - let me know if this can be done better, Thanks, Nicky 
