程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了连接多种身份验证机制Spring Boot Security大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决连接多种身份验证机制Spring Boot Security?

开发过程中遇到连接多种身份验证机制Spring Boot Security的问题如何解决?下面主要结合日常开发的经验,给出你关于连接多种身份验证机制Spring Boot Security的解决方法建议,希望对你解决连接多种身份验证机制Spring Boot Security有所启发或帮助;

Spring不会使用多个AuthenticationProvIDer身份验证请求,因此第一个(支持中的ArrayListAuthenticationProvIDer支持Authentication对象并成功身份验证请求的将是唯一使用的对象。在你的情况是activeDirectoryLdapAuthenticationProvIDer

ActiveDirectoryLdapAuthenticationProvIDer可以使用自定义AuthenticationProvIDer委托给LDAP并进行其他检查,而不是使用:

    CustomerAuthenticationProvIDer implements AuthenticationProvIDer{
        privtae ActiveDirectoryLdapAuthenticationProvIDer  delegate; // add additional methods to initialize delegate during your configuration

          @OverrIDe
         public Authentication authenticate(Authentication auth) throws 
             AuthenticationException {
            Authentication  authentication= delegate.authenticate(auth);
            additionalchecks(authentication);
           return auth;
           }


          @OverrIDe
          public Boolean supports(Class<?> authentication) {
            return Username@R_801_5747@dAuthenticationToken.class.isAssignableFrom(authentication);
          }

        public voID additionalcheck(Authentication authentication){
               // throw AuthenticationException when it's not allowed
        }

    }
@H_874_21@ 

解决方法

我的应用程序有一个安全配置,可通过验证用户身份LDAP。效果很好,但现在我想添加另一个AuthenticationProvider,对尝试进行身份验证的用户进行更多检查。因此,我尝试添加一个DbAuthenticationProvider(出于测试目的)始终拒绝访问。因此,当我尝试使用我的域帐户(适用于activeDirectoryLdapAuthenticationProvider)登录时,由于第二个提供程序的身份验证失败,因此无法访问该页面。

为了实现此目标,我使用了以下代码:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Value("${ad.domain}")
    private String AD_DOMAIN;

    @Value("${ad.url}")
    private String AD_URL;

    @Autowired
    UserRoleComponent userRoleComponent;

    @Autowired
    DbAuthenticationProvider dbAuthenticationProvider;

    private final Logger logger = LoggerFactory.getLogger(WebSecurityConfig.class);

    @Override
    protected void configure(httpSecurity http) throws Exception {
        this.logger.info("Verify logging level");
        http.authorizerequests().anyrequest().fullyAuthenticated().and().formLogin()
                .successHandler(new CustomAuthenticationsuccessHandler()).and().httpBasic().and().logout()
                .logoutUrl("/logout").invalidatehttpSession(true).deleteCookies("JSESSIONID");
        http.formLogin().defaultsuccessUrl("/",truE);
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider());
        auth.authenticationProvider(dbAuthenticationProvider);
    }

    @Bean
    public AuthenticationManager authenticationManager() {
        return new ProvideRMANager(Arrays.asList(activeDirectoryLdapAuthenticationProvider(),dbAuthenticationProvider));
    }

    @Bean
    public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
        ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(AD_DOMAIN,AD_URL);
        provider.setConvertSubErrorCodesToExceptions(true);
        provider.setUseAuthenticationrequestCredentials(true);
        return provider;
    }
}
@H_874_21@

这是我的DbAuthenticationProvider

@Component
public class DbAuthenticationProvider implements AuthenticationProvider {

    Logger logger = LoggerFactory.getLogger(DbAuthenticationProvider.class);

    @Override
    public Authentication authenticate(Authentication auth) throws AuthenticationException {
        auth.setAuthenticated@R_772_5279@;
        this.logger.info("Got initialized");
        return auth;
    }

    @Override
    public Boolean supports(Class<?> authentication) {
        return true;
    }

}
@H_874_21@

可悲的是,我能够登录(访问没有像我预期的那样被拒绝)。我错过了什么吗?

大佬总结

以上是大佬教程为你收集整理的连接多种身份验证机制Spring Boot Security全部内容,希望文章能够帮你解决连接多种身份验证机制Spring Boot Security所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。