Search in sources :

Example 1 with DefaultSubjectContext

use of org.apache.shiro.subject.support.DefaultSubjectContext 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 2 with DefaultSubjectContext

use of org.apache.shiro.subject.support.DefaultSubjectContext 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 3 with DefaultSubjectContext

use of org.apache.shiro.subject.support.DefaultSubjectContext 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)3 DefaultSubjectContext (org.apache.shiro.subject.support.DefaultSubjectContext)3 PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)2 AuthenticationException (org.apache.shiro.authc.AuthenticationException)1 AuthenticationToken (org.apache.shiro.authc.AuthenticationToken)1 UsernamePasswordToken (org.apache.shiro.authc.UsernamePasswordToken)1 SimplePrincipalCollection (org.apache.shiro.subject.SimplePrincipalCollection)1 Subject (org.apache.shiro.subject.Subject)1 Test (org.junit.Test)1