Tob*_*and 7 java authentication spring spring-security spring-boot
我正在尝试创建一个委托身份验证提供程序来执行逻辑,然后根据某些任意逻辑决定选择哪个authenticationProvider; 为了这个例子,如果用户名以前缀开头.
我目前的SecurityConfig将一次尝试一个身份验证提供程序:
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final MyCustomCredentialAuthProvider myAuthProvider;
...
@Override
protected void configure(AuthenticationManagerBuilder auth) {
auth
.ldapAuthentication().configuration(...).here(...).etc(...).and() // ldapAuthenticationProvider is created here
.authenticationProvider(myAuthProvider).and()
// more authentication providers to be added in the future
}
}
Run Code Online (Sandbox Code Playgroud)
根据用户名,我想选择是否要使用尝试提供商,因此如果用户名不是以特定前缀("ldap","custom","ad")开头,则不会调用它们,"等"......),所以:
@Component
public class DelegatingProvider implements AuthenticationProvider {
// Problem: How do I create this ldapAuthenticationProvider bean?
private final LdapAuthenticationProvider ldapAuthenticationProvider;
private final MyCustomCredentialAuthProvider myAuthProvider;
...
@Override
public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
if (authentication.getName() == null) {
throw new BadCredentialsException("No username provided");
} else if (authentication.getName().startsWith("ldapPlease") }
return ldapAuthProvider.authenticate(authentication);
// } else if (...) { ...
// } else if (...) { ...
} else {
return myAuthProvider.authenticate(authentication);
}
}
@Override
public boolean supports(final Class<?> authentication) {
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);;
}
}
Run Code Online (Sandbox Code Playgroud)
我似乎无法以这种方式连接LdapProvider,因为它是由SecurityConfig创建的 - 当它之前由SecurityConfig中的AuthBuilder处理时,如何在LdapProvider bean中创建和连接?
小智 0
@Bean
public LdapAuthenticationProvider ldapAuthentication() {
return new LdapAuthenticationProviderConfigurer().configure(...).here(...).etc(...).build();
}
.....................................
@Component
public class DelegatingProvider implements AuthenticationProvider {
@Autowired
private LdapAuthenticationProvider ldapAuthenticationProvider;
@Autowired
private final MyCustomCredentialAuthProvider myAuthProvider;
...
@Override
public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
if (authentication.getName() == null) {
throw new BadCredentialsException("No username provided");
} else if (authentication.getName().startsWith("ldapPlease") }
return ldapAuthProvider.authenticate(authentication);
// } else if (...) { ...
// } else if (...) { ...
} else {
return myAuthProvider.authenticate(authentication);
}
}
@Override
public boolean supports(final Class<?> authentication) {
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);;
}
}
Run Code Online (Sandbox Code Playgroud)
正如 @NatFar 指定的
@Autowired
private DelegatingProvider delegatingProviderBean;
@Override
protected void configure(AuthenticationManagerBuilder auth) {
auth
.authenticationProvider(delegatingProviderBean).and()
// more authentication providers to be added in the future
}
Run Code Online (Sandbox Code Playgroud)