Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I came up with the following solution to add roles programmatically after login, which works at least on GlassFish 3.1.2 build 23.</p> <pre><code>import com.sun.enterprise.security.SecurityContext; import com.sun.enterprise.security.web.integration.PrincipalGroupFactory; import java.security.Principal; import java.util.Set; import javax.security.auth.Subject; import org.glassfish.security.common.Group; public class GlassFishUtils { public static void addGroupToCurrentUser(String groupName, String realmName) { Subject subject = SecurityContext.getCurrent().getSubject(); Set&lt;Principal&gt; principals = subject.getPrincipals(); Group group = PrincipalGroupFactory.getGroupInstance(groupName, realmName); if (!principals.contains(group)) principals.add(group); } } </code></pre> <p>You will need to add <code>security.jar</code> and <code>common-util.jar</code> from GlassFish to your project libraries.</p> <p>And don't forget to create a <code>&lt;security-role&gt;</code> section in your web.xml for the roles you wish to add.</p> <p>Note that I am using functionality which does not appear to be part of a published stable API, so there is no guarantee that this will keep working in future releases of GlassFish.</p> <p>I got the information on how to add roles from the source code of <code>sun.appserv.security.AppservPasswordLoginModule.commit()</code> of GlassFish. If a future GlassFish release breaks my code, this function would be a good place to start in order to find out how to fix it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload