【问题标题】:ActiveDirectoryLdapAuthenticationProvider and authentication using userDetailsServiceActiveDirectoryLdapAuthenticationProvider 和使用 userDetailsS​​ervice 的身份验证
【发布时间】:2020-12-01 00:30:39
【问题描述】:

我的应用程序中有两个不同的用户。 ldap 用户和 api 用户。 Ldap 用户有权访问一个端点,而 api 用户有权访问不同的端点。我已经使用 UserDetailsS​​ervice 实现了 api 用户身份验证,并在我的 application.yaml 文件中有详细信息。 我现在面临的问题是,只有 Ldap 用户应该访问的端点现在也被我的 api 用户访问。我该如何防止这种情况。请在下面找到我的代码 sn-p

public class ServiceSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    @Qualifier("ldapProvider")
    private AuthenticationProvider authenticationProvider;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

// security for apiuser
              http
                .authorizeRequests()
                .antMatchers(“/abcd/**).hasRole(“admin”)
                .and()
                .httpBasic().and().userDetailsService(userDetailsService());
      

// security for ldap users
        http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers(“/ghhgh” + "/**").fullyAuthenticated()
                .antMatchers("/login*").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().and()
                .authenticationProvider(authenticationProvider)
                .exceptionHandling();
    }

    public UserDetailsService userDetailsService() {

        UserDetails user = User.withUsername(“api”)
                .password(passwordEncoder().encode(“test”))
                .roles(“admin”)
              return new InMemoryUserDetailsManager(user);
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

【问题讨论】:

  • 从您的代码中,我无法判断为哪个用户组创建了哪个端点,但是如果您想将它们分开,请给 api 用户一个角色 api,给 ldap 用户一个角色 ldap然后使用这些角色保护您的端点.antMatchers("ldap_endpoint").hasRole("ldap").antMatchers("api_endpoint").hasRole("api")
  • @Am我已经编辑了我的帖子

标签: spring spring-boot security active-directory ldap


【解决方案1】:

在spring security中确实可以注册多种认证机制。

但是您无法将特定身份验证提供程序注册到特定路由。
春季安全文档说:

ProviderManagerAuthenticationManager 最常用的实现。 ProviderManager 代表ListAuthenticationProviders。每个AuthenticationProvider 都有机会表明身份验证应该成功、失败或表明它无法做出决定并允许下游AuthenticationProvider 做出决定。

所以在每个请求中,注册的AuthenticationProviders 会一个接一个地检查,直到一个成功,或者全部失败。

要解决您的问题,您需要定义多个自定义权限,然后分配给您的用户。
然后,您可以使用这些权限保护您的端点。

例如您授予每个 ldap 用户权限 LDAP_USER 和每个 api 用户权限 API_USER。然后你相​​应地配置你的安全性:

注册所有的AuthenticationProvider:


@Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {

        auth.authenticationProvider(ldapProvider);
        auth.userDetailsService(userDetailsService());
    }

并配置端点:


@Override
protected void configure(HttpSecurity http) throws Exception {


    http
      (...)
      .authorizeRequests()

// security for apiuser
      .antMatchers(“/abcd/**).hasRole(“API_USER”)

// security for ldap users
      .antMatchers(“/ghhgh” + "/**").hasRole("LDAP_USER")
      (...)
    }

【讨论】:

  • 嗨@Amir Schenell,感谢您的解释。我尝试了同样的方法,但我仍然能够使用 apiuser 访问 ldap 端点
  • 您可以将您当前的配置发布到您的问题中,这样我就可以乐在其中了吗?
  • 嗨@Amir Schnell,我错过了向 ldap 添加权限并修复了它。非常感谢你
猜你喜欢
  • 2015-06-02
  • 2015-10-12
  • 2015-10-16
  • 2018-12-06
  • 2018-02-15
  • 2013-09-29
  • 2017-05-10
  • 1970-01-01
相关资源
最近更新 更多