Search in sources :

Example 11 with SecurityManager

use of org.apache.shiro.mgt.SecurityManager in project shiro by apache.

the class DefaultWebEnvironment method getWebSecurityManager.

public WebSecurityManager getWebSecurityManager() {
    SecurityManager sm = super.getSecurityManager();
    assertWebSecurityManager(sm);
    return (WebSecurityManager) sm;
}
Also used : WebSecurityManager(org.apache.shiro.web.mgt.WebSecurityManager) SecurityManager(org.apache.shiro.mgt.SecurityManager) WebSecurityManager(org.apache.shiro.web.mgt.WebSecurityManager)

Example 12 with SecurityManager

use of org.apache.shiro.mgt.SecurityManager in project shiro by apache.

the class ShiroModuleTest method testEventBusAware.

/**
 * @since 1.4
 * @throws Exception
 */
@Test
public void testEventBusAware() throws Exception {
    final MockRealm mockRealm = createMock(MockRealm.class);
    final ShiroModule shiroModule = new ShiroModule() {

        @Override
        protected void configureShiro() {
            bindRealm().to(MockRealm.class);
            binder().bind(MockEventBusAware.class).asEagerSingleton();
            expose(MockEventBusAware.class);
        }

        @Provides
        public MockRealm createRealm() {
            return mockRealm;
        }
    };
    Injector injector = Guice.createInjector(shiroModule);
    EventBus eventBus = injector.getInstance(EventBus.class);
    SecurityManager securityManager = injector.getInstance(SecurityManager.class);
    MockEventBusAware eventBusAware = injector.getInstance(MockEventBusAware.class);
    assertSame(eventBus, eventBusAware.eventBus);
    assertSame(eventBus, ((DefaultSecurityManager) securityManager).getEventBus());
}
Also used : SecurityManager(org.apache.shiro.mgt.SecurityManager) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) Injector(com.google.inject.Injector) DefaultEventBus(org.apache.shiro.event.support.DefaultEventBus) EventBus(org.apache.shiro.event.EventBus) Test(org.junit.Test)

Example 13 with SecurityManager

use of org.apache.shiro.mgt.SecurityManager in project shiro by apache.

the class SecureRemoteInvocationExecutor method invoke.

/*--------------------------------------------
    |               M E T H O D S               |
    ============================================*/
@SuppressWarnings({ "unchecked" })
public Object invoke(final RemoteInvocation invocation, final Object targetObject) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    try {
        SecurityManager securityManager = this.securityManager != null ? this.securityManager : SecurityUtils.getSecurityManager();
        Subject.Builder builder = new Subject.Builder(securityManager);
        String host = (String) invocation.getAttribute(SecureRemoteInvocationFactory.HOST_KEY);
        if (host != null) {
            builder.host(host);
        }
        Serializable sessionId = invocation.getAttribute(SecureRemoteInvocationFactory.SESSION_ID_KEY);
        if (sessionId != null) {
            builder.sessionId(sessionId);
        } else {
            if (log.isTraceEnabled()) {
                log.trace("RemoteInvocation did not contain a Shiro Session id attribute under " + "key [" + SecureRemoteInvocationFactory.SESSION_ID_KEY + "].  A Subject based " + "on an existing Session will not be available during the method invocation.");
            }
        }
        Subject subject = builder.buildSubject();
        return subject.execute(new Callable() {

            public Object call() throws Exception {
                return SecureRemoteInvocationExecutor.super.invoke(invocation, targetObject);
            }
        });
    } catch (ExecutionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof NoSuchMethodException) {
            throw (NoSuchMethodException) cause;
        } else if (cause instanceof IllegalAccessException) {
            throw (IllegalAccessException) cause;
        } else if (cause instanceof InvocationTargetException) {
            throw (InvocationTargetException) cause;
        } else {
            throw new InvocationTargetException(cause);
        }
    } catch (Throwable t) {
        throw new InvocationTargetException(t);
    }
}
Also used : Serializable(java.io.Serializable) SecurityManager(org.apache.shiro.mgt.SecurityManager) Subject(org.apache.shiro.subject.Subject) Callable(java.util.concurrent.Callable) ExecutionException(org.apache.shiro.subject.ExecutionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExecutionException(org.apache.shiro.subject.ExecutionException)

Example 14 with SecurityManager

use of org.apache.shiro.mgt.SecurityManager in project shiro by apache.

the class ShiroFilterFactoryBean method createInstance.

/**
 * This implementation:
 * <ol>
 * <li>Ensures the required {@link #setSecurityManager(org.apache.shiro.mgt.SecurityManager) securityManager}
 * property has been set</li>
 * <li>{@link #createFilterChainManager() Creates} a {@link FilterChainManager} instance that reflects the
 * configured {@link #setFilters(java.util.Map) filters} and
 * {@link #setFilterChainDefinitionMap(java.util.Map) filter chain definitions}</li>
 * <li>Wraps the FilterChainManager with a suitable
 * {@link org.apache.shiro.web.filter.mgt.FilterChainResolver FilterChainResolver} since the Shiro Filter
 * implementations do not know of {@code FilterChainManager}s</li>
 * <li>Sets both the {@code SecurityManager} and {@code FilterChainResolver} instances on a new Shiro Filter
 * instance and returns that filter instance.</li>
 * </ol>
 *
 * @return a new Shiro Filter reflecting any configured filters and filter chain definitions.
 * @throws Exception if there is a problem creating the AbstractShiroFilter instance.
 */
protected AbstractShiroFilter createInstance() throws Exception {
    log.debug("Creating Shiro Filter instance.");
    SecurityManager securityManager = getSecurityManager();
    if (securityManager == null) {
        String msg = "SecurityManager property must be set.";
        throw new BeanInitializationException(msg);
    }
    if (!(securityManager instanceof WebSecurityManager)) {
        String msg = "The security manager does not implement the WebSecurityManager interface.";
        throw new BeanInitializationException(msg);
    }
    FilterChainManager manager = createFilterChainManager();
    // Expose the constructed FilterChainManager by first wrapping it in a
    // FilterChainResolver implementation. The AbstractShiroFilter implementations
    // do not know about FilterChainManagers - only resolvers:
    PathMatchingFilterChainResolver chainResolver = new PathMatchingFilterChainResolver();
    chainResolver.setFilterChainManager(manager);
    // injection of the SecurityManager and FilterChainResolver:
    return new SpringShiroFilter((WebSecurityManager) securityManager, chainResolver);
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) WebSecurityManager(org.apache.shiro.web.mgt.WebSecurityManager) WebSecurityManager(org.apache.shiro.web.mgt.WebSecurityManager) SecurityManager(org.apache.shiro.mgt.SecurityManager) DefaultFilterChainManager(org.apache.shiro.web.filter.mgt.DefaultFilterChainManager) FilterChainManager(org.apache.shiro.web.filter.mgt.FilterChainManager) PathMatchingFilterChainResolver(org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver)

Example 15 with SecurityManager

use of org.apache.shiro.mgt.SecurityManager in project killbill by killbill.

the class EntitlementTestSuiteWithEmbeddedDB method configureShiro.

protected void configureShiro() {
    final Ini config = new Ini();
    config.addSection("users");
    config.getSection("users").put("EntitlementUser", "password, entitlement");
    config.addSection("roles");
    config.getSection("roles").put("entitlement", Permission.ACCOUNT_CAN_CREATE.toString() + "," + Permission.ENTITLEMENT_CAN_CREATE.toString() + "," + Permission.ENTITLEMENT_CAN_CHANGE_PLAN.toString() + "," + Permission.ENTITLEMENT_CAN_PAUSE_RESUME.toString() + "," + Permission.ENTITLEMENT_CAN_TRANSFER.toString() + "," + Permission.ENTITLEMENT_CAN_CANCEL.toString());
    // Reset the security manager
    ThreadContext.unbindSecurityManager();
    final Factory<SecurityManager> factory = new IniSecurityManagerFactory(config);
    final SecurityManager securityManager = factory.getInstance();
    SecurityUtils.setSecurityManager(securityManager);
}
Also used : IniSecurityManagerFactory(org.apache.shiro.config.IniSecurityManagerFactory) SecurityManager(org.apache.shiro.mgt.SecurityManager) Ini(org.apache.shiro.config.Ini)

Aggregations

SecurityManager (org.apache.shiro.mgt.SecurityManager)36 DefaultSecurityManager (org.apache.shiro.mgt.DefaultSecurityManager)13 IniSecurityManagerFactory (org.apache.shiro.config.IniSecurityManagerFactory)11 Test (org.junit.Test)11 Injector (com.google.inject.Injector)8 Provides (com.google.inject.Provides)6 Subject (org.apache.shiro.subject.Subject)6 Session (org.apache.shiro.session.Session)5 WebSecurityManager (org.apache.shiro.web.mgt.WebSecurityManager)5 Realm (org.apache.shiro.realm.Realm)4 IniRealm (org.apache.shiro.realm.text.IniRealm)4 DefaultWebSecurityManager (org.apache.shiro.web.mgt.DefaultWebSecurityManager)4 UnavailableSecurityManagerException (org.apache.shiro.UnavailableSecurityManagerException)3 Ini (org.apache.shiro.config.Ini)3 DelegatingSubject (org.apache.shiro.subject.support.DelegatingSubject)3 AfterClass (org.junit.AfterClass)3 Callable (java.util.concurrent.Callable)2 ServletContext (javax.servlet.ServletContext)2 ConfigurationException (org.apache.shiro.config.ConfigurationException)2 ShiroModuleTest (org.apache.shiro.guice.ShiroModuleTest)2