LDAP を使用して Spring LDAP API で認証
Spring LDAP API認証を実施するには、以下のステップを実行する必要があります。
- Spring LDAPの依存関係を追加します。
まず最初に、プロジェクトにSpring LDAPライブラリを追加する必要があります。MavenもしくはGradleなどのビルドファイルに以下の依存関係を追加することで行えます。
メイブン:
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
gradle
implementation 'org.springframework.ldap:spring-ldap-core:2.3.1.RELEASE'
- LDAP接続の設定:
Spring BootアプリケーションでLDAP接続を設定するには、application.propertiesファイルに以下を追加する必要があります。
ldap.url=ldap://localhost:389
ldap.base.dn=dc=my-domain,dc=com
ldap.user.dn=cn=admin,dc=my-domain,dc=com
ldap.password=admin_password
LDAP サーバーの設定に合わせて変更を行えます。
- LDAP認証プロバイダーを作成する
Spring LDAP APIを使用してLDAP認証を実施するauthenticateメソッドをオーバライドしてAuthenticationProviderインターフェースを実装したクラスを作成します。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;
@Component
public class LdapAuthenticationProvider implements AuthenticationProvider {
@Value("${ldap.user.dn}")
private String ldapUserDn;
@Autowired
private LdapTemplate ldapTemplate;
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String username = authentication.getName();
String password = authentication.getCredentials().toString();
DirContextOperations context;
try {
context = ldapTemplate.authenticate(ldapUserDn, "(uid={0})", new Object[]{username}, password);
} catch (Exception e) {
throw new BadCredentialsException("Invalid LDAP username or password");
}
if (context == null) {
throw new BadCredentialsException("Invalid LDAP username or password");
}
return new UsernamePasswordAuthenticationToken(username, password, authentication.getAuthorities());
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
上記コードでは、LDAP認証を行うためにLdapTemplateが使用されています。このBeanをアプリケーションに注入できます。
- 認証の設定:
LdapAuthenticationProviderを認証マネージャに追加するには、Spring Securityの設定クラスで行います。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private LdapAuthenticationProvider ldapAuthenticationProvider;
@Override
protected void configure(AuthenticationManagerBuilder auth) {
auth.authenticationProvider(ldapAuthenticationProvider);
}
// ...
}
今、Spring SecurityでLDAP認証が可能になりました。