Allow AuthenticationProvider Bean in Java Config
This commit adds support for defaulting java configuration's authentication by providing an AuthenticationProvider Bean. Fixes gh-3091
This commit is contained in:
@@ -24,6 +24,7 @@ import org.springframework.core.Ordered
|
||||
import org.springframework.core.annotation.Order
|
||||
import org.springframework.security.access.annotation.Secured
|
||||
import org.springframework.security.authentication.AuthenticationManager
|
||||
import org.springframework.security.authentication.AuthenticationProvider
|
||||
import org.springframework.security.authentication.TestingAuthenticationToken
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
|
||||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider
|
||||
@@ -423,4 +424,65 @@ class AuthenticationConfigurationTests extends BaseSpringSpec {
|
||||
new BCryptPasswordEncoder()
|
||||
}
|
||||
}
|
||||
|
||||
def 'gh-3091: Allow Configure AuthenticationProvider'() {
|
||||
setup:
|
||||
AuthenticationProvider ap = Mock()
|
||||
AuthenticationProviderBeanConfig.AP = ap
|
||||
loadConfig(AuthenticationProviderBeanConfig)
|
||||
AuthenticationManager am = context.getBean(AuthenticationConfiguration).getAuthenticationManager()
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user", "password");
|
||||
User user = new User("user","password",AuthorityUtils.createAuthorityList("ROLE_USER"))
|
||||
when:
|
||||
am.authenticate(token)
|
||||
then:
|
||||
1 * ap.supports(_) >> true
|
||||
1 * ap.authenticate(token) >> new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities())
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import([AuthenticationConfiguration, ObjectPostProcessorConfiguration])
|
||||
static class AuthenticationProviderBeanConfig {
|
||||
static AuthenticationProvider AP
|
||||
|
||||
@Bean
|
||||
AuthenticationProvider authenticationProvider() {
|
||||
AP
|
||||
}
|
||||
}
|
||||
|
||||
def 'AuthenticationProvider Bean Prioritized over UserDetailsService'() {
|
||||
setup:
|
||||
UserDetailsService uds = Mock()
|
||||
AuthenticationProvider ap = Mock()
|
||||
AuthenticationProviderBeanAndUserDetailsServiceConfig.AP = ap
|
||||
AuthenticationProviderBeanAndUserDetailsServiceConfig.UDS = uds
|
||||
loadConfig(AuthenticationProviderBeanAndUserDetailsServiceConfig)
|
||||
AuthenticationManager am = context.getBean(AuthenticationConfiguration).getAuthenticationManager()
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user", "password");
|
||||
User user = new User("user","password",AuthorityUtils.createAuthorityList("ROLE_USER"))
|
||||
when:
|
||||
am.authenticate(token)
|
||||
then:
|
||||
1 * ap.supports(_) >> true
|
||||
1 * ap.authenticate(token) >> new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities())
|
||||
0 * uds._
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import([AuthenticationConfiguration, ObjectPostProcessorConfiguration])
|
||||
static class AuthenticationProviderBeanAndUserDetailsServiceConfig {
|
||||
static AuthenticationProvider AP
|
||||
static UserDetailsService UDS
|
||||
|
||||
@Bean
|
||||
AuthenticationProvider authenticationProvider() {
|
||||
AP
|
||||
}
|
||||
|
||||
@Bean
|
||||
UserDetailsService uds() {
|
||||
UDS
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user