LDAPについてはほとんど何も知りませんが、Springのセキュリティについてはあまり知りませんが、LDAPインスタンスに対して認証するようにSpring Bootアプリを構成しようとしています。
adldap.company.comでldapサーバー名とdc = ad、dc = company、dc = comのベースdnが与えられました
単純なバインドを実行して機能するいくつかのpythonコードがあります。
LDAP_USERNAME = '[email protected]'
LDAP_PASSWORD = 'password'
base_dn = 'dc=ad,dc=company,dc=com' # not used for bind I guess, only search
try:
ldap_client = ldap.initialize('ldap://adldap.company.com')
ldap_client.set_option(ldap.OPT_REFERRALS,0)
ldap_client.simple_bind_s(LDAP_USERNAME, LDAP_PASSWORD)
except ldap.INVALID_CREDENTIALS as e:
ldap_client.unbind()
return 'Wrong username and password: %s' % e
except ldap.SERVER_DOWN:
return 'AD server not available'
このコードを実行すると、「[email protected]」としてパスワード「password」で正常にバインドされているようです。
また、authを処理する必要があると思うWebSecurityConfigクラスもあります。
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/secure")
.authorizeRequests()
.anyRequest().fullyAuthenticated()
.and()
.httpBasic();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.ldapAuthentication()
.userDnPatterns("uid={0}")
.contextSource()
.url("ldap://adldap.company.com");
//.url("ldap://adldap.company.com/dc=ad,dc=company,dc=com");
}
}
アプリで/ secureにアクセスすると、基本認証ポップアップが表示されますが、入力しようとすると401 Unauthorizedが表示されます。私は、ドメインなしで「[email protected]」を試してみましたが、それを{0}@adldap.company.comなどのuserDnPatternsに入れたり、他のものをたくさん入れたりしています。ベースDNが含まれるかどうかに関係なく、さまざまなURLを使用してみました。何も動作しないようです。何が欠けていますか?
また、これはユーザーを認証する正しい方法ですか?バインド認証とバインドと検索の両方について読みましたが、サーバーは匿名のバインドを許可していないため、バインドして検索を実行できる何らかの「アプリユーザー」が必要だと思いますよね?それは「良い」ですか?
Active Directoryには、通常のLDAP DNバインディングとは異なり、ユーザー認証用の独自の非標準構文があります。
Spring Securityは、Active Directory用の特別なAuthenticationProviderを提供します。
これを試して :
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/secure")
.authorizeRequests()
.anyRequest().fullyAuthenticated()
.and()
.httpBasic();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider());
}
@Bean
public AuthenticationManager authenticationManager() {
return new ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProvider()));
}
@Bean
public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider("adldap.company.com", "ldap://adldap.company.com");
provider.setConvertSubErrorCodesToExceptions(true);
provider.setUseAuthenticationRequestCredentials(true);
return provider;
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加