Search in sources :

Example 51 with User

use of org.springframework.security.core.userdetails.User 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 52 with User

use of org.springframework.security.core.userdetails.User in project dhis2-core by dhis2.

the class DefaultUserDetailsService method loadUserByUsername.

// -------------------------------------------------------------------------
// UserDetailsService implementation
// -------------------------------------------------------------------------
@Override
@Transactional
public final UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
    UserCredentials credentials = userService.getUserCredentialsByUsername(username);
    if (credentials == null) {
        credentials = userService.getUserCredentialsByOpenId(username);
        if (credentials == null) {
            throw new UsernameNotFoundException("Username does not exist");
        }
    }
    boolean enabled = !credentials.isDisabled();
    boolean credentialsNonExpired = userService.credentialsNonExpired(credentials);
    boolean accountNonLocked = !securityService.isLocked(credentials.getUsername());
    if (ObjectUtils.anyIsFalse(enabled, credentialsNonExpired, accountNonLocked)) {
        log.info(String.format("Login attempt for disabled/locked user: '%s', enabled: %b, credentials non-expired: %b, account non-locked: %b", username, enabled, credentialsNonExpired, accountNonLocked));
    }
    return new User(credentials.getUsername(), credentials.getPassword(), enabled, true, credentialsNonExpired, accountNonLocked, SecurityUtils.getGrantedAuthorities(credentials));
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(org.springframework.security.core.userdetails.User) UserCredentials(org.hisp.dhis.user.UserCredentials) Transactional(org.springframework.transaction.annotation.Transactional)

Example 53 with User

use of org.springframework.security.core.userdetails.User in project dhis2-core by dhis2.

the class GhostAutomaticAccessProvider method initialise.

// -------------------------------------------------------------------------
// AdminAccessManager implementation
// -------------------------------------------------------------------------
@Override
public void initialise() {
    String username = "ghost_admin";
    String password = "";
    UserDetails user = new User(username, password, true, true, true, true, getGrantedAuthorities());
    authentication = new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities());
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) User(org.springframework.security.core.userdetails.User) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 54 with User

use of org.springframework.security.core.userdetails.User in project av-service by dvoraka.

the class BasicUserDetailsService method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String username) {
    if ("JOHN".equals(username)) {
        List<GrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
        return new User("JOHN", "PASS", true, true, true, true, authorities);
    } else {
        throw new UsernameNotFoundException("User not found: " + username);
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(org.springframework.security.core.userdetails.User) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList)

Aggregations

User (org.springframework.security.core.userdetails.User)54 Test (org.junit.Test)30 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)16 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)15 UserDetails (org.springframework.security.core.userdetails.UserDetails)14 Authentication (org.springframework.security.core.Authentication)13 GrantedAuthority (org.springframework.security.core.GrantedAuthority)10 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)6 ArrayList (java.util.ArrayList)5 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)4 Before (org.junit.Before)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 SecurityContext (org.springframework.security.core.context.SecurityContext)3 FilterChain (javax.servlet.FilterChain)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Assertion (org.jasig.cas.client.validation.Assertion)2 AssertionImpl (org.jasig.cas.client.validation.AssertionImpl)2 DirContextOperations (org.springframework.ldap.core.DirContextOperations)2 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)2