Search in sources :

Example 6 with RunAs

use of javax.annotation.security.RunAs in project Payara by payara.

the class RunAsHandler method processAnnotation.

protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo, WebComponentContext[] webCompContexts) throws AnnotationProcessorException {
    RunAs runAsAn = (RunAs) ainfo.getAnnotation();
    for (WebComponentContext webCompContext : webCompContexts) {
        WebComponentDescriptor webDesc = webCompContext.getDescriptor();
        // override by xml
        if (webDesc.getRunAsIdentity() != null) {
            continue;
        }
        String roleName = runAsAn.value();
        Role role = new Role(roleName);
        // add Role if not exists
        webDesc.getWebBundleDescriptor().addRole(role);
        RunAsIdentityDescriptor runAsDesc = new RunAsIdentityDescriptor();
        runAsDesc.setRoleName(roleName);
        webDesc.setRunAsIdentity(runAsDesc);
    }
    return getDefaultProcessedResult();
}
Also used : Role(org.glassfish.security.common.Role) WebComponentDescriptor(com.sun.enterprise.deployment.WebComponentDescriptor) WebComponentContext(com.sun.enterprise.deployment.annotation.context.WebComponentContext) RunAsIdentityDescriptor(com.sun.enterprise.deployment.RunAsIdentityDescriptor) RunAs(javax.annotation.security.RunAs)

Example 7 with RunAs

use of javax.annotation.security.RunAs in project tomcat70 by apache.

the class WebAnnotationSet method loadApplicationServletAnnotations.

/**
 * Process the annotations for the servlets.
 *
 * @param context The context which will have its annotations processed
 */
protected static void loadApplicationServletAnnotations(Context context) {
    Container[] children = context.findChildren();
    for (Container child : children) {
        if (child instanceof Wrapper) {
            Wrapper wrapper = (Wrapper) child;
            if (wrapper.getServletClass() == null) {
                continue;
            }
            Class<?> clazz = Introspection.loadClass(context, wrapper.getServletClass());
            if (clazz == null) {
                continue;
            }
            loadClassAnnotation(context, clazz);
            loadFieldsAnnotation(context, clazz);
            loadMethodsAnnotation(context, clazz);
            /* Process RunAs annotation which can be only on servlets.
                 * Ref JSR 250, equivalent to the run-as element in
                 * the deployment descriptor
                 */
            RunAs runAs = clazz.getAnnotation(RunAs.class);
            if (runAs != null) {
                wrapper.setRunAs(runAs.value());
            }
            // Process ServletSecurity annotation
            ServletSecurity servletSecurity = clazz.getAnnotation(ServletSecurity.class);
            if (servletSecurity != null) {
                context.addServletSecurity(new ApplicationServletRegistration(wrapper, context), new ServletSecurityElement(servletSecurity));
            }
        }
    }
}
Also used : Wrapper(org.apache.catalina.Wrapper) Container(org.apache.catalina.Container) ServletSecurity(javax.servlet.annotation.ServletSecurity) RunAs(javax.annotation.security.RunAs) ApplicationServletRegistration(org.apache.catalina.core.ApplicationServletRegistration) ServletSecurityElement(javax.servlet.ServletSecurityElement)

Example 8 with RunAs

use of javax.annotation.security.RunAs in project tomee by apache.

the class RunAsRule method apply.

@Override
public Statement apply(final Statement base, final Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            final RunAs annotation = description.getAnnotation(RunAs.class);
            final As as = description.getAnnotation(As.class);
            String currentRole = role.get();
            // no more needed
            role.remove();
            if (annotation == null && as == null && currentRole == null) {
                base.evaluate();
                return;
            }
            final BeanContext beanContext = getBeanContext();
            if (currentRole == null) {
                if (annotation == null) {
                    currentRole = as.value();
                } else {
                    currentRole = annotation.value();
                }
            }
            final String runAs = beanContext.getRunAs();
            final String runAsUser = beanContext.getRunAsUser();
            beanContext.setRunAs(currentRole);
            final ThreadContext old = ThreadContext.enter(new ThreadContext(beanContext, null));
            try {
                base.evaluate();
            } finally {
                // reset for next test
                ThreadContext.exit(old);
                beanContext.setRunAs(runAs);
                beanContext.setRunAsUser(runAsUser);
            }
        }
    };
}
Also used : BeanContext(org.apache.openejb.BeanContext) RunAs(javax.annotation.security.RunAs) Statement(org.junit.runners.model.Statement) RunAs(javax.annotation.security.RunAs) ThreadContext(org.apache.openejb.core.ThreadContext)

Aggregations

RunAs (javax.annotation.security.RunAs)8 RunAsIdentityDescriptor (com.sun.enterprise.deployment.RunAsIdentityDescriptor)5 Role (org.glassfish.security.common.Role)5 EjbDescriptor (com.sun.enterprise.deployment.EjbDescriptor)2 WebComponentDescriptor (com.sun.enterprise.deployment.WebComponentDescriptor)2 EjbContext (com.sun.enterprise.deployment.annotation.context.EjbContext)2 WebComponentContext (com.sun.enterprise.deployment.annotation.context.WebComponentContext)2 ServletSecurityElement (javax.servlet.ServletSecurityElement)2 ServletSecurity (javax.servlet.annotation.ServletSecurity)2 Container (org.apache.catalina.Container)1 Wrapper (org.apache.catalina.Wrapper)1 ApplicationServletRegistration (org.apache.catalina.core.ApplicationServletRegistration)1 BeanContext (org.apache.openejb.BeanContext)1 ThreadContext (org.apache.openejb.core.ThreadContext)1 EEModuleClassDescription (org.jboss.as.ee.component.EEModuleClassDescription)1 RunAsPrincipal (org.jboss.ejb3.annotation.RunAsPrincipal)1 Statement (org.junit.runners.model.Statement)1