Search in sources :

Example 1 with AuthorizingRealm

use of org.apache.shiro.realm.AuthorizingRealm in project ddf by codice.

the class TestResourceUsagePlugin method setSubject.

private void setSubject(String expectedUsername) {
    AuthorizingRealm realm = mock(AuthorizingRealm.class);
    when(realm.getName()).thenReturn("mockRealm");
    when(realm.isPermitted(any(PrincipalCollection.class), any(Permission.class))).thenReturn(true);
    Collection<Realm> realms = new ArrayList<>();
    realms.add(realm);
    DefaultSecurityManager manager = new DefaultSecurityManager();
    manager.setRealms(realms);
    SimplePrincipalCollection principalCollection = new SimplePrincipalCollection(new Principal() {

        @Override
        public String getName() {
            return expectedUsername;
        }

        @Override
        public String toString() {
            return expectedUsername;
        }
    }, realm.getName());
    subject = new MockSubject(manager, principalCollection);
}
Also used : Permission(org.apache.shiro.authz.Permission) ArrayList(java.util.ArrayList) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) Matchers.anyString(org.mockito.Matchers.anyString) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) AuthorizingRealm(org.apache.shiro.realm.AuthorizingRealm) Realm(org.apache.shiro.realm.Realm) AuthorizingRealm(org.apache.shiro.realm.AuthorizingRealm) Principal(java.security.Principal)

Example 2 with AuthorizingRealm

use of org.apache.shiro.realm.AuthorizingRealm in project shiro by apache.

the class AllSuccessfulStrategyTest method beforeAttemptRealmDoesntSupportToken.

@Test(expected = UnsupportedTokenException.class)
public void beforeAttemptRealmDoesntSupportToken() {
    Realm notSupportingRealm = new AuthorizingRealm() {

        public boolean supports(AuthenticationToken token) {
            return false;
        }

        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
            return null;
        }

        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
            return null;
        }
    };
    strategy.beforeAttempt(notSupportingRealm, null, null);
}
Also used : AuthenticationToken(org.apache.shiro.authc.AuthenticationToken) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) Realm(org.apache.shiro.realm.Realm) SimpleAccountRealm(org.apache.shiro.realm.SimpleAccountRealm) AuthorizingRealm(org.apache.shiro.realm.AuthorizingRealm) AuthorizingRealm(org.apache.shiro.realm.AuthorizingRealm) Test(org.junit.Test)

Example 3 with AuthorizingRealm

use of org.apache.shiro.realm.AuthorizingRealm in project ddf by codice.

the class OperationPluginTest method setup.

@Before
public void setup() {
    plugin = new OperationPlugin();
    plugin.setPermissions(new PermissionsImpl());
    AuthorizingRealm realm = mock(AuthorizingRealm.class);
    when(realm.getName()).thenReturn("mockRealm");
    when(realm.isPermitted(any(PrincipalCollection.class), any(Permission.class))).then(makeDecision());
    Collection<Realm> realms = new ArrayList<Realm>();
    realms.add(realm);
    DefaultSecurityManager manager = new DefaultSecurityManager();
    manager.setRealms(realms);
    SimplePrincipalCollection principalCollection = new SimplePrincipalCollection(new Principal() {

        @Override
        public String getName() {
            return "testuser";
        }
    }, realm.getName());
    subject = new MockSubject(manager, principalCollection);
}
Also used : ArrayList(java.util.ArrayList) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) AuthorizingRealm(org.apache.shiro.realm.AuthorizingRealm) PermissionsImpl(ddf.security.permission.impl.PermissionsImpl) CollectionPermission(ddf.security.permission.CollectionPermission) Permission(org.apache.shiro.authz.Permission) KeyValueCollectionPermission(ddf.security.permission.KeyValueCollectionPermission) Realm(org.apache.shiro.realm.Realm) AuthorizingRealm(org.apache.shiro.realm.AuthorizingRealm) Principal(java.security.Principal) Before(org.junit.Before)

Example 4 with AuthorizingRealm

use of org.apache.shiro.realm.AuthorizingRealm in project ddf by codice.

the class SecurityManagerImpl method createPrincipalFromToken.

/**
     * Creates a new principal object from an incoming security token.
     *
     * @param token SecurityToken that contains the principals.
     * @return new SimplePrincipalCollection
     */
private SimplePrincipalCollection createPrincipalFromToken(SecurityToken token) {
    SimplePrincipalCollection principals = new SimplePrincipalCollection();
    for (Realm curRealm : realms) {
        LOGGER.debug("Configuring settings for realm name: {} type: {}", curRealm.getName(), curRealm.getClass().toString());
        LOGGER.debug("Is authorizer: {}, is AuthorizingRealm: {}", curRealm instanceof Authorizer, curRealm instanceof AuthorizingRealm);
        SecurityAssertion securityAssertion = null;
        try {
            securityAssertion = new SecurityAssertionImpl(token, usernameAttributeList);
            Principal principal = securityAssertion.getPrincipal();
            if (principal != null) {
                principals.add(principal.getName(), curRealm.getName());
            }
        } catch (Exception e) {
            LOGGER.warn("Encountered error while trying to get the Principal for the SecurityToken. Security functions may not work properly.", e);
        }
        if (securityAssertion != null) {
            principals.add(securityAssertion, curRealm.getName());
        }
    }
    return principals;
}
Also used : Authorizer(org.apache.shiro.authz.Authorizer) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SecurityAssertion(ddf.security.assertion.SecurityAssertion) Realm(org.apache.shiro.realm.Realm) AuthorizingRealm(org.apache.shiro.realm.AuthorizingRealm) AuthorizingRealm(org.apache.shiro.realm.AuthorizingRealm) Principal(java.security.Principal) SecurityServiceException(ddf.security.service.SecurityServiceException) SecurityAssertionImpl(ddf.security.assertion.impl.SecurityAssertionImpl)

Example 5 with AuthorizingRealm

use of org.apache.shiro.realm.AuthorizingRealm in project nutzboot by nutzam.

the class ShiroEnvStarter method getWebSecurityManager.

@IocBean(name = "shiroWebSecurityManager")
public WebSecurityManager getWebSecurityManager() {
    DefaultWebSecurityManager webSecurityManager = new DefaultWebSecurityManager() {

        protected SubjectContext resolveSession(SubjectContext context) {
            if (context.resolveSession() != null) {
                return context;
            }
            try {
                Session session = resolveContextSession(context);
                if (session != null) {
                    context.setSession(session);
                }
            } catch (InvalidSessionException e) {
            }
            return context;
        }
    };
    // Shiro Session相关
    if (conf.getBoolean(PROP_SESSION_ENABLE, true)) {
        webSecurityManager.setSessionManager(ioc.get(WebSessionManager.class, "shiroWebSessionManager"));
    }
    List<Realm> realms = new ArrayList<>();
    for (String realmName : ioc.getNamesByType(Realm.class)) {
        AuthorizingRealm realm = ioc.get(AuthorizingRealm.class, realmName);
        if (conf.getBoolean(PROP_REALM_CACHE_ENABLE, false)) {
            realm.setCacheManager(ioc.get(CacheManager.class, "shiroCacheManager"));
        }
        realms.add(realm);
    }
    if (ioc.has("authenticationStrategy")) {
        ModularRealmAuthenticator modularRealmAuthenticator = new ModularRealmAuthenticator();
        modularRealmAuthenticator.setAuthenticationStrategy(ioc.get(AuthenticationStrategy.class, "authenticationStrategy"));
        if (realms.size() > 0)
            modularRealmAuthenticator.setRealms(realms);
        webSecurityManager.setAuthenticator(modularRealmAuthenticator);
    }
    if (realms.size() > 0)
        webSecurityManager.setRealms(realms);
    webSecurityManager.setRememberMeManager(ioc.get(RememberMeManager.class, "shiroRememberMeManager"));
    return webSecurityManager;
}
Also used : InvalidSessionException(org.apache.shiro.session.InvalidSessionException) RememberMeManager(org.apache.shiro.mgt.RememberMeManager) CookieRememberMeManager(org.apache.shiro.web.mgt.CookieRememberMeManager) SubjectContext(org.apache.shiro.subject.SubjectContext) DefaultWebSecurityManager(org.apache.shiro.web.mgt.DefaultWebSecurityManager) AuthenticationStrategy(org.apache.shiro.authc.pam.AuthenticationStrategy) ArrayList(java.util.ArrayList) AuthorizingRealm(org.apache.shiro.realm.AuthorizingRealm) ModularRealmAuthenticator(org.apache.shiro.authc.pam.ModularRealmAuthenticator) LCacheManager(org.nutz.plugins.cache.impl.lcache.LCacheManager) MemoryConstrainedCacheManager(org.apache.shiro.cache.MemoryConstrainedCacheManager) RedisCacheManager(org.nutz.plugins.cache.impl.redis.RedisCacheManager) CacheManager(org.apache.shiro.cache.CacheManager) EhCacheManager(org.apache.shiro.cache.ehcache.EhCacheManager) DefaultWebSessionManager(org.apache.shiro.web.session.mgt.DefaultWebSessionManager) WebSessionManager(org.apache.shiro.web.session.mgt.WebSessionManager) Realm(org.apache.shiro.realm.Realm) AuthorizingRealm(org.apache.shiro.realm.AuthorizingRealm) Session(org.apache.shiro.session.Session) IocBean(org.nutz.ioc.loader.annotation.IocBean)

Aggregations

AuthorizingRealm (org.apache.shiro.realm.AuthorizingRealm)8 Realm (org.apache.shiro.realm.Realm)7 ArrayList (java.util.ArrayList)5 Principal (java.security.Principal)4 SimplePrincipalCollection (org.apache.shiro.subject.SimplePrincipalCollection)4 Permission (org.apache.shiro.authz.Permission)3 DefaultSecurityManager (org.apache.shiro.mgt.DefaultSecurityManager)3 PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)3 CollectionPermission (ddf.security.permission.CollectionPermission)2 KeyValueCollectionPermission (ddf.security.permission.KeyValueCollectionPermission)2 PermissionsImpl (ddf.security.permission.impl.PermissionsImpl)2 Metacard (ddf.catalog.data.Metacard)1 ResultImpl (ddf.catalog.data.impl.ResultImpl)1 DeleteRequest (ddf.catalog.operation.DeleteRequest)1 ResourceRequest (ddf.catalog.operation.ResourceRequest)1 CreateRequestImpl (ddf.catalog.operation.impl.CreateRequestImpl)1 DeleteResponseImpl (ddf.catalog.operation.impl.DeleteResponseImpl)1 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)1 QueryResponseImpl (ddf.catalog.operation.impl.QueryResponseImpl)1 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)1