Example usage for org.apache.shiro.subject SubjectContext resolveSession

List of usage examples for org.apache.shiro.subject SubjectContext resolveSession

Introduction

In this page you can find the example usage for org.apache.shiro.subject SubjectContext resolveSession.

Prototype

Session resolveSession();

Source Link

Usage

From source file:com.caricah.iotracah.bootstrap.security.IOTSecurityManager.java

License:Apache License

/**
 * Attempts to resolve any associated session based on the context and returns a
 * context that represents this resolved {@code Session} to ensure it may be referenced if necessary by the
 * invoked {@link SubjectFactory} that performs actual {@link Subject} construction.
 * <p/>// w w w  .j a va2  s .co  m
 * If there is a {@code Session} already in the context because that is what the caller wants to be used for
 * {@code Subject} construction, or if no session is resolved, this method effectively does nothing
 * returns the context method argument unaltered.
 *
 * @param context the subject context data that may resolve a Session instance.
 * @return The context to use to pass to a {@link SubjectFactory} for subject creation.
 * @since 1.0
 */
private SubjectContext resolveSession(SubjectContext context) {
    if (context.resolveSession() != null) {
        log.debug("Context already contains a session.  Returning.");
        return context;
    }
    try {

        //Context couldn't resolve it directly,
        // let's see if we can since we have direct access to
        // the session manager:
        IOTClient session = resolveContextSession(context);

        if (session != null) {

            context.setAuthenticated(true);

            context.setSession(session);

            PrincipalCollection principles = session.getPrincipleCollection();
            if (null != principles) {
                context.setPrincipals(principles);
            }

        }
    } catch (InvalidSessionException e) {
        log.trace("Resolved SubjectContext context session is invalid.  Ignoring and creating an anonymous "
                + "(session-less) Subject instance.", e);
    }
    return context;
}

From source file:com.caricah.iotracah.bootstrap.security.IOTSubjectFactory.java

License:Apache License

public Subject createSubject(SubjectContext context) {
    org.apache.shiro.mgt.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 IOTSubject(principals, authenticated, host, session, sessionCreationEnabled, securityManager);
}