Search in sources :

Example 31 with UserDetailsService

use of org.springframework.security.core.userdetails.UserDetailsService in project molgenis by molgenis.

the class PlatformITConfig method userDetailsService.

@Bean
public UserDetailsService userDetailsService() {
    UserDetailsService userDetailsService = mock(UserDetailsService.class);
    UserDetails adminUserDetails = mock(UserDetails.class);
    Collection authorities = singleton(new SimpleGrantedAuthority(ROLE_SYSTEM));
    when(adminUserDetails.getAuthorities()).thenReturn(authorities);
    when(userDetailsService.loadUserByUsername("admin")).thenReturn(adminUserDetails);
    return userDetailsService;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) UserDetails(org.springframework.security.core.userdetails.UserDetails) Collection(java.util.Collection) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService)

Example 32 with UserDetailsService

use of org.springframework.security.core.userdetails.UserDetailsService in project oc-explorer by devgateway.

the class TestUserDetailsConfiguration method testUserDetailsAdminProcuringEntity.

@Bean("testUserDetailsAdminProcuringEntity")
public UserDetailsService testUserDetailsAdminProcuringEntity() {
    return new UserDetailsService() {

        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            Person person = new Person();
            person.setUsername(username);
            person.setPassword("idontcare");
            person.setAuthorities(Arrays.asList(new SimpleGrantedAuthority("ROLE_PROCURING_ENTITY"), new SimpleGrantedAuthority("ROLE_ADMIN")));
            return personRepository.save(person);
        }
    };
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) Person(org.devgateway.toolkit.persistence.dao.Person) Bean(org.springframework.context.annotation.Bean)

Example 33 with UserDetailsService

use of org.springframework.security.core.userdetails.UserDetailsService in project Activiti by Activiti.

the class DemoApplicationConfiguration method myUserDetailsService.

@Bean
public UserDetailsService myUserDetailsService() {
    InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();
    String[][] usersGroupsAndRoles = { { "bob", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam" }, { "john", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam" }, { "hannah", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam" }, { "other", "password", "ROLE_ACTIVITI_USER", "GROUP_otherTeam" }, { "admin", "password", "ROLE_ACTIVITI_ADMIN" } };
    for (String[] user : usersGroupsAndRoles) {
        List<String> authoritiesStrings = asList(Arrays.copyOfRange(user, 2, user.length));
        logger.info("> Registering new user: " + user[0] + " with the following Authorities[" + authoritiesStrings + "]");
        inMemoryUserDetailsManager.createUser(new User(user[0], passwordEncoder().encode(user[1]), authoritiesStrings.stream().map(s -> new SimpleGrantedAuthority(s)).collect(Collectors.toList())));
    }
    return inMemoryUserDetailsManager;
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) LoggerFactory(org.slf4j.LoggerFactory) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) Autowired(org.springframework.beans.factory.annotation.Autowired) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(org.springframework.security.core.userdetails.User) HttpSecurity(org.springframework.security.config.annotation.web.builders.HttpSecurity) Collectors(java.util.stream.Collectors) Configuration(org.springframework.context.annotation.Configuration) WebSecurityConfigurerAdapter(org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter) AuthenticationManagerBuilder(org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder) List(java.util.List) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) EnableWebSecurity(org.springframework.security.config.annotation.web.configuration.EnableWebSecurity) InMemoryUserDetailsManager(org.springframework.security.provisioning.InMemoryUserDetailsManager) Arrays.asList(java.util.Arrays.asList) Bean(org.springframework.context.annotation.Bean) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(org.springframework.security.core.userdetails.User) InMemoryUserDetailsManager(org.springframework.security.provisioning.InMemoryUserDetailsManager) Bean(org.springframework.context.annotation.Bean)

Example 34 with UserDetailsService

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

the class UserDetailsServiceFactoryBean method authenticationUserDetailsService.

@SuppressWarnings("unchecked")
AuthenticationUserDetailsService authenticationUserDetailsService(String name) {
    UserDetailsService uds;
    if (!StringUtils.hasText(name)) {
        Map<String, ?> beans = getBeansOfType(AuthenticationUserDetailsService.class);
        if (!beans.isEmpty()) {
            if (beans.size() > 1) {
                throw new ApplicationContextException("More than one AuthenticationUserDetailsService registered." + " Please use a specific Id reference.");
            }
            return (AuthenticationUserDetailsService) beans.values().toArray()[0];
        }
        uds = getUserDetailsService();
    } else {
        Object bean = this.beanFactory.getBean(name);
        if (bean instanceof AuthenticationUserDetailsService) {
            return (AuthenticationUserDetailsService) bean;
        } else if (bean instanceof UserDetailsService) {
            uds = cachingUserDetailsService(name);
            if (uds == null) {
                uds = (UserDetailsService) bean;
            }
        } else {
            throw new ApplicationContextException("Bean '" + name + "' must be a UserDetailsService or an" + " AuthenticationUserDetailsService");
        }
    }
    return new UserDetailsByNameServiceWrapper(uds);
}
Also used : UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) CachingUserDetailsService(org.springframework.security.authentication.CachingUserDetailsService) AuthenticationUserDetailsService(org.springframework.security.core.userdetails.AuthenticationUserDetailsService) AuthenticationUserDetailsService(org.springframework.security.core.userdetails.AuthenticationUserDetailsService) UserDetailsByNameServiceWrapper(org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper) ApplicationContextException(org.springframework.context.ApplicationContextException)

Example 35 with UserDetailsService

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

the class LdapUserServiceBeanDefinitionParserTests method externalContextMapperIsSupported.

@Test
public void externalContextMapperIsSupported() {
    setContext("<ldap-server id='someServer' ldif='classpath:test-server.ldif'/>" + "<ldap-user-service id='ldapUDS' user-search-filter='(uid={0})' user-context-mapper-ref='mapper'/>" + "<b:bean id='mapper' class='" + InetOrgPersonContextMapper.class.getName() + "'/>");
    UserDetailsService uds = (UserDetailsService) this.appCtx.getBean("ldapUDS");
    UserDetails ben = uds.loadUserByUsername("ben");
    assertThat(ben instanceof InetOrgPerson).isTrue();
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) InetOrgPerson(org.springframework.security.ldap.userdetails.InetOrgPerson) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) LdapUserDetailsService(org.springframework.security.ldap.userdetails.LdapUserDetailsService) InetOrgPersonContextMapper(org.springframework.security.ldap.userdetails.InetOrgPersonContextMapper) Test(org.junit.jupiter.api.Test)

Aggregations

UserDetailsService (org.springframework.security.core.userdetails.UserDetailsService)44 Test (org.junit.jupiter.api.Test)26 UserDetails (org.springframework.security.core.userdetails.UserDetails)23 Test (org.junit.Test)7 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)7 LdapUserDetailsService (org.springframework.security.ldap.userdetails.LdapUserDetailsService)7 User (org.springframework.security.core.userdetails.User)6 Authentication (org.springframework.security.core.Authentication)5 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)5 Bean (org.springframework.context.annotation.Bean)4 List (java.util.List)3 UserDetailsPasswordService (org.springframework.security.core.userdetails.UserDetailsPasswordService)3 BCryptPasswordEncoder (org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)3 NoOpPasswordEncoder (org.springframework.security.crypto.password.NoOpPasswordEncoder)3 PasswordEncoder (org.springframework.security.crypto.password.PasswordEncoder)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Person (org.devgateway.toolkit.persistence.dao.Person)2 IUserRoleListService (org.pentaho.platform.api.engine.IUserRoleListService)2 ITenant (org.pentaho.platform.api.mt.ITenant)2