Example usage for org.apache.shiro.subject PrincipalCollection asSet

List of usage examples for org.apache.shiro.subject PrincipalCollection asSet

Introduction

In this page you can find the example usage for org.apache.shiro.subject PrincipalCollection asSet.

Prototype

Set asSet();

Source Link

Document

Returns a single Subject's principals retrieved from all configured Realms as a Set, or an empty Set if there are not any principals.

Usage

From source file:org.aw20.plugin.login.LoginExtension.java

License:Open Source License

@Override
public void requestStart(cfSession session) {

    // the plugin may be disabled
    if (userFormField == null)
        return;/*w ww  .ja v a  2 s  .co  m*/

    cfStructData formData = (cfStructData) session.getData("form");

    if (formData.containsKey(userFormField) && formData.containsKey(passwordFormField)) {
        try {
            String username = formData.getData(userFormField).getString();
            String password = formData.getData(passwordFormField).getString();

            // Remove the fields from the form post
            formData.setData(passwordFormField, new cfStringData("******"));

            // Attempt to login them in
            Subject user = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);

            user.login(token);

            if (user.isAuthenticated()) {
                PrincipalCollection pc = user.getPrincipals();
                formData.setData(returnFormField, new cfStringData(pc.asSet().toString()));
                user.logout();
            } else {
                formData.setData(returnFormField, new cfStringData("[failed]"));
            }

        } catch (Exception e) {
            formData.setData(returnFormField, new cfStringData("[exception]" + e.getMessage()));
            PluginManager.getPlugInManager().log("[LoginExtension] " + e.getMessage());
        }

    }

}

From source file:org.fcrepo.auth.common.AbstractPrincipalProvider.java

License:Apache License

@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
        throws IOException, ServletException {
    final HttpServletRequest hsRequest = (HttpServletRequest) request;
    final Subject currentUser = SecurityUtils.getSubject();
    PrincipalCollection principals = currentUser.getPrincipals();

    final Set<Principal> newPrincipals = getPrincipals(hsRequest);
    if (newPrincipals.size() > 0) {
        final Set<Principal> currentPrincipals;
        if (principals == null || principals.asList().isEmpty()) {
            log.debug("Shiro Principal object is not found!");
            currentPrincipals = newPrincipals;
        } else {/*from   www . j  a  va 2 s  .  co m*/
            currentPrincipals = new HashSet<>((Set<Principal>) principals.asSet());
            log.debug("Number of Principals already in session object: {}", currentPrincipals.size());
            currentPrincipals.addAll(newPrincipals);
        }
        log.debug("Number of Principals after processing the current request: {}", currentPrincipals.size());
        principals = new SimplePrincipalCollection(currentPrincipals, REALM_NAME);
        currentUser.runAs(principals);
    } else {
        log.debug("New Principals not found in the request!");
    }
    chain.doFilter(request, response);
}