Search in sources :

Example 1 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project spring-security by spring-projects.

the class JdbcDaoImpl method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    List<UserDetails> users = loadUsersByUsername(username);
    if (users.size() == 0) {
        this.logger.debug("Query returned no results for user '" + username + "'");
        throw new UsernameNotFoundException(this.messages.getMessage("JdbcDaoImpl.notFound", new Object[] { username }, "Username {0} not found"));
    }
    // contains no GrantedAuthority[]
    UserDetails user = users.get(0);
    Set<GrantedAuthority> dbAuthsSet = new HashSet<GrantedAuthority>();
    if (this.enableAuthorities) {
        dbAuthsSet.addAll(loadUserAuthorities(user.getUsername()));
    }
    if (this.enableGroups) {
        dbAuthsSet.addAll(loadGroupAuthorities(user.getUsername()));
    }
    List<GrantedAuthority> dbAuths = new ArrayList<GrantedAuthority>(dbAuthsSet);
    addCustomAuthorities(user.getUsername(), dbAuths);
    if (dbAuths.size() == 0) {
        this.logger.debug("User '" + username + "' has no authorities and will be treated as 'not found'");
        throw new UsernameNotFoundException(this.messages.getMessage("JdbcDaoImpl.noAuthority", new Object[] { username }, "User {0} has no GrantedAuthority"));
    }
    return createUserDetails(username, user, dbAuths);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetails(org.springframework.security.core.userdetails.UserDetails) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 2 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project spring-security by spring-projects.

the class WithUserDetailsSecurityContextFactory method createSecurityContext.

public SecurityContext createSecurityContext(WithUserDetails withUser) {
    String beanName = withUser.userDetailsServiceBeanName();
    UserDetailsService userDetailsService = StringUtils.hasLength(beanName) ? this.beans.getBean(beanName, UserDetailsService.class) : this.beans.getBean(UserDetailsService.class);
    String username = withUser.value();
    Assert.hasLength(username, "value() must be non empty String");
    UserDetails principal = userDetailsService.loadUserByUsername(username);
    Authentication authentication = new UsernamePasswordAuthenticationToken(principal, principal.getPassword(), principal.getAuthorities());
    SecurityContext context = SecurityContextHolder.createEmptyContext();
    context.setAuthentication(authentication);
    return context;
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) Authentication(org.springframework.security.core.Authentication) SecurityContext(org.springframework.security.core.context.SecurityContext) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 3 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project spring-security by spring-projects.

the class SecurityMockMvcRequestPostProcessorsDigestTests method setup.

@Before
public void setup() {
    this.password = "password";
    request = new MockHttpServletRequest();
    entryPoint = new DigestAuthenticationEntryPoint();
    entryPoint.setKey("key");
    entryPoint.setRealmName("Spring Security");
    filter = new DigestAuthenticationFilter();
    filter.setUserDetailsService(new UserDetailsService() {

        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            return new User(username, password, AuthorityUtils.createAuthorityList("ROLE_USER"));
        }
    });
    filter.setAuthenticationEntryPoint(entryPoint);
    filter.afterPropertiesSet();
}
Also used : DigestAuthenticationEntryPoint(org.springframework.security.web.authentication.www.DigestAuthenticationEntryPoint) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetails(org.springframework.security.core.userdetails.UserDetails) User(org.springframework.security.core.userdetails.User) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DigestAuthenticationFilter(org.springframework.security.web.authentication.www.DigestAuthenticationFilter) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) Before(org.junit.Before)

Example 4 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project spring-security by spring-projects.

the class JdbcUserServiceBeanDefinitionParserTests method rolePrefixIsUsedWhenSet.

@Test
public void rolePrefixIsUsedWhenSet() {
    setContext("<jdbc-user-service id='myUserService' role-prefix='PREFIX_' data-source-ref='dataSource'/>" + DATA_SOURCE);
    JdbcUserDetailsManager mgr = (JdbcUserDetailsManager) appContext.getBean("myUserService");
    UserDetails rod = mgr.loadUserByUsername("rod");
    assertThat(AuthorityUtils.authorityListToSet(rod.getAuthorities())).contains("PREFIX_ROLE_SUPERVISOR");
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) JdbcUserDetailsManager(org.springframework.security.provisioning.JdbcUserDetailsManager) Test(org.junit.Test)

Example 5 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project spring-security by spring-projects.

the class UserServiceBeanDefinitionParserTests method embeddedUsersWithNoPasswordIsGivenGeneratedValue.

@Test
public void embeddedUsersWithNoPasswordIsGivenGeneratedValue() {
    setContext("<user-service id='service'>" + "    <user name='joe' authorities='ROLE_A'/>" + "</user-service>");
    UserDetailsService userService = (UserDetailsService) appContext.getBean("service");
    UserDetails joe = userService.loadUserByUsername("joe");
    assertThat(joe.getPassword().length() > 0).isTrue();
    Long.parseLong(joe.getPassword());
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) Test(org.junit.Test)

Aggregations

UserDetails (org.springframework.security.core.userdetails.UserDetails)97 Test (org.junit.Test)37 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)32 Authentication (org.springframework.security.core.Authentication)30 GrantedAuthority (org.springframework.security.core.GrantedAuthority)16 User (org.springframework.security.core.userdetails.User)14 UserDetailsService (org.springframework.security.core.userdetails.UserDetailsService)14 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)9 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)8 LdapUserDetailsService (org.springframework.security.ldap.userdetails.LdapUserDetailsService)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 UserAccountBean (org.akaza.openclinica.bean.login.UserAccountBean)6 UserAccountDAO (org.akaza.openclinica.dao.login.UserAccountDAO)6 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)6 User (org.apache.atlas.web.model.User)4 User (org.hisp.dhis.user.User)4 IOException (java.io.IOException)3 Date (java.util.Date)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)3