Example usage for org.springframework.security.core Authentication getAuthorities

List of usage examples for org.springframework.security.core Authentication getAuthorities

Introduction

In this page you can find the example usage for org.springframework.security.core Authentication getAuthorities.

Prototype

Collection<? extends GrantedAuthority> getAuthorities();

Source Link

Document

Set by an AuthenticationManager to indicate the authorities that the principal has been granted.

Usage

From source file:de.hybris.platform.acceleratorstorefrontcommons.security.StorefrontAuthenticationSuccessHandler.java

protected boolean isAdminAuthority(final Authentication authentication) {
    return CollectionUtils.isNotEmpty(authentication.getAuthorities())
            && authentication.getAuthorities().contains(adminAuthority);
}

From source file:ar.com.zauber.commons.auth.acegi.AbstractAcegiAuthenticationUserMapper.java

/** @see AuthenticationUserMapper#getRoles() */
public Set<String> getRoles() {
    final SecurityContext context = SecurityContextHolder.getContext();
    final Authentication auth = context.getAuthentication();
    final Set<String> ret = new HashSet<String>();
    if (auth.isAuthenticated()) {
        final Collection<GrantedAuthority> roles = auth.getAuthorities();
        for (final GrantedAuthority role : roles) {
            ret.add(role.getAuthority());
        }//from ww  w.j  av  a 2s.c  o m
    }

    return ret;
}

From source file:oobbit.orm.Users.java

public ArrayList<String> getCurrentUserRoles() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    ArrayList<String> asStrings = new ArrayList<>();

    for (GrantedAuthority authority : auth.getAuthorities()) {
        asStrings.add(authority.getAuthority());
    }/*from   w  w  w .j a v a 2  s .  co m*/

    return asStrings;
}

From source file:edu.uiowa.icts.util.DebugRolesTag.java

/**
 * <p>doStartTag.</p>//ww  w. j av  a2s. c  o  m
 *
 * @return a int.
 */
public int doStartTag() {

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {

        Collection<? extends GrantedAuthority> authorities = auth.getAuthorities();
        if (authorities != null) {

            iterator = authorities.iterator();

        } else {
            log.debug("no authorities, list is null");
        }

    } else {
        log.debug("authentication object is null");
    }

    if (iterator != null && iterator.hasNext()) {
        pageContext.setAttribute("role", iterator.next().getAuthority());
        return EVAL_BODY_INCLUDE;
    }

    return SKIP_BODY;
}

From source file:br.com.sicva.seguranca.DirecionadorUsuario.java

protected String determineTargetUrl(Authentication authentication) {
    boolean isAtendente = false;
    boolean isAdmin = false;
    boolean isEnfermeiro = false;
    Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
    for (GrantedAuthority grantedAuthority : authorities) {
        if (grantedAuthority.getAuthority().equals("ROLE_ATENDENTE")) {
            isAtendente = true;//ww  w  .  ja  v  a2s  . com
            break;
        } else if (grantedAuthority.getAuthority().equals("ROLE_ADMINISTRADOR")) {
            isAdmin = true;
            break;
        } else if (grantedAuthority.getAuthority().equals("ROLE_ENFERMEIRO")) {
            isEnfermeiro = true;
            break;
        }
    }

    if (isAtendente) {
        return "/atendente/index.xhtml";
    } else if (isAdmin) {
        return "/admin/index.xhtml";
    } else if (isEnfermeiro) {
        return "/enfermeiro/index.xhtml";
    } else {
        throw new IllegalStateException();
    }
}

From source file:mx.unam.pixel.controller.LoginController.java

@PostConstruct
public void init() {
    Object user = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (!(auth instanceof AnonymousAuthenticationToken) && (user instanceof UserDetails)) {
        this.username = ((UserDetails) user).getUsername();
        if (auth.getAuthorities().size() > 0) {
            this.rol = auth.getAuthorities().iterator().next().getAuthority();
        }/*from  ww  w.  j  av a 2  s  .co  m*/
        this.loged = true;
    } else if (user instanceof String) {
        this.username = user.toString();
        this.loged = false;
    }

}

From source file:fi.vm.sade.organisaatio.auth.PermissionChecker.java

private boolean checkCRUDRyhma(OrganisaatioContext authContext) {
    Set<OrganisaatioTyyppi> tyypit = authContext.getOrgTypes();
    if (tyypit.size() == 1 && tyypit.contains(OrganisaatioTyyppi.RYHMA)) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        for (GrantedAuthority ga : auth.getAuthorities()) {
            if (ga.getAuthority().startsWith("ROLE_APP_ORGANISAATIOHALLINTA_RYHMA_")) {
                return true;
            }//from   www.java 2  s . c om
        }
    }
    return false;
}

From source file:org.mitre.openid.connect.web.UserInfoInterceptor.java

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();

    if (auth instanceof Authentication) {
        request.setAttribute("userAuthorities", gson.toJson(auth.getAuthorities()));
    }//from  ww w  . j  a va2 s .co m

    if (!trustResolver.isAnonymous(auth)) { // skip lookup on anonymous logins
        if (auth instanceof OIDCAuthenticationToken) {
            // if they're logging into this server from a remote OIDC server, pass through their user info
            OIDCAuthenticationToken oidc = (OIDCAuthenticationToken) auth;
            if (oidc.getUserInfo() != null) {
                request.setAttribute("userInfo", oidc.getUserInfo());
                request.setAttribute("userInfoJson", oidc.getUserInfo().toJson());
            } else {
                request.setAttribute("userInfo", null);
                request.setAttribute("userInfoJson", "null");
            }
        } else {
            // don't bother checking if we don't have a principal or a userInfoService to work with
            if (auth != null && auth.getName() != null && userInfoService != null) {

                // try to look up a user based on the principal's name
                UserInfo user = userInfoService.getByUsername(auth.getName());

                // if we have one, inject it so views can use it
                if (user != null) {
                    request.setAttribute("userInfo", user);
                    request.setAttribute("userInfoJson", user.toJson());
                }
            }
        }
    }

    return true;
}

From source file:com.epam.ta.reportportal.auth.permissions.ReportPortalPermissionEvaluator.java

private boolean checkPermission(Authentication authentication, Object targetDomainObject,
        String permissionKey) {/*from  w w w .  ja v a  2 s.  c  om*/
    verifyPermissionIsDefined(permissionKey);
    if (allowAllToAdmin && authentication.isAuthenticated()
            && authentication.getAuthorities().contains(ADMIN_AUTHORITY)) {
        return true;
    }
    Permission permission = permissionNameToPermissionMap.get(permissionKey);
    return permission.isAllowed(authentication, targetDomainObject);
}

From source file:nz.co.senanque.vaadinsupport.viewmanager.SpringLoginListener.java

private void login(String username, String password) {
    Set<String> permissionsList = new HashSet<String>();
    try {//from   w w w .  ja  v  a  2  s. com
        Authentication authentication = new UsernamePasswordAuthenticationToken(username, password);
        authentication = getAuthenticationManager().authenticate(authentication);
        for (GrantedAuthority grantedAuthority : authentication.getAuthorities()) {
            permissionsList.add(grantedAuthority.getAuthority());
        }

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    getPermissionManager().setPermissionsList(permissionsList);
    getPermissionManager().setCurrentUser(username);
}