Example usage for org.springframework.security.core GrantedAuthority getAuthority

List of usage examples for org.springframework.security.core GrantedAuthority getAuthority

Introduction

In this page you can find the example usage for org.springframework.security.core GrantedAuthority getAuthority.

Prototype

String getAuthority();

Source Link

Document

If the GrantedAuthority can be represented as a String and that String is sufficient in precision to be relied upon for an access control decision by an AccessDecisionManager (or delegate), this method should return such a String.

Usage

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

@RequestMapping(method = RequestMethod.GET, value = "/", headers = "Accept=application/json")
@ResponseBody/*from   w  w  w .ja  v  a 2 s  .c  o  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: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;//from   w  ww  . ja  v  a 2s  .  co m
            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:configuration.AuthSuccessHandler.java

/**
 * Builds the target URL according to the logic defined in the main class
 * Javadoc.//from w  ww  .jav a  2 s  .c  o  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: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;//  w ww.j  av a 2  s. co  m
            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:com.healthcit.analytics.service.LdapManager.java

public UserDetails mapUserFromContext(DirContextOperations ctx, String username,
        Collection<GrantedAuthority> authority) {

    userService.setAuthType("ldap");
    UserDetails originalUser = super.mapUserFromContext(ctx, username, authority);

    // Current authorities come from LDAP groups
    Collection<GrantedAuthority> newAuthorities = originalUser.getAuthorities();

    List<GrantedAuthority> grantedAuthorityList = new ArrayList<GrantedAuthority>();
    if (newAuthorities != null && !newAuthorities.isEmpty()) {

        for (GrantedAuthority currentUserRoles : newAuthorities) {
            grantedAuthorityList.add(new GrantedAuthorityImpl(currentUserRoles.getAuthority()));
        }/*  w ww  .  j a  v a2 s . c o m*/
    }

    UserDetails res = new User(originalUser.getUsername(), originalUser.getPassword(), true, true, true, true,
            grantedAuthorityList);

    return res;
}

From source file:org.client.one.model.UserIdentity.java

/**
   * Check if a role is present in the authorities of current user
   * @param authorities all authorities assigned to current user
   * @param role required authority//from  w  ww  .j  av  a2 s.  co  m
   * @return true if role is present in list of authorities assigned to current user, false otherwise
   */
private boolean isRolePresent(Collection<GrantedAuthority> authorities, String role) {
    boolean isRolePresent = false;
    for (GrantedAuthority grantedAuthority : authorities) {
        isRolePresent = grantedAuthority.getAuthority().equals(role);
        if (isRolePresent)
            break;
    }
    return isRolePresent;
}

From source file:cn.net.withub.demo.bootsec.hello.security.CustomAccessDecisionManager.java

@Override
public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes)
        throws AccessDeniedException, InsufficientAuthenticationException {
    if (configAttributes == null) {
        //??//from  ww w  . j  a  v  a 2s  .co  m
        throw new AccessDeniedException("Access Dendied");
    }

    //???(???)
    for (ConfigAttribute configAttribute : configAttributes) {
        //????
        String needPermission = configAttribute.getAttribute();

        System.out.println("needPermission is " + needPermission);

        //??authentication
        for (GrantedAuthority ga : authentication.getAuthorities()) {

            if (needPermission.equals(ga.getAuthority())) {
                return;
            }

        }

    }

    //??
    throw new AccessDeniedException("Access Dendied");
    //throw new InsufficientAuthenticationException("???");
}

From source file:com.khs.sherpa.spring.SpringAuthentication.java

public String[] authenticate(String username, String password, HttpServletRequest request,
        HttpServletResponse response) {// w  w  w  .jav  a 2  s  .  c om
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);

    Authentication authentication = null;

    try {
        authentication = authenticationManager.authenticate(token);
    } catch (AuthenticationException e) {
        throw new SherpaInvalidUsernamePassword("username and/or password is incorrect");
    }

    if (authentication.isAuthenticated() == false) {
        throw new SherpaInvalidUsernamePassword("username and/or password is incorrect");
    }

    List<String> roles = new ArrayList<String>();
    for (GrantedAuthority auth : authentication.getAuthorities()) {
        roles.add(auth.getAuthority());
    }

    SecurityContextImpl context = new SecurityContextImpl();
    context.setAuthentication(authentication);

    SecurityContextHolder.setContext(context);

    request.getSession().setAttribute("SPRING_SECURITY_CONTEXT_KEY", context);

    return roles.toArray(new String[roles.size()]);

}

From source file:de.tudarmstadt.ukp.clarin.webanno.webapp.security.SpringAuthenticatedWebSession.java

private void addRolesFromAuthentication(Roles roles, Authentication authentication) {
    for (GrantedAuthority authority : authentication.getAuthorities()) {
        roles.add(authority.getAuthority());
    }/*from  w  ww  .  j  a v a 2  s  .  c  o  m*/
}

From source file:com.castlemock.web.basis.model.user.service.UserDetailSecurityServiceTest.java

@Test
public void testLoadUserByUsername() {
    final UserDto user = UserDtoGenerator.generateUserDto();
    final ReadUserByUsernameOutput output = new ReadUserByUsernameOutput(user);
    Mockito.when(serviceProcessor.process(Mockito.any(ReadUserByUsernameInput.class))).thenReturn(output);

    final UserDetails userDetails = service.loadUserByUsername("username");
    Assert.assertEquals(user.getUsername(), userDetails.getUsername());
    Assert.assertEquals(user.getPassword(), userDetails.getPassword());
    final Collection<? extends GrantedAuthority> grantedAuthorityList = userDetails.getAuthorities();
    Assert.assertEquals(1, grantedAuthorityList.size());
    for (GrantedAuthority grantedAuthority : grantedAuthorityList) {
        Assert.assertEquals(user.getRole().name(), grantedAuthority.getAuthority());
    }/*  w  ww.jav a2 s . c o  m*/
}