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:nc.noumea.mairie.appock.services.impl.AuthHelperImpl.java

@Override
public boolean hasCurrentUserRole(Role role) {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        return false;
    }//from  ww  w .j av a2 s .  c  o m
    for (GrantedAuthority ga : authentication.getAuthorities()) {
        if (ga.getAuthority().equals(role.name()))
            return true;
    }
    return false;
}

From source file:org.jutge.joc.porra.entitystash.service.EntityStashEntityService.java

public Account getAccount() {
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        final Collection<? extends GrantedAuthority> authorities = auth.getAuthorities();
        if (authorities != null && authorities.contains(USER_AUTHORITY)) {
            final String name = auth.getName();
            return this.accountService.getByName(name);
        }//from ww w .ja  v a2s  .  c  o m
    }
    return null;
}

From source file:com.ushahidi.swiftriver.core.api.auth.crowdmapid.CrowdmapIDAuthenticationProviderTest.java

/**
 * Tests authenticating a user via CrowmdmapID
 *//*from  ww  w.  j av a2s .  com*/
@SuppressWarnings("unchecked")
@Test
public void authenticate() {
    Authentication mockAuthentication = mock(Authentication.class);
    Object mockCredentials = mock(Object.class);
    User mockUser = mock(User.class);

    Set<Role> userRoles = new HashSet<Role>();
    Role role = new Role();
    role.setName("user");
    userRoles.add(role);

    when(mockAuthentication.getName()).thenReturn("test@swiftapp.com");
    when(mockAuthentication.getCredentials()).thenReturn(mockCredentials);
    when(mockCredentials.toString()).thenReturn("pa55w0rd");
    when(mockCrowdmapIDClient.signIn(anyString(), anyString())).thenReturn(true);
    when(mockUserDao.findByUsernameOrEmail(anyString())).thenReturn(mockUser);
    when(mockUser.getRoles()).thenReturn(userRoles);

    Authentication authentication = authenticationProvider.authenticate(mockAuthentication);
    List<GrantedAuthority> authorities = (List<GrantedAuthority>) authentication.getAuthorities();

    verify(mockUserDao).findByUsernameOrEmail("test@swiftapp.com");
    verify(mockCrowdmapIDClient).signIn("test@swiftapp.com", "pa55w0rd");
    assertEquals(1, authorities.size());
    assertEquals("ROLE_USER", authorities.get(0).getAuthority());
}

From source file:tn.rnu.isi.gestioninscription.security.MySimpleUrlAuthenticationSuccessHandler.java

/**
 * Builds the target URL according to the logic defined in the main class
 * Javadoc.//w w  w. j  ava  2s .  c om
 */
protected String determineTargetUrl(Authentication authentication) {
    boolean isEtudiant = false;
    boolean isAdmin = false;
    Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
    for (GrantedAuthority grantedAuthority : authorities) {
        if (grantedAuthority.getAuthority().equals("ETUDIANT")) {
            isEtudiant = true;
            break;
        } else if (grantedAuthority.getAuthority().equals("ADMIN")) {
            isAdmin = true;
            break;
        }
    }

    if (isEtudiant) {
        return "/pages/index.xhtml";
    } else if (isAdmin) {
        return "/admin/index.xhtml";
    } else {
        throw new IllegalStateException();
    }
}

From source file:com.sharmila.hibernatespringsecurity.authentication.MyAuthenticationSuccessHandler.java

protected String determineTargetUrl(Authentication authentication) {
    boolean isAdmin = false;
    boolean isUser = false;

    Collection<? extends GrantedAuthority> authoritites = authentication.getAuthorities();
    for (GrantedAuthority auth : authoritites) {
        if (auth.getAuthority().equals("ROLE_USER")) {
            isUser = true;/*from  w ww.j a va 2 s  . c  o  m*/
            break;
        }
        if (auth.getAuthority().equals("ROLE_ADMIN")) {
            isAdmin = true;
            break;
        }
    }
    if (isUser) {
        return "/userprofile";
    } else if (isAdmin) {
        return "/admin";
    } else {
        throw new IllegalStateException();
    }

}

From source file:org.openinfinity.core.aspect.AuditTrailAspect.java

private void writeRolesToAuditTrailIfEnabled(ArgumentBuilder builder, AuditTrail auditTrail,
        Authentication authentication) {
    if (auditTrail.isRolesEnabled() && authentication != null && authentication.getAuthorities() != null) {
        Collection<? extends GrantedAuthority> grantedAuthorities = authentication.getAuthorities();
        builder.append(" with granted authorities: [");
        for (GrantedAuthority grantedAuthority : grantedAuthorities)
            builder.append("{" + grantedAuthority.getAuthority() + "}");
        builder.append("] ");
    }/*from  w w w. j a v  a2  s  .  c o m*/
}

From source file:configuration.AuthSuccessHandler.java

/**
 * Builds the target URL according to the logic defined in the main class
 * Javadoc./*from  w  w w  .j  a v  a  2s. co  m*/
 *
 * @param authentication
 * @return
 */
protected String determineTargetUrl(Authentication authentication) {
    boolean isUser = false;
    boolean isManager = false;
    Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
    OUTER: for (GrantedAuthority grantedAuthority : authorities) {
        switch (grantedAuthority.getAuthority()) {
        case "user":
            isUser = true;
            break OUTER;
        case "manager":
            isManager = true;
            break OUTER;
        }
    }

    if (isUser) {
        return "/seat/list";
    } else if (isManager) {
        return "/play/list";
    } else {
        throw new IllegalStateException();
    }
}

From source file:com.hp.autonomy.frontend.configuration.authentication.IdolPreAuthenticatedAuthenticationProviderTest.java

@Test
public void authenticateWithExistingUser() {
    when(userService.getUser(SAMPLE_USER, true)).thenReturn(new UserRoles(SAMPLE_USER));
    final Authentication communityAuthentication = authenticationProvider.authenticate(authentication);
    assertTrue(communityAuthentication.isAuthenticated());
    assertThat(communityAuthentication.getAuthorities(), hasSize(2));
}

From source file:myDarkDiary.service.handlers.MySimpleUrlAuthenticationSuccessHandler.java

/** Builds the target URL according to the logic defined in the main class Javadoc. */
protected String determineTargetUrl(Authentication authentication) {
    boolean isUser = false;
    boolean isAdmin = false;
    Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
    for (GrantedAuthority grantedAuthority : authorities) {
        if (grantedAuthority.getAuthority().equals("ROLE_USER")) {
            isUser = true;//from  w  ww.  j a  va2s. c  om
            break;
        } else if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")) {
            isAdmin = true;
            break;
        }
    }

    if (isAdmin) {
        return "/admin/console";
    } else if (isUser) {
        return "/profile";
    } else {
        throw new IllegalStateException();
    }
}

From source file:fi.vm.sade.organisaatio.resource.OrganisaatioDevResource.java

/**
 * Hakee autentikoituneen kyttjn roolit/*from  w  ww.  java 2s.c  om*/
 * @return Operaatio palauttaa samat kuin /cas/myroles. HUOM! Testikyttn tarkoitettu.
 */
@GET
@Path("/myroles")
@Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
@PreAuthorize("hasRole('ROLE_APP_ORGANISAATIOHALLINTA')")
public String getRoles() {
    StringBuilder ret = new StringBuilder("[");

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    ret.append("\"");
    ret.append(auth.getName());
    ret.append("\",");

    for (GrantedAuthority ga : auth.getAuthorities()) {
        ret.append("\"");
        ret.append(ga.getAuthority().replace("ROLE_", ""));
        ret.append("\",");
    }
    ret.setCharAt(ret.length() - 1, ']');
    return ret.toString();
}