Search in sources :

Example 11 with InMemoryXmlApplicationContext

use of org.springframework.security.config.util.InMemoryXmlApplicationContext in project spring-security by spring-projects.

the class LdapProviderBeanDefinitionParserTests method simpleProviderAuthenticatesCorrectly.

@Test
public void simpleProviderAuthenticatesCorrectly() {
    this.appCtx = new InMemoryXmlApplicationContext("<ldap-server ldif='classpath:test-server.ldif' port='0'/>" + "<authentication-manager>" + "  <ldap-authentication-provider group-search-filter='member={0}' />" + "</authentication-manager>");
    AuthenticationManager authenticationManager = this.appCtx.getBean(BeanIds.AUTHENTICATION_MANAGER, AuthenticationManager.class);
    Authentication auth = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken("ben", "benspassword"));
    UserDetails ben = (UserDetails) auth.getPrincipal();
    assertThat(ben.getAuthorities()).hasSize(3);
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) UserDetails(org.springframework.security.core.userdetails.UserDetails) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) InMemoryXmlApplicationContext(org.springframework.security.config.util.InMemoryXmlApplicationContext) Test(org.junit.jupiter.api.Test)

Example 12 with InMemoryXmlApplicationContext

use of org.springframework.security.config.util.InMemoryXmlApplicationContext in project spring-security by spring-projects.

the class LdapProviderBeanDefinitionParserTests method multipleProvidersAreSupported.

@Test
public void multipleProvidersAreSupported() {
    this.appCtx = new InMemoryXmlApplicationContext("<ldap-server ldif='classpath:test-server.ldif' port='0'/>" + "<authentication-manager>" + "  <ldap-authentication-provider group-search-filter='member={0}' />" + "  <ldap-authentication-provider group-search-filter='uniqueMember={0}' />" + "</authentication-manager>");
    ProviderManager providerManager = this.appCtx.getBean(BeanIds.AUTHENTICATION_MANAGER, ProviderManager.class);
    assertThat(providerManager.getProviders()).hasSize(2);
    assertThat(providerManager.getProviders()).extracting("authoritiesPopulator.groupSearchFilter").containsExactly("member={0}", "uniqueMember={0}");
}
Also used : ProviderManager(org.springframework.security.authentication.ProviderManager) InMemoryXmlApplicationContext(org.springframework.security.config.util.InMemoryXmlApplicationContext) Test(org.junit.jupiter.api.Test)

Example 13 with InMemoryXmlApplicationContext

use of org.springframework.security.config.util.InMemoryXmlApplicationContext in project spring-security by spring-projects.

the class LdapProviderBeanDefinitionParserTests method supportsPasswordComparisonAuthenticationWithPasswordEncoder.

@Test
public void supportsPasswordComparisonAuthenticationWithPasswordEncoder() {
    this.appCtx = new InMemoryXmlApplicationContext("<ldap-server ldif='classpath:test-server.ldif' port='0'/>" + "<authentication-manager>" + "  <ldap-authentication-provider user-dn-pattern='uid={0},ou=people'>" + "    <password-compare password-attribute='uid'>" + "      <password-encoder ref='passwordEncoder' />" + "    </password-compare>" + "  </ldap-authentication-provider>" + "</authentication-manager>" + "<b:bean id='passwordEncoder' class='org.springframework.security.crypto.password.NoOpPasswordEncoder' factory-method='getInstance' />");
    AuthenticationManager authenticationManager = this.appCtx.getBean(BeanIds.AUTHENTICATION_MANAGER, AuthenticationManager.class);
    Authentication auth = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken("ben", "ben"));
    assertThat(auth).isNotNull();
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) InMemoryXmlApplicationContext(org.springframework.security.config.util.InMemoryXmlApplicationContext) Test(org.junit.jupiter.api.Test)

Example 14 with InMemoryXmlApplicationContext

use of org.springframework.security.config.util.InMemoryXmlApplicationContext in project spring-security by spring-projects.

the class AuthenticationProviderBeanDefinitionParserTests method providerWithMd5PasswordEncoderWorks.

@Test
public void providerWithMd5PasswordEncoderWorks() {
    // @formatter:off
    this.appContext = new InMemoryXmlApplicationContext(" <authentication-manager>" + " <authentication-provider>" + "        <password-encoder ref='passwordEncoder'/>" + "        <user-service>" + "            <user name='bob' password='12b141f35d58b8b3a46eea65e6ac179e' authorities='ROLE_A' />" + "        </user-service>" + "    </authentication-provider>" + " </authentication-manager>" + " <b:bean id='passwordEncoder'  class='" + MessageDigestPasswordEncoder.class.getName() + "'>" + "     <b:constructor-arg value='MD5'/>" + " </b:bean>");
    // @formatter:on
    getProvider().authenticate(this.bob);
}
Also used : InMemoryXmlApplicationContext(org.springframework.security.config.util.InMemoryXmlApplicationContext) Test(org.junit.jupiter.api.Test)

Example 15 with InMemoryXmlApplicationContext

use of org.springframework.security.config.util.InMemoryXmlApplicationContext in project spring-security by spring-projects.

the class AuthenticationProviderBeanDefinitionParserTests method providerWithShaPasswordEncoderWorks.

@Test
public void providerWithShaPasswordEncoderWorks() {
    // @formatter:off
    this.appContext = new InMemoryXmlApplicationContext(" <authentication-manager>" + " <authentication-provider>" + "        <password-encoder ref='passwordEncoder'/>" + "        <user-service>" + "            <user name='bob' password='{SSHA}PpuEwfdj7M1rs0C2W4ssSM2XEN/Y6S5U' authorities='ROLE_A' />" + "        </user-service>" + "    </authentication-provider>" + " </authentication-manager>" + " <b:bean id='passwordEncoder'  class='" + LdapShaPasswordEncoder.class.getName() + "'/>");
    // @formatter:on
    getProvider().authenticate(this.bob);
}
Also used : InMemoryXmlApplicationContext(org.springframework.security.config.util.InMemoryXmlApplicationContext) Test(org.junit.jupiter.api.Test)

Aggregations

InMemoryXmlApplicationContext (org.springframework.security.config.util.InMemoryXmlApplicationContext)20 Test (org.junit.jupiter.api.Test)15 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)4 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)4 Authentication (org.springframework.security.core.Authentication)4 LdapTemplate (org.springframework.ldap.core.LdapTemplate)3 ProviderManager (org.springframework.security.authentication.ProviderManager)3 DefaultSpringSecurityContextSource (org.springframework.security.ldap.DefaultSpringSecurityContextSource)3 Filter (jakarta.servlet.Filter)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 MessageFormat (java.text.MessageFormat)1 Test (org.junit.Test)1 AuthenticationProvider (org.springframework.security.authentication.AuthenticationProvider)1 UserDetails (org.springframework.security.core.userdetails.UserDetails)1 MessageDigestPasswordEncoder (org.springframework.security.crypto.password.MessageDigestPasswordEncoder)1 ApacheDSContainer (org.springframework.security.ldap.server.ApacheDSContainer)1 InetOrgPersonContextMapper (org.springframework.security.ldap.userdetails.InetOrgPersonContextMapper)1