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:ph.fingra.statisticsweb.security.FingraphAuthenticationSuccessHandler.java

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws ServletException, IOException {

    Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());
    if (roles.contains("ROLE_ADMIN")) {
        //getRedirectStrategy().sendRedirect(request, response, adminTargetUrl);
        setDefaultTargetUrl(adminTargetUrl);
    } else if (roles.contains("ROLE_USER")) {
        // getRedirectStrategy().sendRedirect(request, response, userTargetUrl);
        setDefaultTargetUrl(userTargetUrl);
    }//from  w w  w  . ja  v a  2  s .c om
    super.onAuthenticationSuccess(request, response, authentication);
    return;
}

From source file:com.hp.autonomy.frontend.find.idol.beanconfiguration.IdolLoginSuccessHandler.java

@Override
protected String determineTargetUrl(final HttpServletRequest request, final HttpServletResponse response) {
    final Authentication authentication = authenticationInformationRetriever.getAuthentication();

    for (final GrantedAuthority grantedAuthority : authentication.getAuthorities()) {
        final String authority = grantedAuthority.getAuthority();

        if (roleDefault.equalsIgnoreCase(authority)) {
            return configUrl;
        }/*from w w w.j  a  v a  2  s.c  om*/
    }

    return applicationUrl;
}

From source file:shiver.me.timbers.spring.security.jwt.JwtPrincipalAuthenticationConverter.java

@Override
public JwtPrincipal convert(Authentication authentication) {
    return new JwtPrincipal(extractUsername(authentication),
            grantedAuthorityConverter.convert(authentication.getAuthorities()));
}

From source file:org.woofenterprise.dogs.web.controllers.WelcomeController.java

@RequestMapping("/")
public String welcomePage(Model model) {
    List<AppointmentDTO> appointments = new ArrayList<>(facade.findAllAppointmentsForToday());
    Collections.sort(appointments, new Comparator<AppointmentDTO>() {

        @Override//from  w w w.j  av  a  2s  .c o  m
        public int compare(AppointmentDTO o1, AppointmentDTO o2) {
            return o1.getStartTime().compareTo(o2.getStartTime());
        }
    });
    model.addAttribute("appointments", appointments);

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

    for (GrantedAuthority auth : authentication.getAuthorities()) {

        log.warn("SECURITY PRINCIPLES ROLES: {}", auth.getAuthority());
    }
    return "welcome";
}

From source file:com.example.AuthenticationController.java

@PostMapping("/factor")
public void accept(@RequestParam String factor, Principal principal, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    if (!"red".equals(factor)) {
        response.sendRedirect("/factor?error=true");
        return;//from  ww w.j  a  v  a2s.  c om
    }
    Authentication authentication = (Authentication) principal;
    Collection<GrantedAuthority> authorities = new ArrayList<>(authentication.getAuthorities());
    authorities.add(new SimpleGrantedAuthority("ROLE_FACTOR"));
    PreAuthenticatedAuthenticationToken successful = new PreAuthenticatedAuthenticationToken(
            authentication.getPrincipal(), authentication.getCredentials(), authorities);
    successful.setDetails(authentication.getDetails());
    SecurityContextHolder.getContext().setAuthentication(successful);
    handler.onAuthenticationSuccess(request, response, successful);
}

From source file:org.georchestra.security.SecurityRequestHeaderProvider.java

@Override
protected Collection<Header> getCustomRequestHeaders(HttpSession session, HttpServletRequest originalRequest) {

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
    List<Header> headers = new ArrayList<Header>();
    if (authentication.getName().equals("anonymousUser"))
        return headers;
    headers.add(new BasicHeader(HeaderNames.SEC_USERNAME, authentication.getName()));
    StringBuilder roles = new StringBuilder();
    for (GrantedAuthority grantedAuthority : authorities) {
        if (roles.length() != 0)
            roles.append(";");

        roles.append(grantedAuthority.getAuthority());
    }//ww w.  j  a v a 2s.  c o m
    headers.add(new BasicHeader(HeaderNames.SEC_ROLES, roles.toString()));

    return headers;
}

From source file:de.qucosa.fedora.FedoraRepositoryFactory.java

private FedoraCredentials getFedoraCredentials(Authentication auth,
        FedoraAuthorityCredentialsMap fedoraAuthorityCredentialsMap) throws Exception {
    GrantedAuthority firstGranted = auth.getAuthorities().iterator().next();
    if (fedoraAuthorityCredentialsMap.containsKey(firstGranted.getAuthority())) {
        return new ComparableFedoraCredentials(fedoraAuthorityCredentialsMap.get(firstGranted.getAuthority()));
    } else {//from ww w .  ja v a 2  s  .  co  m
        throw new Exception("No Fedora credential configured for authority " + firstGranted.getAuthority());
    }
}

From source file:org.jasig.schedassist.web.security.DelegateAuthenticationSuccessHandler.java

/**
 * Redirects to the correct url based on the {@link GrantedAuthority}s contained by the {@link Authentication} argument.
 * /*  w  ww .  j av  a2 s .c o m*/
 * @see org.springframework.security.web.authentication.AuthenticationSuccessHandler#onAuthenticationSuccess(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.security.core.Authentication)
 */
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {

    Collection<GrantedAuthority> authorities = authentication.getAuthorities();
    if (authorities.contains(SecurityConstants.DELEGATE_OWNER)) {
        // redirect to delegateOwnerTarget
        this.redirectStrategy.sendRedirect(request, response, this.delegateOwnerTarget);
    } else if (authorities.contains(SecurityConstants.DELEGATE_REGISTER)) {
        // redirect to delegateRegisterTarget
        this.redirectStrategy.sendRedirect(request, response, this.delegateRegisterTarget);
    } else {
        // this must be a logout success
        // redirect to logoutTarget
        this.redirectStrategy.sendRedirect(request, response, this.logoutTarget);
    }
}

From source file:com.iselect.web.security.CustomSuccessHandler.java

protected String determineTargetUrl(Authentication authentication) {
    String url = "";

    Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();

    List<String> roles = new ArrayList<String>();

    for (GrantedAuthority a : authorities) {
        roles.add(a.getAuthority());//from ww  w  . j a  va 2  s.  c o  m
    }

    if (isDba(roles)) {
        url = "/db";
    } else if (isAdmin(roles)) {
        url = "/acount";
    } else if (isUser(roles)) {
        url = "/home";
    } else {
        url = "/accessDenied";
    }

    return url;
}

From source file:cz.fi.muni.pa036.airticketbooking.service.SecurityServiceImpl.java

public boolean isCurrentlyLoggedUserAdmin() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>) auth.getAuthorities();
    Iterator<SimpleGrantedAuthority> authoritiesIterator = authorities.iterator();
    while (authoritiesIterator.hasNext()) {
        if (authoritiesIterator.next().getAuthority().equals("ROLE_ADMIN")) {
            return true;
        }//from  w  w w .  j a v a  2  s  .c o  m
    }
    return false;
}