2 Replies Latest reply on Feb 9, 2012 10:11 AM by method_ben_qc

    How Can I push/pop the subjectContext

    method_ben_qc Newbie

      Hi,

       

      In JBoss 4.2.3, I used the class org.jboss.security.SecurityAssociation to push the subjectContext. Here's the code:

       

      public static void pushSubjectContext(String principal, String credential)

      {

          SecurityAssociation.pushSubjectContext(new Subject(), new SimplePrincipal(principal), credential);

      }

       

      In JBoss AS 7, the class org.jboss.security.SecurityAssociation doesn't exist anymore. I thought its has been replaced by org.jboss.security.SecurityContextAssociation and I can use it like this:

       

      public static void pushSubjectContext(String principal, String credential)

      {

          SecurityContextAssociation.pushRunAsIdentity(new RunAsIdentity(principal, credential));

      }

       

      But it doesn't work!

       

      I would like to to know how I can push/pop a new subject context. I use this method to change the subject context for calling another EJB which the security is more strict

       

      @SecurityDomain("a_security_domain")

      @PermitAll

      public ejb_1

      {

      ....

          pushSubjectContext("system", "admin")

          call ejb_2.method;

          popSubjectContext();

      ...

      }

       

      @SecurityDomain("a_security_domain")

      @RolesAllowed( {"system"})

      @DeclareRoles( {"admin"})

      public ejb_2

      {

      ....

          public void method()

      ...

      }

       

       

      Thank you

        • 1. Re: How Can I push/pop the subjectContext
          method_ben_qc Newbie

          The class org.jboss.security.client.SecurityClient is used instead of org.jboss.security.SecurityAssociation for pushing/poping the subject context on the stack.

           

          Here's the way to push/pop the subject context:

           

              public static void pushSubjectContext(String principal, String credential)

              {

                  try

                  {

                          SecurityClient securityClient = getSecurityClient();

                         

                          securityClient.setSimple(principal, credential);

                          securityClient.login();

                  }

                  catch(Exception e)

                  {

                      throw new InternalException(e);

                  }

              }

           

              public static void popSubjectContext()

              {

                     try

                      {

                          getSecurityClient().logout();

                      }

                      catch(Exception e)

                      {

                          throw new InternalException(e);

                      }

              }