Spring Security LDAPの紹介

Spring Security LDAPの紹介

1. 概要

このクイックチュートリアルでは、Spring Security LDAPの設定方法を学習します。

始める前に、LDAPとは何かについて注意してください。LDAPはLightweight Directory Access Protocolの略であり、ネットワーク経由でディレクトリサービスにアクセスするためのオープンでベンダーに依存しないプロトコルです。

参考文献:

Spring LDAPの概要

Spring LDAP APIを使用してユーザーを認証および検索する方法、およびディレクトリサーバーでユーザーを作成および変更する方法を学びます。

Spring Data LDAPのガイド

LDAPでSpring Dataを使用する方法を学びます。

Spring SecurityによるSpringデータ

Spring DataとSpring Securityを統合する方法をご覧ください。

2. メーベン依存

まず、必要なMaven依存関係を見てみましょう。


    org.springframework.security
    spring-security-ldap



    org.apache.directory.server
    apacheds-server-jndi
    1.5.5

注:拡張可能で埋め込み可能なディレクトリサーバーであるLDAPサーバーとしてApacheDSを使用しました。

3. Java設定

次に、Spring SecurityJavaの構成について説明します。

public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.ldapAuthentication()
            .userSearchBase("ou=people")
            .userSearchFilter("(uid={0})")
            .groupSearchBase("ou=groups")
            .groupSearchFilter("member={0}")
            .contextSource()
            .root("dc=example,dc=com")
            .ldif("classpath:users.ldif");
    }
}

もちろん、これは構成のLDAP関連部分のみです。完全なJava構成はhereにあります。

4. XML構成

それでは、対応するXML構成を見てみましょう。


    
    


繰り返しますが、これは構成の一部であり、LDAPに関連する部分です。完全なXML構成はhereにあります。

5. LDAPデータ交換フォーマット

LDAPデータは、LDAPデータ交換形式(LDIF)を使用して表すことができます。ユーザーデータの例を次に示します。

dn: ou=groups,dc=example,dc=com
objectclass: top
objectclass: organizationalUnit
ou: groups

dn: ou=people,dc=example,dc=com
objectclass: top
objectclass: organizationalUnit
ou: people

dn: uid=example,ou=people,dc=example,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Jim Beam
sn: Beam
uid: example
userPassword: password

dn: cn=admin,ou=groups,dc=example,dc=com
objectclass: top
objectclass: groupOfNames
cn: admin
member: uid=example,ou=people,dc=example,dc=com

dn: cn=user,ou=groups,dc=example,dc=com
objectclass: top
objectclass: groupOfNames
cn: user
member: uid=example,ou=people,dc=example,dc=com

6. アプリケーション

最後に、簡単なアプリケーションを示します。

@Controller
public class MyController {

    @RequestMapping("/secure")
    public String secure(Map model, Principal principal) {
        model.put("title", "SECURE AREA");
        model.put("message", "Only Authorized Users Can See This Page");
        return "home";
    }
}

7. 結論

LDAPを使用したSpring Securityのクイックガイドでは、LDIFを使用して基本システムをプロビジョニングし、そのシステムのセキュリティを構成する方法を学びました。

このチュートリアルのfull implementationは、the github projectにあります。これはEclipseベースのプロジェクトであるため、そのままインポートして実行するのは簡単です。