当前位置: 首页 > 知识库问答 >
问题:

创建委托身份验证提供者(Spring Security)

龙宣
2023-03-14

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

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

根据用户名,我想选择是否要使用try a provider,这样,如果用户名不是以特定前缀(“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);;
    }
}

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

共有1个答案

季骏祥
2023-03-14
    @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
    }
 类似资料:
  • 问题内容: 我的Web应用程序有多个身份验证管理器(一个用于API,一个用于WEB访问)。该api应该仅具有基本的身份验证服务- 通过spring安全标记进行配置,如下所示: 我无法内联身份验证提供程序,因为我希望它可以被子bean配置覆盖。 我的问题是我无法在security:authentication-provider元素上定义别名/ id以在身份验证管理器中引用它。有一个简单的解决方法吗?

  • 我正在尝试在Spring身份验证服务器(Spring Security)中配置多个身份验证提供程序(主要和次要) 根据“身份验证”方法留档: 返回:包含凭据的完全经过身份验证的对象。如果AuthenticationProvider无法支持对传递的身份验证对象的身份验证,则可能返回null。在这种情况下,将尝试下一个支持提供的身份验证类的AuthenticationProvider。 如果身份验证失

  • 我在调试身份验证问题时遇到了这个代码片段: 我在调试和摆弄用户凭证时注意到,如果第一个身份验证提供者(即< code > userdailsservice )无法对我的用户进行身份验证,那么就会远程调用我的LDAP服务器来尝试对我的用户进行身份验证。但是,如果第一个身份验证提供者成功地对我的用户进行了身份验证,则不会调用第二个身份验证提供者。 我的问题是,列出这些身份验证提供者的工作方式是否使得如

  • 问题内容: 我正在尝试使用Google Chrome浏览器进行selenium测试。我想使用HTTP基本身份验证登录。在Selenium中未实现,因此建议加载扩展。我正在使用来自的代码 https://github.com/RobinDev/Selenium-Chrome-HTTP-Private- Proxy, 以及“如何使用chrome驱动程序使用Java覆盖selenium2中的基本身份验证

  • 我的目标是使用凭据进行两步身份验证。第一步是检查具有主体的用户是否在特殊数据库表中具有角色。其次是执行标准ldap身份验证。 我需要的是同时执行这两项检查,但身份验证提供程序的常见方法是在任何身份验证提供程序首次成功后声明身份验证成功。因此,我决定创建一个自定义的 AuthenticationProvider 实现,它调用 LdapAuthenticationProvider,然后执行数据库检查逻

  • 我与拉雷维尔和托拉斯合作,基础工作很好。我确实有一个问题似乎不是随包而来的。我想要的是,当我执行时,如果他们登录,它也会返回他们的角色。目前我知道我可以做