Search in sources :

Example 71 with PrincipalCollection

use of org.apache.shiro.subject.PrincipalCollection in project production_ssm by megagao.

the class CustomRealm method clearCached.

// 清除缓存
public void clearCached() {
    PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();
    super.clearCache(principals);
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection)

Example 72 with PrincipalCollection

use of org.apache.shiro.subject.PrincipalCollection in project dubidubi by lzzzz4.

the class LoginRealm method clearCache.

// 清除缓存
public void clearCache() {
    PrincipalCollection principalCollection = SecurityUtils.getSubject().getPrincipals();
    super.clearCache(principalCollection);
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection)

Example 73 with PrincipalCollection

use of org.apache.shiro.subject.PrincipalCollection in project shiro by apache.

the class DefaultSubjectDAO method mergePrincipals.

/**
 * Merges the Subject's current {@link org.apache.shiro.subject.Subject#getPrincipals()} with whatever may be in
 * any available session.  Only updates the Subject's session if the session does not match the current principals
 * state.
 *
 * @param subject the Subject for which principals will potentially be merged into the Subject's session.
 */
protected void mergePrincipals(Subject subject) {
    // merge PrincipalCollection state:
    PrincipalCollection currentPrincipals = null;
    // A more comprehensive review / cleaning of runAs should be performed for Shiro 1.3 / 2.0 +
    if (subject.isRunAs() && subject instanceof DelegatingSubject) {
        try {
            Field field = DelegatingSubject.class.getDeclaredField("principals");
            field.setAccessible(true);
            currentPrincipals = (PrincipalCollection) field.get(subject);
        } catch (Exception e) {
            throw new IllegalStateException("Unable to access DelegatingSubject principals property.", e);
        }
    }
    if (currentPrincipals == null || currentPrincipals.isEmpty()) {
        currentPrincipals = subject.getPrincipals();
    }
    Session session = subject.getSession(false);
    if (session == null) {
        if (!isEmpty(currentPrincipals)) {
            session = subject.getSession();
            session.setAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY, currentPrincipals);
        }
    // otherwise no session and no principals - nothing to save
    } else {
        PrincipalCollection existingPrincipals = (PrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
        if (isEmpty(currentPrincipals)) {
            if (!isEmpty(existingPrincipals)) {
                session.removeAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
            }
        // otherwise both are null or empty - no need to update the session
        } else {
            if (!currentPrincipals.equals(existingPrincipals)) {
                session.setAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY, currentPrincipals);
            }
        // otherwise they're the same - no need to update the session
        }
    }
}
Also used : Field(java.lang.reflect.Field) DelegatingSubject(org.apache.shiro.subject.support.DelegatingSubject) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) Session(org.apache.shiro.session.Session)

Example 74 with PrincipalCollection

use of org.apache.shiro.subject.PrincipalCollection in project shiro by apache.

the class DefaultSubjectFactory method createSubject.

public Subject createSubject(SubjectContext context) {
    SecurityManager securityManager = context.resolveSecurityManager();
    Session session = context.resolveSession();
    boolean sessionCreationEnabled = context.isSessionCreationEnabled();
    PrincipalCollection principals = context.resolvePrincipals();
    boolean authenticated = context.resolveAuthenticated();
    String host = context.resolveHost();
    return new DelegatingSubject(principals, authenticated, host, session, sessionCreationEnabled, securityManager);
}
Also used : DelegatingSubject(org.apache.shiro.subject.support.DelegatingSubject) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) Session(org.apache.shiro.session.Session)

Example 75 with PrincipalCollection

use of org.apache.shiro.subject.PrincipalCollection in project shiro by apache.

the class DelegatingSubject method getPreviousPrincipals.

public PrincipalCollection getPreviousPrincipals() {
    PrincipalCollection previousPrincipals = null;
    List<PrincipalCollection> stack = getRunAsPrincipalsStack();
    int stackSize = stack != null ? stack.size() : 0;
    if (stackSize > 0) {
        if (stackSize == 1) {
            previousPrincipals = this.principals;
        } else {
            // always get the one behind the current:
            assert stack != null;
            previousPrincipals = stack.get(1);
        }
    }
    return previousPrincipals;
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection)

Aggregations

PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)88 SimplePrincipalCollection (org.apache.shiro.subject.SimplePrincipalCollection)40 Test (org.junit.Test)36 SecurityAssertion (ddf.security.assertion.SecurityAssertion)23 Subject (ddf.security.Subject)15 Subject (org.apache.shiro.subject.Subject)15 Principal (java.security.Principal)14 ArrayList (java.util.ArrayList)10 DefaultSecurityManager (org.apache.shiro.mgt.DefaultSecurityManager)10 AuthorizationInfo (org.apache.shiro.authz.AuthorizationInfo)9 Permission (org.apache.shiro.authz.Permission)8 Session (org.apache.shiro.session.Session)8 SimpleSession (org.apache.shiro.session.mgt.SimpleSession)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Attribute (ddf.security.assertion.Attribute)5 Map (java.util.Map)5 AuthenticationToken (org.apache.shiro.authc.AuthenticationToken)5 CollectionPermission (ddf.security.permission.CollectionPermission)4