java - 创建委托(delegate)身份验证提供程序 (Spring Security)

标签 java spring spring-boot authentication spring-security

我正在尝试创建一个委托(delegate)身份验证提供程序来执行逻辑,然后再根据一些任意逻辑决定选择哪个身份验证提供程序;为了这个例子,如果用户名以前缀开头。

我当前的 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
    }
}

根据用户名,我想选择是否要尝试使用提供程序,因此如果用户名不以特定前缀(“ldap”、“custom”、 “广告”、“等等”……),所以:

@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);;
    }
}

我似乎无法以这种方式连接 LdapProvider,因为它是由 SecurityConfig 创建的 - 当 LdapProvider bean 之前由 SecurityConfig 中的 AuthBuilder 处理时,我如何创建和连接 LdapProvider bean?

最佳答案

    @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);;
        }
    }

正如@NatFar 指定的那样

    @Autowired
    private DelegatingProvider delegatingProviderBean;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) {
        auth
          .authenticationProvider(delegatingProviderBean).and()
          // more authentication providers to be added in the future
    }

关于java - 创建委托(delegate)身份验证提供程序 (Spring Security),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54828640/

相关文章:

java - 将布局转换为图像,如何获得良好的质量?

java - 是否可以使用 ANT 构建文件访问/添加远程位置 jar 到项目中?

java - 扑克牌经销商的无限循环

java - 为什么 @AuthenticationPrincipal 返回 Authentication 而不是主体对象?

Spring ApplicationListener在webapp上被触发了两次

java - 在 Java Spring Boot 中从 Principal 中提取数据

java - 同时显示底部导航 View 和抽屉式导航

Spring Data Rest : how to register a custom conversionService with custom Converter<Entity, 资源>?

java - 在 UserDetailsS​​ervice 中使用 @Cacheable 时清空编码密码

java - Spring Boot 2 安全性 - 预验证 token - 允许健康检查