Example usage for org.apache.shiro.subject.support DefaultSubjectContext DefaultSubjectContext

List of usage examples for org.apache.shiro.subject.support DefaultSubjectContext DefaultSubjectContext

Introduction

In this page you can find the example usage for org.apache.shiro.subject.support DefaultSubjectContext DefaultSubjectContext.

Prototype

public DefaultSubjectContext() 

Source Link

Usage

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.// w  w w .j a va  2s  . 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:io.vertx.ext.auth.impl.realms.ShiroAuthRealmImpl.java

License:Open Source License

@Override
public String login(JsonObject credentials) {
    SubjectContext subjectContext = new DefaultSubjectContext();
    Subject subject = securityManager.createSubject(subjectContext);
    String username = credentials.getString("username");
    String password = credentials.getString("password");
    AuthenticationToken token = new UsernamePasswordToken(username, password);
    try {//  w  w  w. ja va2 s .c  om
        subject.login(token);
        return subject.getPrincipal().toString();
    } catch (UnknownAccountException | IncorrectCredentialsException | LockedAccountException
            | ExcessiveAttemptsException e) {
        return null;
    } catch (AuthenticationException ae) {
        // Unexpected exception - log it
        log.error("Unexpected exception when logging in", ae.getCause());
        return null;
    }
}

From source file:io.vertx.ext.auth.impl.realms.ShiroAuthRealmImpl.java

License:Open Source License

@Override
public boolean hasRole(String principal, String role) {
    SubjectContext subjectContext = new DefaultSubjectContext();
    PrincipalCollection coll = new SimplePrincipalCollection(principal);
    subjectContext.setPrincipals(coll);// w  w  w  .  j  a v  a 2s . co  m
    Subject subject = securityManager.createSubject(subjectContext);
    return subject.hasRole(role);
}

From source file:io.vertx.ext.auth.impl.realms.ShiroAuthRealmImpl.java

License:Open Source License

@Override
public boolean hasPermission(String principal, String permission) {
    SubjectContext subjectContext = new DefaultSubjectContext();
    PrincipalCollection coll = new SimplePrincipalCollection(principal);
    subjectContext.setPrincipals(coll);//ww  w.j  av  a 2 s .c o  m
    Subject subject = securityManager.createSubject(subjectContext);
    try {
        subject.checkPermission(permission);
        return true;
    } catch (AuthorizationException e) {
        return false;
    }
}

From source file:io.vertx.ext.auth.shiro.impl.ShiroAuthProviderImpl.java

License:Open Source License

@Override
public void authenticate(JsonObject authInfo, Handler<AsyncResult<User>> resultHandler) {
    vertx.executeBlocking(fut -> {//from   w w w  .j a v a2s .  c o m
        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);
        } catch (AuthenticationException e) {
            throw new VertxException(e);
        }
        fut.complete(new ShiroUser(vertx, securityManager, subject, rolePrefix));
    }, resultHandler);
}

From source file:io.vertx.ext.auth.shiro.impl.ShiroAuthRealmBase.java

License:Open Source License

@Override
public void login(JsonObject principal, JsonObject credentials) {
    SubjectContext subjectContext = new DefaultSubjectContext();
    Subject subject = securityManager.createSubject(subjectContext);
    String username = principal.getString("username");
    String password = credentials.getString("password");
    AuthenticationToken token = new UsernamePasswordToken(username, password);
    try {/*from   w ww . j a  v  a 2  s.  c o  m*/
        subject.login(token);
    } catch (AuthenticationException e) {
        throw new VertxException(e);
    }
}

From source file:io.vertx.ext.auth.shiro.impl.ShiroAuthRealmBase.java

License:Open Source License

@Override
public boolean hasRole(JsonObject principal, String role) {
    SubjectContext subjectContext = new DefaultSubjectContext();
    String username = principal.getString("username");
    PrincipalCollection coll = new SimplePrincipalCollection(username);
    subjectContext.setPrincipals(coll);/*ww w  .  ja v a  2 s. com*/
    Subject subject = securityManager.createSubject(subjectContext);
    return subject.hasRole(role);
}

From source file:io.vertx.ext.auth.shiro.impl.ShiroAuthRealmBase.java

License:Open Source License

@Override
public boolean hasPermission(JsonObject principal, String permission) {
    SubjectContext subjectContext = new DefaultSubjectContext();
    String username = principal.getString("username");
    PrincipalCollection coll = new SimplePrincipalCollection(username);
    subjectContext.setPrincipals(coll);//  w ww . jav  a 2s .  com
    Subject subject = securityManager.createSubject(subjectContext);
    try {
        subject.checkPermission(permission);
        return true;
    } catch (AuthorizationException e) {
        return false;
    }
}

From source file:io.vertx.ext.auth.shiro.impl.ShiroUser.java

License:Open Source License

@Override
public void setAuthProvider(AuthProvider authProvider) {
    if (authProvider instanceof ShiroAuthProviderImpl) {
        ShiroAuthProviderImpl shiroAuthProvider = (ShiroAuthProviderImpl) authProvider;
        this.vertx = shiroAuthProvider.getVertx();
        this.securityManager = shiroAuthProvider.getSecurityManager();

        // 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 {// w  w w.  j  a v  a 2 s.c  o m
        throw new IllegalArgumentException("Not a ShiroAuthProviderImpl");
    }
}

From source file:org.openengsb.core.security.internal.RootSubjectHolder.java

License:Apache License

public static void init() {
    DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
    defaultSecurityManager.setAuthenticator(new Authenticator() {
        @Override//w  ww .j  av  a  2 s.  c  om
        public AuthenticationInfo authenticate(AuthenticationToken authenticationToken)
                throws AuthenticationException {
            return new SimpleAuthenticationInfo(new Object(), null, "openengsb");
        }
    });
    Subject subject = defaultSecurityManager.createSubject(new DefaultSubjectContext());
    synchronized (rootSubject) {
        rootSubject.set(defaultSecurityManager.login(subject, null));
        rootSubject.notifyAll();
    }
}