3

私は、LDAPプロトコルを使用してActive Directoryに対して認証するためにSpring Securityを使用しています。コードの後に​​は、認証にうまく機能し、あまりにも(springSecurity.xml)をLDAPテンプレートを設定する:LDAP-AD Usernameコンテキストソースのパスワード設定

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/security" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:context="http://www.springframework.org/schema/context" 
      xmlns:beans="http://www.springframework.org/schema/beans" 
      xmlns:ldap="http://www.springframework.org/schema/ldap" 
      xsi:schemaLocation=" 
      http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd 
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context.xsd 
      http://www.springframework.org/schema/ldap 
      http://www.springframework.org/schema/ldap/spring-ldap.xsd"> 

    <http use-expressions="true"> 

     <intercept-url pattern="/login" access="permitAll" /> 
     <intercept-url pattern="/authenticated" access="isAuthenticated()" /> 

     <form-login login-page="/login" default-target-url="/authenticated" 
      authentication-failure-url="/login?error=true" /> 
     <logout /> 


    </http> 

    <beans:bean 
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <beans:property name="location"> 
      <beans:value>classpath:/ldap.properties</beans:value> 
     </beans:property> 
     <beans:property name="SystemPropertiesMode"> 
      <beans:value>2</beans:value> <!-- OVERRIDE is 2 --> 
     </beans:property> 
    </beans:bean> 

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


    <authentication-manager> 
     <authentication-provider ref="adAuthenticationProvider" /> 
    </authentication-manager> 

<!-- Ldap after authentication --> 

    <context:property-placeholder location="classpath:/ldap.properties" 
     system-properties-mode="OVERRIDE" /> 
    <context:annotation-config /> 

    <ldap:context-source id="contextSource" 
         password="${sample.ldap.password}" 
         url="${sample.ldap.url}" 
         username="${sample.ldap.userDn}" 
         base="${sample.ldap.base}" 
         referral="follow" /> 

    <ldap:ldap-template id="ldapTemplate" 
     context-source-ref="contextSource" /> 

    <ldap:repositories base-package="com.domain" /> 

    <beans:bean class="com.service.UserService"> 
     <beans:property name="directoryType" value="${sample.ldap.directory.type}" /> 
    </beans:bean> 

    <!-- Required to make sure BaseLdapName is populated in UserService --> 
    <beans:bean 
     class="org.springframework.ldap.core.support.BaseLdapPathBeanPostProcessor" /> 

</beans:beans> 

login.jspをからj_usernameとj_passwordというのフェッチ中に認証が正常に動作します。 ldapテンプレートを設定するには、私はプロパティファイルで定義されたusernameとpassword属性を使用していますが、私は春のセキュリティから同じユーザ名とパスワードを使いたいと思っています。 ldap:context-source id = "contextSource"でUsernameとPassword属性のプロパティをバインドする方法として私に教えてください。

コードが少し乱雑ですが、改善のための入力が歓迎されます。

答えて

3

リファレンスドキュメントのConfiguration chapterで指定されているように、ContextSourceのSpring Security認証を使用するには、ContextSourceの構成要素にカスタムauthentication-source-refを指定します。あなたの場合は、SpringSecurityAuthenticationSourceを使用し、Spring Securityに同梱されています。

+0

私はこのソリューションを前に試しましたが、別のクラスを使用して春のセキュリティ認証を取得しました。 :) – NewBee

関連する問題