Search in sources :

Example 91 with SimpleGrantedAuthority

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);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(br.fatecsp.engsoft.entities.User) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) HashSet(java.util.HashSet)

Example 92 with SimpleGrantedAuthority

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")));
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(org.springframework.security.core.userdetails.User) Test(org.junit.Test)

Example 93 with SimpleGrantedAuthority

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")));
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(org.springframework.security.core.userdetails.User) Test(org.junit.Test)

Example 94 with SimpleGrantedAuthority

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));
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.Test)

Example 95 with SimpleGrantedAuthority

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));
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.Test)

Aggregations

SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)109 GrantedAuthority (org.springframework.security.core.GrantedAuthority)64 Test (org.junit.Test)49 ArrayList (java.util.ArrayList)30 Authentication (org.springframework.security.core.Authentication)27 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)22 HashSet (java.util.HashSet)14 User (org.springframework.security.core.userdetails.User)11 ObjectIdentity (org.springframework.security.acls.model.ObjectIdentity)8 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)8 MutableAcl (org.springframework.security.acls.model.MutableAcl)7 AnonymousAuthenticationToken (org.springframework.security.authentication.AnonymousAuthenticationToken)7 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)7 AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)7 Before (org.junit.Before)6 OrcidOAuth2Authentication (org.orcid.core.oauth.OrcidOAuth2Authentication)6 ScopePathType (org.orcid.jaxb.model.message.ScopePathType)6 List (java.util.List)5 Map (java.util.Map)5 DBUnitTest (org.orcid.test.DBUnitTest)5