title: Login a user programmatically via JAAS
body: <p>I'm trying to run code within JBoss Container under a different authentication by programatically logging in a user like that (stripped exception handling):</p>
  
  <pre><code>LoginContext ctx = ctx = 
      new LoginContext("MyLoginSchema", 
          new UsernamePasswordCallbackHandler("newuser", "")
      );
  ctx.login();
  
  Subject.doAs(ctx.getSubject(), new PrivilegedAction&lt;T&gt;() {
      @Override
      public T run() {
          Subject.getSubject(AccessController.getContext());
          InitialContext ic = new InitialContext();
          EJBContext sctxLookup = (EJBContext) ic.lookup("java:comp/EJBContext");
          Principal principal = sctxLookup.getCallerPrincipal();
      }           
  }); 
  </code></pre>
  
  <p>Login of <code>newuser</code> works (Call of <code>LoginModule</code> was successful) but <code>Subject.doAs()</code> doesn't associate the new Subject with the <code>EJBContext</code>. The code in the <code>run()</code>-Method still fetches the old user's principal from <code>EJBContext</code>.</p>
  
  <p>I tested another method of retrieving the logged in user but same behavior here:</p>
  
  <pre><code>Subject caller = (Subject) PolicyContext.getContext("javax.security.auth.Subject.container");
  </code></pre>
  
  <p>Any ideas?</p>