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

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

Introduction

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

Prototype

void setSession(Session session);

Source Link

Document

Sets the Session to use when building the Subject instance.

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/>//from www .  ja  va2s .c  o  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;
}