Search in sources :

Example 1 with PolicyContextException

use of javax.security.jacc.PolicyContextException in project javaee7-samples by javaee-samples.

the class SubjectServlet method doGet.

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        Subject subject = (Subject) PolicyContext.getContext("javax.security.auth.Subject.container");
        if (subject != null) {
            response.getWriter().print("Obtained subject from context.\n");
            // Get the permissions associated with the Subject we obtained
            PermissionCollection permissionCollection = getPermissionCollection(subject);
            // Resolve any potentially unresolved permissions
            permissionCollection.implies(new WebRoleRefPermission("", "nothing"));
            // Filter just the roles from all the permissions, which may include things like 
            // java.net.SocketPermission, java.io.FilePermission, and obtain the actual role names.
            Set<String> roles = filterRoles(request, permissionCollection);
            for (String role : roles) {
                response.getWriter().print("User has role " + role + "\n");
            }
        }
    } catch (PolicyContextException e) {
        e.printStackTrace(response.getWriter());
    }
}
Also used : PermissionCollection(java.security.PermissionCollection) WebRoleRefPermission(javax.security.jacc.WebRoleRefPermission) PolicyContextException(javax.security.jacc.PolicyContextException) Subject(javax.security.auth.Subject)

Example 2 with PolicyContextException

use of javax.security.jacc.PolicyContextException in project tomee by apache.

the class JaccPermissionsBuilder method install.

public void install(final PolicyContext policyContext) throws OpenEJBException {
    if (SystemInstance.get().hasProperty("openejb.geronimo")) {
        return;
    }
    try {
        final PolicyConfigurationFactory factory = PolicyConfigurationFactory.getPolicyConfigurationFactory();
        final PolicyConfiguration policy = factory.getPolicyConfiguration(policyContext.getContextID(), false);
        policy.addToExcludedPolicy(policyContext.getExcludedPermissions());
        policy.addToUncheckedPolicy(policyContext.getUncheckedPermissions());
        for (final Map.Entry<String, PermissionCollection> entry : policyContext.getRolePermissions().entrySet()) {
            policy.addToRole(entry.getKey(), entry.getValue());
        }
        policy.commit();
    } catch (final ClassNotFoundException e) {
        throw new OpenEJBException("PolicyConfigurationFactory class not found", e);
    } catch (final PolicyContextException e) {
        throw new OpenEJBException("JACC PolicyConfiguration failed: ContextId=" + policyContext.getContextID(), e);
    }
}
Also used : PermissionCollection(java.security.PermissionCollection) OpenEJBException(org.apache.openejb.OpenEJBException) PolicyConfiguration(javax.security.jacc.PolicyConfiguration) PolicyContextException(javax.security.jacc.PolicyContextException) PolicyConfigurationFactory(javax.security.jacc.PolicyConfigurationFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with PolicyContextException

use of javax.security.jacc.PolicyContextException in project hibernate-orm by hibernate.

the class StandardJaccServiceImpl method addPermission.

@Override
public void addPermission(GrantedPermission permissionDeclaration) {
    if (policyConfiguration == null) {
        policyConfiguration = locatePolicyConfiguration(contextId);
    }
    for (String grantedAction : permissionDeclaration.getPermissibleAction().getImpliedActions()) {
        final EJBMethodPermission permission = new EJBMethodPermission(permissionDeclaration.getEntityName(), grantedAction, // interfaces
        null, // arguments
        null);
        log.debugf("Adding permission [%s] to role [%s]", grantedAction, permissionDeclaration.getRole());
        try {
            policyConfiguration.addToRole(permissionDeclaration.getRole(), permission);
        } catch (PolicyContextException pce) {
            throw new HibernateException("policy context exception occurred", pce);
        }
    }
}
Also used : HibernateException(org.hibernate.HibernateException) PolicyContextException(javax.security.jacc.PolicyContextException) EJBMethodPermission(javax.security.jacc.EJBMethodPermission)

Aggregations

PolicyContextException (javax.security.jacc.PolicyContextException)3 PermissionCollection (java.security.PermissionCollection)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Subject (javax.security.auth.Subject)1 EJBMethodPermission (javax.security.jacc.EJBMethodPermission)1 PolicyConfiguration (javax.security.jacc.PolicyConfiguration)1 PolicyConfigurationFactory (javax.security.jacc.PolicyConfigurationFactory)1 WebRoleRefPermission (javax.security.jacc.WebRoleRefPermission)1 OpenEJBException (org.apache.openejb.OpenEJBException)1 HibernateException (org.hibernate.HibernateException)1