【问题标题】:Creating a delegating LdapAuthenticationProvider without using xml configuration, rather by java code创建委托 LdapAuthenticationProvider 而不使用 xml 配置,而不是通过 java 代码
【发布时间】:2020-12-07 12:13:46
【问题描述】:

我想写一个自定义的 LdapAuthenticationProvider 如下:

public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        LdapAuthenticationProvider ldap = new LdapAuthenticationProvider(); // don't know how to do 
                                                                                  instantiate this 
        Authentication authentication1 = super.authenticate(authentication);
       return new LdapToken(null,null,"",true,"");
   }

我的意图是将身份验证委托给默认的 LdapAuthenticationProvider,但在身份验证后返回一些自定义令牌。

但我无法创建 LdapAuthenticationProvider 的对象,因为 LdapAuthenticationProviderConfigurer 的构建方法实际上返回 LdapAuthenticationProvider 的对象是私有的。而且我不想使用 XML 配置文件。

谁能告诉我如何做到这一点?

【问题讨论】:

    标签: spring-boot rest spring-security ldap spring-ldap


    【解决方案1】:

    您需要使用AuthenticationManager,而不是直接调用LdapAuthenticationProvider

    您需要在配置中将其公开为可注入 bean:

    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
        @Bean
        @Override
        public AuthenticationManager authenticationManagerBean() throws Exception {
            return super.authenticationManagerBean();
        }
    }
    

    假设您的 LdapAuthenticationProvider 是一个可检测的 Bean,AuthenticationManager 将自动注入它。

    【讨论】:

    • 那它怎么知道它必须调用 LdapAuthenticationProvider 认证方法呢?这是 spring security 本身提供的一个类。我想保留默认的 ldap 提供程序,但最后我想返回我自己的类型令牌。我也有自己的过滤器和令牌类。但面临与提供商的问题
    猜你喜欢
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多