Search in sources :

Example 1 with SubjectContext

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

the class DefaultSecurityManager method createSubject.

/**
 * Creates a {@code Subject} instance for the user represented by the given method arguments.
 *
 * @param token    the {@code AuthenticationToken} submitted for the successful authentication.
 * @param info     the {@code AuthenticationInfo} of a newly authenticated user.
 * @param existing the existing {@code Subject} instance that initiated the authentication attempt
 * @return the {@code Subject} instance that represents the context and session data for the newly
 *         authenticated subject.
 */
protected Subject createSubject(AuthenticationToken token, AuthenticationInfo info, Subject existing) {
    SubjectContext context = createSubjectContext();
    context.setAuthenticated(true);
    context.setAuthenticationToken(token);
    context.setAuthenticationInfo(info);
    if (existing != null) {
        context.setSubject(existing);
    }
    return createSubject(context);
}
Also used : SubjectContext(org.apache.shiro.subject.SubjectContext) DefaultSubjectContext(org.apache.shiro.subject.support.DefaultSubjectContext)

Example 2 with SubjectContext

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

the class DefaultSecurityManager method createSubject.

/**
 * This implementation functions as follows:
 * <p/>
 * <ol>
 * <li>Ensures the {@code SubjectContext} is as populated as it can be, using heuristics to acquire
 * data that may not have already been available to it (such as a referenced session or remembered principals).</li>
 * <li>Calls {@link #doCreateSubject(org.apache.shiro.subject.SubjectContext)} to actually perform the
 * {@code Subject} instance creation.</li>
 * <li>calls {@link #save(org.apache.shiro.subject.Subject) save(subject)} to ensure the constructed
 * {@code Subject}'s state is accessible for future requests/invocations if necessary.</li>
 * <li>returns the constructed {@code Subject} instance.</li>
 * </ol>
 *
 * @param subjectContext any data needed to direct how the Subject should be constructed.
 * @return the {@code Subject} instance reflecting the specified contextual data.
 * @see #ensureSecurityManager(org.apache.shiro.subject.SubjectContext)
 * @see #resolveSession(org.apache.shiro.subject.SubjectContext)
 * @see #resolvePrincipals(org.apache.shiro.subject.SubjectContext)
 * @see #doCreateSubject(org.apache.shiro.subject.SubjectContext)
 * @see #save(org.apache.shiro.subject.Subject)
 * @since 1.0
 */
public Subject createSubject(SubjectContext subjectContext) {
    // create a copy so we don't modify the argument's backing map:
    SubjectContext context = copy(subjectContext);
    // ensure that the context has a SecurityManager instance, and if not, add one:
    context = ensureSecurityManager(context);
    // Resolve an associated Session (usually based on a referenced session ID), and place it in the context before
    // sending to the SubjectFactory.  The SubjectFactory should not need to know how to acquire sessions as the
    // process is often environment specific - better to shield the SF from these details:
    context = resolveSession(context);
    // Similarly, the SubjectFactory should not require any concept of RememberMe - translate that here first
    // if possible before handing off to the SubjectFactory:
    context = resolvePrincipals(context);
    Subject subject = doCreateSubject(context);
    // save this subject for future reference if necessary:
    // (this is needed here in case rememberMe principals were resolved and they need to be stored in the
    // session, so we don't constantly rehydrate the rememberMe PrincipalCollection on every operation).
    // Added in 1.2:
    save(subject);
    return subject;
}
Also used : SubjectContext(org.apache.shiro.subject.SubjectContext) DefaultSubjectContext(org.apache.shiro.subject.support.DefaultSubjectContext) Subject(org.apache.shiro.subject.Subject)

Example 3 with SubjectContext

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

the class AbstractRememberMeManagerTest method testGetRememberedPrincipalsWithEmptySerializedBytes.

/**
 * Tests the {@link AbstractRememberMeManager#getRememberedPrincipals(SubjectContext)} method
 * implementation when the internal
 * {@link AbstractRememberMeManager#getRememberedSerializedIdentity(SubjectContext)} method
 * returns null or empty bytes.
 */
@Test
public void testGetRememberedPrincipalsWithEmptySerializedBytes() {
    AbstractRememberMeManager rmm = new DummyRememberMeManager();
    // Since the dummy's getRememberedSerializedIdentity implementation returns an empty byte
    // array, we should be ok:
    PrincipalCollection principals = rmm.getRememberedPrincipals(new DefaultSubjectContext());
    assertNull(principals);
    // try with a null return value too:
    rmm = new DummyRememberMeManager() {

        @Override
        protected byte[] getRememberedSerializedIdentity(SubjectContext subjectContext) {
            return null;
        }
    };
    principals = rmm.getRememberedPrincipals(new DefaultSubjectContext());
    assertNull(principals);
}
Also used : SubjectContext(org.apache.shiro.subject.SubjectContext) DefaultSubjectContext(org.apache.shiro.subject.support.DefaultSubjectContext) DefaultSubjectContext(org.apache.shiro.subject.support.DefaultSubjectContext) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) Test(org.junit.Test)

Example 4 with SubjectContext

use of org.apache.shiro.subject.SubjectContext in project vertx-auth by vert-x3.

the class ShiroAuthProviderImpl method authenticate.

@Override
public void authenticate(JsonObject authInfo, Handler<AsyncResult<User>> resultHandler) {
    vertx.executeBlocking(fut -> {
        // before doing any shiro operations set the context
        SecurityUtils.setSecurityManager(securityManager);
        // proceed
        SubjectContext subjectContext = new DefaultSubjectContext();
        Subject subject = securityManager.createSubject(subjectContext);
        String username = authInfo.getString("username");
        String password = authInfo.getString("password");
        AuthenticationToken token = new UsernamePasswordToken(username, password);
        try {
            subject.login(token);
            fut.complete(new ShiroUser(vertx, securityManager, subject, rolePrefix));
        } catch (AuthenticationException e) {
            fut.fail(e);
        }
    }, resultHandler);
}
Also used : AuthenticationToken(org.apache.shiro.authc.AuthenticationToken) SubjectContext(org.apache.shiro.subject.SubjectContext) DefaultSubjectContext(org.apache.shiro.subject.support.DefaultSubjectContext) AuthenticationException(org.apache.shiro.authc.AuthenticationException) DefaultSubjectContext(org.apache.shiro.subject.support.DefaultSubjectContext) Subject(org.apache.shiro.subject.Subject) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken)

Example 5 with SubjectContext

use of org.apache.shiro.subject.SubjectContext in project vertx-auth by vert-x3.

the class ShiroUser method setAuthProvider.

@Override
public void setAuthProvider(AuthProvider authProvider) {
    if (authProvider instanceof ShiroAuthProviderImpl) {
        ShiroAuthProviderImpl shiroAuthProvider = (ShiroAuthProviderImpl) authProvider;
        this.vertx = shiroAuthProvider.getVertx();
        this.securityManager = shiroAuthProvider.getSecurityManager();
        // before doing any shiro operations set the context
        SecurityUtils.setSecurityManager(securityManager);
        // generate the subject back from the provider
        SubjectContext subjectContext = new DefaultSubjectContext();
        PrincipalCollection coll = new SimplePrincipalCollection(username, shiroAuthProvider.getRealmName());
        subjectContext.setPrincipals(coll);
        subject = securityManager.createSubject(subjectContext);
    } else {
        throw new IllegalArgumentException("Not a ShiroAuthProviderImpl");
    }
}
Also used : SubjectContext(org.apache.shiro.subject.SubjectContext) DefaultSubjectContext(org.apache.shiro.subject.support.DefaultSubjectContext) DefaultSubjectContext(org.apache.shiro.subject.support.DefaultSubjectContext) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection)

Aggregations

SubjectContext (org.apache.shiro.subject.SubjectContext)6 DefaultSubjectContext (org.apache.shiro.subject.support.DefaultSubjectContext)5 PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)2 Subject (org.apache.shiro.subject.Subject)2 ArrayList (java.util.ArrayList)1 AuthenticationException (org.apache.shiro.authc.AuthenticationException)1 AuthenticationToken (org.apache.shiro.authc.AuthenticationToken)1 UsernamePasswordToken (org.apache.shiro.authc.UsernamePasswordToken)1 RememberMeManager (org.apache.shiro.mgt.RememberMeManager)1 Realm (org.apache.shiro.realm.Realm)1 InvalidSessionException (org.apache.shiro.session.InvalidSessionException)1 Session (org.apache.shiro.session.Session)1 SimplePrincipalCollection (org.apache.shiro.subject.SimplePrincipalCollection)1 CookieRememberMeManager (org.apache.shiro.web.mgt.CookieRememberMeManager)1 DefaultWebSecurityManager (org.apache.shiro.web.mgt.DefaultWebSecurityManager)1 DefaultWebSessionManager (org.apache.shiro.web.session.mgt.DefaultWebSessionManager)1 WebSessionManager (org.apache.shiro.web.session.mgt.WebSessionManager)1 Test (org.junit.Test)1 IocBean (org.nutz.ioc.loader.annotation.IocBean)1