Example usage for org.springframework.security.core.authority SimpleGrantedAuthority SimpleGrantedAuthority

List of usage examples for org.springframework.security.core.authority SimpleGrantedAuthority SimpleGrantedAuthority

Introduction

In this page you can find the example usage for org.springframework.security.core.authority SimpleGrantedAuthority SimpleGrantedAuthority.

Prototype

public SimpleGrantedAuthority(String role) 

Source Link

Usage

From source file:org.pac4j.springframework.security.authentication.CopyRolesUserDetailsService.java

public UserDetails loadUserDetails(final ClientAuthenticationToken token) throws UsernameNotFoundException {
    final List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    for (String role : token.getUserProfile().getRoles()) {
        authorities.add(new SimpleGrantedAuthority(role));
    }/*from w  w w .j a v a  2  s.c  o m*/

    return new UserDetails() {

        private static final long serialVersionUID = 6523314653561682296L;

        public Collection<? extends GrantedAuthority> getAuthorities() {
            return authorities;
        }

        public String getPassword() {
            return null;
        }

        public String getUsername() {
            return token.getUserProfile().getId();
        }

        public boolean isAccountNonExpired() {
            return true;
        }

        public boolean isAccountNonLocked() {
            return true;
        }

        public boolean isCredentialsNonExpired() {
            return true;
        }

        public boolean isEnabled() {
            return true;
        }
    };

}

From source file:de.metas.ui.web.security.MetasfreshUserDetailsService.java

@Override
public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {
    // TODO: actually fetch from database

    final String password = "System";
    final List<GrantedAuthority> authorities = new ArrayList<>();
    authorities.add(new SimpleGrantedAuthority("System"));
    authorities.add(new SimpleGrantedAuthority("Admin"));

    final User user = new User(username, password, authorities);
    return user;/*from   w ww  .j a  va 2  s  .  c o  m*/
}

From source file:eu.trentorise.smartcampus.permissionprovider.oauth.UserDetailsRepo.java

@Override
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
    List<GrantedAuthority> list = Collections
            .<GrantedAuthority>singletonList(new SimpleGrantedAuthority("ROLE_USER"));

    Long id = null;/*from w  w w  . ja  va2s .c  om*/
    // expected that the user name is the numerical identifier
    try {
        id = Long.parseLong(userName);
    } catch (NumberFormatException e) {
        throw new UsernameNotFoundException("Incorrect user ID: " + userName);
    }

    eu.trentorise.smartcampus.permissionprovider.model.User userEntity = userRepository.findOne(id);
    if (userEntity == null)
        throw new UsernameNotFoundException("User with id " + id + " does not exist.");
    return new User(userEntity.getId().toString(), "", list);
}

From source file:com.base.service.MyUserDetailsService.java

private Set<GrantedAuthority> buildUserAuthority(Set<UserRoles> userRoles) {

    Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>();

    // Build user's authorities
    for (UserRoles userRole : userRoles) {
        setAuths.add(new SimpleGrantedAuthority(userRole.getRole()));
    }/*from w  ww .j av  a  2 s  .co  m*/

    Set<GrantedAuthority> Result = new HashSet<GrantedAuthority>(setAuths);

    return Result;
}

From source file:org.homiefund.api.dto.UserDTO.java

public void setHomes(Collection<HomeDTO> homes) {
    authorities = new ArrayList<>(homes.stream().map(h -> new SimpleGrantedAuthority(h.getId().toString()))
            .collect(Collectors.toList()));
}

From source file:edu.mayo.cts2.uriresolver.security.UserDetailsServiceImpl.java

private static boolean importUser() throws NamingException {
    boolean importedUsers = false;

    String creds = (String) context.lookup("uriResolverDatabaseCredentials");
    if (creds != null) {
        String[] arr = creds.split("\\s");
        String username = arr[0].trim();
        String password = arr[1].trim();

        logger.info("ACCOUNT: " + username + "\t" + password);
        Set<GrantedAuthority> authList = new HashSet<GrantedAuthority>();
        authList.add(new SimpleGrantedAuthority("ROLE_USER"));
        UserDetails user = new UserDetailsImpl(username, password, authList);
        userRepository.put(username, user);
        importedUsers = true;/*www  .j a va2 s  . co  m*/
    }

    return importedUsers;
}

From source file:ts.security.MongoUserDetailsService.java

public List<GrantedAuthority> getAuthorities(String role) {
    List<GrantedAuthority> authList = new ArrayList<>();
    if (role.equals("ROLE_USER")) {
        authList.add(new SimpleGrantedAuthority("ROLE_USER"));

    } else if (role.equals("ROLE_ADMIN")) {
        authList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
    }//from w w w.j a v a2  s.  c  o m
    return authList;
}

From source file:org.oncoblocks.centromere.web.test.security.User.java

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
    if (roles == null) {
        return Collections.emptyList();
    }/*  w ww .j  ava  2 s  . c  o  m*/
    Set<GrantedAuthority> authorities = new HashSet<>();
    for (String role : roles) {
        authorities.add(new SimpleGrantedAuthority(role));
    }
    return authorities;
}

From source file:org.opentides.util.SecurityUtilTest.java

@Test
public void testGetSessionUser() {
    List<GrantedAuthority> auths = new ArrayList<>();
    auths.add(new SimpleGrantedAuthority("ROLE1"));
    auths.add(new SimpleGrantedAuthority("ROLE2"));

    UserDetails userDetails = new User("admin", "password", auths);
    SessionUser sessionUser = new SessionUser(userDetails);
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(sessionUser,
            null, auths);/*from   ww w.  j av a 2  s . c  om*/
    SecurityContextHolder.getContext().setAuthentication(authentication);

    SessionUser actual = SecurityUtil.getSessionUser();
    assertNotNull(actual);
    assertEquals("admin", actual.getUsername());
}

From source file:org.modeshape.example.springsecurity.SecurityConfig.java

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

    UserDetails admin = new User("admin", "123", Arrays.asList(new SimpleGrantedAuthority("admin"),
            new SimpleGrantedAuthority("readonly"), new SimpleGrantedAuthority("readwrite")));

    UserDetails user1 = new User("user1", "123",
            Arrays.asList(new SimpleGrantedAuthority("readonly"), new SimpleGrantedAuthority("readwrite")));

    UserDetails user2 = new User("user2", "123", Arrays.asList(new SimpleGrantedAuthority("readonly")));

    UserDetailsManager userDetailsManager = new InMemoryUserDetailsManager(Arrays.asList(admin, user1, user2));
    auth.userDetailsService(userDetailsManager);
}