Search in sources :

Example 6 with AccessControlContext

use of java.security.AccessControlContext in project robovm by robovm.

the class myPrivilegedExceptionAction method test_doAsPrivileged_01.

/**
     * javax.security.auth.Subject#doAsPrivileged(Subject subject,
     *                                                   PrivilegedAction action,
     *                                                   AccessControlContext acc)
     */
public void test_doAsPrivileged_01() {
    Subject subj = new Subject();
    PrivilegedAction<Object> pa = new myPrivilegedAction();
    PrivilegedAction<Object> paNull = null;
    AccessControlContext acc = AccessController.getContext();
    try {
        Object obj = Subject.doAsPrivileged(null, pa, acc);
    } catch (Exception e) {
        fail("Unexpected exception: " + e);
    }
    try {
        Object obj = Subject.doAsPrivileged(subj, pa, acc);
    } catch (Exception e) {
        fail("Unexpected exception: " + e);
    }
    try {
        Object obj = Subject.doAsPrivileged(subj, paNull, acc);
        fail("NullPointerException wasn't thrown");
    } catch (NullPointerException npe) {
    }
}
Also used : AccessControlContext(java.security.AccessControlContext) Subject(javax.security.auth.Subject) PrivilegedActionException(java.security.PrivilegedActionException)

Example 7 with AccessControlContext

use of java.security.AccessControlContext in project spring-framework by spring-projects.

the class AbstractBeanFactory method registerDisposableBeanIfNecessary.

/**
	 * Add the given bean to the list of disposable beans in this factory,
	 * registering its DisposableBean interface and/or the given destroy method
	 * to be called on factory shutdown (if applicable). Only applies to singletons.
	 * @param beanName the name of the bean
	 * @param bean the bean instance
	 * @param mbd the bean definition for the bean
	 * @see RootBeanDefinition#isSingleton
	 * @see RootBeanDefinition#getDependsOn
	 * @see #registerDisposableBean
	 * @see #registerDependentBean
	 */
protected void registerDisposableBeanIfNecessary(String beanName, Object bean, RootBeanDefinition mbd) {
    AccessControlContext acc = (System.getSecurityManager() != null ? getAccessControlContext() : null);
    if (!mbd.isPrototype() && requiresDestruction(bean, mbd)) {
        if (mbd.isSingleton()) {
            // Register a DisposableBean implementation that performs all destruction
            // work for the given bean: DestructionAwareBeanPostProcessors,
            // DisposableBean interface, custom destroy method.
            registerDisposableBean(beanName, new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), acc));
        } else {
            // A bean with a custom scope...
            Scope scope = this.scopes.get(mbd.getScope());
            if (scope == null) {
                throw new IllegalStateException("No Scope registered for scope name '" + mbd.getScope() + "'");
            }
            scope.registerDestructionCallback(beanName, new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), acc));
        }
    }
}
Also used : AccessControlContext(java.security.AccessControlContext) Scope(org.springframework.beans.factory.config.Scope)

Example 8 with AccessControlContext

use of java.security.AccessControlContext in project spring-framework by spring-projects.

the class CallbacksSecurityTests method testContainerPrivileges.

@Test
public void testContainerPrivileges() throws Exception {
    AccessControlContext acc = provider.getAccessControlContext();
    AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

        @Override
        public Object run() throws Exception {
            beanFactory.getBean("working-factory-method");
            beanFactory.getBean("container-execution");
            return null;
        }
    }, acc);
}
Also used : AccessControlContext(java.security.AccessControlContext) BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeansException(org.springframework.beans.BeansException) Test(org.junit.Test)

Example 9 with AccessControlContext

use of java.security.AccessControlContext in project spring-framework by spring-projects.

the class CallbacksSecurityTests method testSecuritySanity.

@Test
public void testSecuritySanity() throws Exception {
    AccessControlContext acc = provider.getAccessControlContext();
    try {
        acc.checkPermission(new PropertyPermission("*", "read"));
        fail("Acc should not have any permissions");
    } catch (SecurityException se) {
    // expected
    }
    final CustomCallbackBean bean = new CustomCallbackBean();
    final Method method = bean.getClass().getMethod("destroy");
    method.setAccessible(true);
    try {
        AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

            @Override
            public Object run() throws Exception {
                method.invoke(bean);
                return null;
            }
        }, acc);
        fail("expected security exception");
    } catch (Exception ex) {
    }
    final Class<ConstructorBean> cl = ConstructorBean.class;
    try {
        AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

            @Override
            public Object run() throws Exception {
                return cl.newInstance();
            }
        }, acc);
        fail("expected security exception");
    } catch (Exception ex) {
    }
}
Also used : AccessControlContext(java.security.AccessControlContext) PropertyPermission(java.util.PropertyPermission) Method(java.lang.reflect.Method) CustomCallbackBean(org.springframework.beans.factory.support.security.support.CustomCallbackBean) BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeansException(org.springframework.beans.BeansException) ConstructorBean(org.springframework.beans.factory.support.security.support.ConstructorBean) Test(org.junit.Test)

Example 10 with AccessControlContext

use of java.security.AccessControlContext in project AsmackService by rtreffer.

the class Subject method doAs_PrivilegedExceptionAction.

// instantiates a new context and passes it to AccessController
@SuppressWarnings("unchecked")
private static Object doAs_PrivilegedExceptionAction(Subject subject, PrivilegedExceptionAction action, final AccessControlContext context) throws PrivilegedActionException {
    AccessControlContext newContext;
    final SubjectDomainCombiner combiner;
    if (subject == null) {
        // performance optimization
        // if subject is null there is nothing to combine
        combiner = null;
    } else {
        combiner = new SubjectDomainCombiner(subject);
    }
    PrivilegedAction<AccessControlContext> dccAction = new PrivilegedAction<AccessControlContext>() {

        public AccessControlContext run() {
            return new AccessControlContext(context, combiner);
        }
    };
    newContext = AccessController.doPrivileged(dccAction);
    return AccessController.doPrivileged(action, newContext);
}
Also used : AccessControlContext(java.security.AccessControlContext) PrivilegedAction(java.security.PrivilegedAction)

Aggregations

AccessControlContext (java.security.AccessControlContext)96 Subject (javax.security.auth.Subject)23 ProtectionDomain (java.security.ProtectionDomain)20 PrivilegedAction (java.security.PrivilegedAction)18 Permissions (java.security.Permissions)13 PrivilegedActionException (java.security.PrivilegedActionException)12 IOException (java.io.IOException)10 SocketPermission (java.net.SocketPermission)10 Test (org.testng.annotations.Test)8 Principal (java.security.Principal)7 DatagramSocket (java.net.DatagramSocket)5 InetAddress (java.net.InetAddress)4 MulticastSocket (java.net.MulticastSocket)4 CodeSource (java.security.CodeSource)4 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)4 ExecutorService (java.util.concurrent.ExecutorService)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)4 ReflectionException (javax.management.ReflectionException)4 Test (org.junit.Test)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3