use of org.springframework.security.core.authority.SimpleGrantedAuthority in project Projeto_Detetive_ES3 by VitorAndrioli.
the class UserDetailsServiceImpl method loadUserByUsername.
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Optional<User> userOp = Optional.ofNullable(UserRepository.findByUsername(username));
User user = userOp.orElseThrow(() -> new UsernameNotFoundException("Usuario não encontrado"));
Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
user.getRoles().stream().forEach(role -> {
grantedAuthorities.add(new SimpleGrantedAuthority(role.getName()));
});
return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), grantedAuthorities);
}
use of org.springframework.security.core.authority.SimpleGrantedAuthority in project coffeenet-starter by coffeenet.
the class DevelopmentCoffeeNetCurrentUserServiceTest method getAdmin.
@Test
public void getAdmin() {
providePrinciple(new User("admin", "", singleton(new SimpleGrantedAuthority("ROLE_COFFEENET-ADMIN"))));
CoffeeNetUserDetails coffeeNetUserDetails = sut.get().get();
assertThat(coffeeNetUserDetails, instanceOf(HumanCoffeeNetUser.class));
assertThat(coffeeNetUserDetails.getPassword(), is(nullValue()));
assertThat(coffeeNetUserDetails.getEmail(), is("admin@coffeenet"));
assertThat(coffeeNetUserDetails.getUsername(), is("admin"));
assertThat(coffeeNetUserDetails.getAuthorities(), contains(new SimpleGrantedAuthority("ROLE_COFFEENET-ADMIN")));
}
use of org.springframework.security.core.authority.SimpleGrantedAuthority in project coffeenet-starter by coffeenet.
the class DevelopmentCoffeeNetCurrentUserServiceTest method getAdminWihMultipleRoles.
@Test
public void getAdminWihMultipleRoles() {
providePrinciple(new User("admin", "", asList(new SimpleGrantedAuthority("ROLE_COFFEENET-ADMIN"), new SimpleGrantedAuthority("ROLE_ADMIN"))));
CoffeeNetUserDetails coffeeNetUserDetails = sut.get().get();
assertThat(coffeeNetUserDetails, instanceOf(HumanCoffeeNetUser.class));
assertThat(coffeeNetUserDetails.getPassword(), is(nullValue()));
assertThat(coffeeNetUserDetails.getEmail(), is("admin@coffeenet"));
assertThat(coffeeNetUserDetails.getUsername(), is("admin"));
assertThat(coffeeNetUserDetails.getAuthorities(), contains(new SimpleGrantedAuthority("ROLE_ADMIN"), new SimpleGrantedAuthority("ROLE_COFFEENET-ADMIN")));
}
use of org.springframework.security.core.authority.SimpleGrantedAuthority in project coffeenet-starter by coffeenet.
the class HumanCoffeeNetUserTest method testHumanUserProperties.
@Test
public void testHumanUserProperties() {
String coffeeNetUser = "ROLE_COFFEENET-USER";
String coffeeNetAdmin = "ROLE_COFFEENET-ADMIN";
List<GrantedAuthority> gAuth = asList(new SimpleGrantedAuthority(coffeeNetUser), new SimpleGrantedAuthority(coffeeNetAdmin));
HumanCoffeeNetUser sut = new HumanCoffeeNetUser("username", "email@coffeenet", gAuth);
assertThat(sut.getUsername(), is("username"));
assertThat(sut.getEmail(), is("email@coffeenet"));
assertThat(sut.getAuthorities().contains(new SimpleGrantedAuthority(coffeeNetUser)), is(true));
assertThat(sut.getAuthorities().contains(new SimpleGrantedAuthority(coffeeNetAdmin)), is(true));
assertThat(sut.isMachineUser(), is(false));
}
use of org.springframework.security.core.authority.SimpleGrantedAuthority in project coffeenet-starter by coffeenet.
the class MachineCoffeeNetUserTest method testMachineUserProperties.
@Test
public void testMachineUserProperties() {
String coffeeNetAdmin = "ROLE_COFFEENET-ADMIN";
List<GrantedAuthority> gAuth = singletonList(new SimpleGrantedAuthority(coffeeNetAdmin));
MachineCoffeeNetUser sut = new MachineCoffeeNetUser("username", gAuth);
assertThat(sut.getUsername(), is("username"));
assertThat(sut.getAuthorities().contains(new SimpleGrantedAuthority(coffeeNetAdmin)), is(true));
assertThat(sut.isCoffeeNetAdmin(), is(false));
assertThat(sut.isMachineUser(), is(true));
}
Aggregations