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

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

Introduction

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

Prototype

void setAuthenticated(boolean authc);

Source Link

Document

Sets whether or not the constructed Subject instance should be considered as authenticated.

Usage

From source file:br.com.criativasoft.opendevice.wsrest.RestWebSecurityManager.java

License:Open Source License

@Override
protected Subject createSubject(AuthenticationToken token, AuthenticationInfo info, Subject existing) {
    SubjectContext context = createSubjectContext();
    context.setAuthenticated(true);
    context.setAuthenticationToken(token);
    context.setAuthenticationInfo(info);
    if (existing != null) {
        // FIX Avoid session creation if previous Subject is disabled.
        // org.apache.shiro.subject.SubjectContext.isSessionCreationEnabled()
        if (existing instanceof WebDelegatingSubject) {
            context.setSessionCreationEnabled(WebUtils._isSessionCreationEnabled(this));
            context.setSecurityManager(((WebDelegatingSubject) existing).getSecurityManager());
        }/*from  w  w w  .j  a v a  2s.  c o  m*/
        context.setSubject(existing);
    }
    return createSubject(context);
}

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

License:Apache License

/**
 * Logs in the specified Subject using the given {@code authenticationToken}, returning an updated Subject
 * instance reflecting the authenticated state if successful or throwing {@code AuthenticationException} if it is
 * not.//from   ww w.j  a va  2 s .c  o m
 * <p>
 * Note that most application developers should probably not call this method directly unless they have a good
 * reason for doing so.  The preferred way to log in a Subject is to call
 * <code>subject.{@link Subject#login login(authenticationToken)}</code> (usually after
 * acquiring the Subject by calling {@link SecurityUtils#getSubject() SecurityUtils.getSubject()}).
 * <p>
 * Framework developers on the other hand might find calling this method directly useful in certain cases.
 *
 * @param subject             the subject against which the authentication attempt will occur
 * @param authenticationToken the token representing the Subject's principal(s) and credential(s)
 * @return the subject instance reflecting the authenticated state after a successful attempt
 * @throws AuthenticationException if the login attempt failed.
 * @since 1.0
 */
@Override
public Subject login(Subject subject, AuthenticationToken authenticationToken) throws AuthenticationException {

    AuthenticationInfo info = authenticate(authenticationToken);

    SubjectContext context = new DefaultSubjectContext();
    context.setAuthenticated(true);
    context.setAuthenticationToken(authenticationToken);
    context.setAuthenticationInfo(info);
    context.setSessionCreationEnabled(true);
    if (subject != null) {
        context.setSubject(subject);
    }

    return createSubject(context);
}

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  w  w w  .  j  a  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;
}

From source file:io.buji.pac4j.ClientSubjectFactory.java

License:Apache License

@Override
public Subject createSubject(SubjectContext context) {

    boolean authenticated = context.isAuthenticated();

    if (authenticated) {

        AuthenticationToken token = context.getAuthenticationToken();

        if (token != null && token instanceof ClientToken) {
            ClientToken clientToken = (ClientToken) token;
            if (clientToken.isRememberMe()) {
                context.setAuthenticated(false);
            }// w w  w. ja v  a 2  s  .  c  o  m
        }
    }

    return super.createSubject(context);
}

From source file:io.buji.pac4j.subject.Pac4jSubjectFactory.java

License:Apache License

@Override
public Subject createSubject(SubjectContext context) {

    boolean authenticated = context.isAuthenticated();

    if (authenticated) {

        AuthenticationToken token = context.getAuthenticationToken();

        if (token != null && token instanceof Pac4jToken) {
            final Pac4jToken clientToken = (Pac4jToken) token;
            if (clientToken.isRememberMe()) {
                context.setAuthenticated(false);
            }/*from ww  w. j  a  v  a2  s. com*/
        }
    }

    return super.createSubject(context);
}