1

Active Directory 認証に Spring Security 3.1 を使用し、権限をロードするためにローカル データベースを使用しています。同様の例を見たことがありますが、正確に何を使用すればよいかはまだ明確ではありません。spring-security.xml の現在の設定は次のとおりです。

  <!-- LDAP server details -->
  <security:authentication-manager>
    <security:authentication-provider ref="ldapActiveDirectoryAuthProvider" />
  </security:authentication-manager>


  <beans:bean id="ldapActiveDirectoryAuthProvider" class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider">
    <beans:constructor-arg value="${ldap.domain}" />
    <beans:constructor-arg value="${ldap.url}" />
    <beans:property name="useAuthenticationRequestCredentials" value="true" />
    <beans:property name="convertSubErrorCodesToExceptions" value="true" />
  </beans:bean>

「BookStoreDbAuthPopulator.java」という名前のクラスがあります。このクラス内で、次のメソッドを呼び出しています。

    // Load additional authorities and create an Authentication object
    final List<GrantedAuthority> authorities = loadRolesFromDatabaseHere();

私にとってまだ明確でないこと:ロードされた権限をdbからUserDetailsに追加するために、「BookStoreDbAuthPopulator.java」はどのインターフェースを実装する必要がありますか? 「UserDetailsContextMapper」または「GrantedAuthoritiesMapper」または「AuthenticationProvider」?

このソリューションに基づいて: Spring Security 3 Active Directory 認証、データベース認証 "BookStoreDbAuthPopulator.java" は "AuthenticationProvider" を実装する必要があります。「BookStoreDbAuthPopulator.java」を「ldapActiveDirectoryAuthProvider」Bean のプロパティとして使用する必要があるかどうかは疑問です。

よろしくお願いします。

4

1 に答える 1

1

私の最終的な解決策は、「BookStoreDbAuthPopulator.java」が「UserDetailsContextMapper」を実装することです。

public class BookStoreDbAuthPopulator implements UserDetailsContextMapper {

   // populating roles assigned to the user from AUTHORITIES table in DB
   private List<SimpleGrantedAuthority> loadRolesFromDatabase(String username) {

      //"SELECT ROLE FROM AUTHORITIES WHERE LCASE(USERNAME) LIKE ?"
      ...
   }

   @Override
   public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) {
      List<SimpleGrantedAuthority> allAuthorities = new ArrayList<SimpleGrantedAuthority>();
      for (GrantedAuthority auth : authorities) {
        if (auth != null && !auth.getAuthority().isEmpty()) {
           allAuthorities.add((SimpleGrantedAuthority) auth);
        }
      }
      // add additional roles from the database table
      allAuthorities.addAll(loadRolesFromDatabase(username));
      return new User(username, "", true, true, true, true, allAuthorities);
   }

   @Override
   public void mapUserToContext(UserDetails user, DirContextAdapter ctx) {
   }

}

次に、spring-security.xml で

  <!-- AuthenticationManager: AuthenticationProvider, LDAP server details -->
     <security:authentication-manager alias="authenticationManager">
        <security:authentication-provider ref="ldapActiveDirectoryAuthProvider" />
     </security:authentication-manager>

  <beans:bean id="ldapActiveDirectoryAuthProvider" class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider">
     <!-- the domain name (may be null or empty). If no domain name is configured, it is assumed that the username will always contain the domain name. -->
     <beans:constructor-arg value="${ldap.domain}" />
     <!-- an LDAP url (or multiple URLs) -->
     <beans:constructor-arg value="${ldap.url}" />
     <!-- Determines whether the supplied password will be used as the credentials in the successful authentication token. -->
     <beans:property name="useAuthenticationRequestCredentials" value="true" />
     <!-- by setting this property to true, when the authentication fails the error codes will also be used to control the exception raised. -->
     <beans:property name="convertSubErrorCodesToExceptions" value="true" />
     <!-- for customizing user authorities -->
     <beans:property name="userDetailsContextMapper" ref="myUserDetailsContextMapper" />
  </beans:bean>
     <!-- Customizing UserDetail -->
  <beans:bean id="myUserDetailsContextMapper" class="com.mybookstore.mywebcomp.w.BookStoreDbAuthPopulator">
  </beans:bean>
于 2013-12-02T14:01:35.040 に答える