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:tomekkup.helenos.security.web.authentication.AbstractJsonAuthenticationHandler.java

protected final void writeResult(HttpServletResponse response, Authentication authentication)
        throws IOException {
    response.setHeader("Content-Type", "application/json");
    PrintWriter writer = response.getWriter();
    objectMapper.writeValue(writer, (authentication != null ? authentication.getAuthorities() : null));
    writer.flush();/*from  w  w w.ja v a 2s .co m*/
}

From source file:oauth2.authentication.MyAuthenticationSuccessHandler.java

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

    if (authentication.isAuthenticated()) {
        for (Map.Entry<String, String> entry : redirectsByAuthority.entrySet()) {
            if (authentication.getAuthorities().stream()
                    .anyMatch(authority -> Objects.equal(entry.getKey(), authority.getAuthority()))) {
                getRedirectStrategy().sendRedirect(request, response, entry.getValue());
                return;
            }/*from  ww w  .  ja v  a  2  s  . c  om*/
        }
    }
    super.onAuthenticationSuccess(request, response, authentication);
}

From source file:io.onedecision.engine.decisions.web.UserProfileController.java

@RequestMapping(method = RequestMethod.GET, value = "/", headers = "Accept=application/json")
@ResponseBody/* w  w  w  . j av  a 2s.  co m*/
public final Profile getProfile(Authentication auth) throws DecisionException {
    LOGGER.info(String.format("getProfile"));

    Profile profile = new Profile();
    profile.setUsername(auth.getName());
    for (GrantedAuthority authority : auth.getAuthorities()) {
        // trim ROLE_prefix
        profile.getRoles().add(authority.getAuthority().substring(5));
    }

    return profile;
}

From source file:com.traffitruck.web.JsonController.java

@RequestMapping(value = "/deleteLoadAdmin", method = RequestMethod.POST)
String deleteLoad(@RequestParam("loadId") String loadId) {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    for (GrantedAuthority auth : authentication.getAuthorities()) {
        if (Role.ADMIN.toString().equals(auth.getAuthority())) {
            dao.deleteLoadByAdmin(loadId);
        }/*from  w ww. jav  a2 s .com*/
    }
    return "Success!";
}

From source file:com.traffitruck.web.JsonController.java

@RequestMapping(value = "/allow_load_details", method = RequestMethod.POST)
String allowUserToSeeLoadsWithoutTrucks(@RequestParam("username") String username) {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    for (GrantedAuthority auth : authentication.getAuthorities()) {
        if (Role.ADMIN.toString().equals(auth.getAuthority())) {
            dao.enableViewingLoads(username);
        }/*from  w ww.  j av  a 2s .co  m*/
    }
    return "Success!";
}

From source file:br.ufpa.psi.comportamente.labgame.seguranca.MySimpleUrlAuthenticationSuccessHandler.java

/**
 * Builds the target URL according to the logic defined in the main class
 * Javadoc./*from w w  w.ja  v a 2  s  . c om*/
 *
 * @param authentication
 * @return
 */
protected String determineTargetUrl(Authentication authentication) {
    boolean isJogador = false;
    boolean isPesquisador = false;
    Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
    OUTER: for (GrantedAuthority grantedAuthority : authorities) {
        switch (grantedAuthority.getAuthority()) {
        case "ROLE_JOGADOR":
            isJogador = true;
            break OUTER;
        case "ROLE_PESQUISADOR":
            isPesquisador = true;
            break OUTER;
        }
    }

    if (isJogador) {
        return "/paginas/publico/jogador.jsf";
    } else if (isPesquisador) {
        return "/paginas/pesquisador/pesquisador.jsf";
    } else {
        throw new IllegalStateException();
    }
}

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

@Test
@SuppressWarnings("unchecked")
public void Can_convert_a_jwt_principle_to_a_jwt_authentication() {

    final JwtPrincipal principal = mock(JwtPrincipal.class);

    final List<String> roles = mock(List.class);
    final String username = someString();
    final Collection<GrantedAuthority> authorities = asList(mock(GrantedAuthority.class),
            mock(GrantedAuthority.class), mock(GrantedAuthority.class));

    // Given/*from  w  w  w .  ja  v a  2s . c  om*/
    given(principal.getUsername()).willReturn(username);
    given(principal.getRoles()).willReturn(roles);
    given(grantedAuthorityConverter.convert(roles)).willReturn((Collection) authorities);

    // When
    final Authentication actual = converter.convert(principal);

    // Then
    assertThat(actual.getPrincipal(), is((Object) username));
    assertThat(actual.getAuthorities(), is((Collection) authorities));
    assertThat(actual.isAuthenticated(), is(true));
}

From source file:org.springmodules.validation.valang.functions.InRoleFunction.java

protected Object doGetResult(Object target) {

    Object role = getArguments()[0].getResult(target);

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        return Boolean.FALSE;
    }/*from  www  .  ja v a2s.c o m*/

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

    for (GrantedAuthority grantedAuthority : authorities) {
        if (grantedAuthority.equals(role)) {
            return Boolean.TRUE;
        }
    }

    return Boolean.FALSE;
}

From source file:ru.codemine.ccms.service.EmployeeService.java

public boolean isCurrentUserHasRole(String role) {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        Collection<? extends GrantedAuthority> authorities = auth.getAuthorities();

        return authorities.contains(new SimpleGrantedAuthority(role));
    }//from  ww  w  . ja v  a2  s. c o m

    return false;
}

From source file:com.springsource.hq.plugin.tcserver.serverconfig.web.support.HqAuthenticationFilter.java

/**
 * This takes an existing Authentication object, and converts it into an tc Server plugin-based object.
 * //from  ww  w.  java 2 s  .  c  o m
 * @param auth
 * @param sessionId
 * @return
 */
private UsernamePasswordAuthenticationToken createHqAuthenticationToken(Authentication auth, String sessionId) {
    List<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
    auths.addAll(auth.getAuthorities());
    auths.add(new GrantedAuthorityImpl(defaultRole));
    UsernamePasswordAuthenticationToken newToken = new HqAuthenticationToken(auth.getPrincipal(),
            auth.getCredentials(), auths, sessionId);
    return newToken;
}