복붙노트

[SPRING] Spring LDAP에 LDAP 캐시를 추가하는 방법은 무엇입니까?

SPRING

Spring LDAP에 LDAP 캐시를 추가하는 방법은 무엇입니까?

빠른 쿼리를 허용하기 위해 LDAP 사용자 데이터를 로컬에 캐시하고 싶습니다. 스프링 LDAP이 그런 기능을 제공합니까? 어떻게해야합니까?

나는 인증과 인증을 위해 Spring Security 3.1과 Spring LDAP 1.3.1을 사용하고있다. 존재하는 경우 내장 메커니즘을 사용하여 LDAP 용 캐시를 갖는 것이 좋습니다.

applicationContext-ldap.xml :

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

    <!-- Ldap -->
    <jee:jndi-lookup id="ldapUrl" jndi-name="appName/ldapUrl" expected-type="java.lang.String" />
    <jee:jndi-lookup id="ldapUser" jndi-name="appName/ldapUser" expected-type="java.lang.String" />
    <jee:jndi-lookup id="ldapPassword" jndi-name="appName/ldapPassword" expected-type="java.lang.String" />

    <!-- for authentication and search purpose -->
    <bean id="ldapContextSource" class="org.springframework.ldap.core.support.LdapContextSource">
        <property name="url" ref="ldapUrl" />
        <property name="userDn" ref="ldapUser" />
        <property name="password" ref="ldapPassword" />
        <property name="pooled" value="true" />
    </bean>

    <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
        <property name="contextSource" ref="ldapContextSource" />
    </bean>

    <!-- for pagination search purpose  -->
    <bean id="dirContext" factory-bean="ldapContextSource" factory-method="getReadOnlyContext" scope="session"/>

    <bean id="singleLdapContextSource" class="org.springframework.ldap.core.support.SingleContextSource" scope="session">
        <constructor-arg ref="dirContext"/>
    </bean>

    <bean id="singleLdapTemplate" class="org.springframework.ldap.core.LdapTemplate" scope="session">
        <property name="contextSource" ref="singleLdapContextSource" />
    </bean>

</beans>

spring-security.xml :

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

    <!-- This is where we configure Spring-Security  -->
    <security:http
        auto-config="true"
        use-expressions="true"
        access-denied-page="/auth/denied"
    >
        <security:intercept-url pattern="/login" access="permitAll"/>
        <security:intercept-url pattern="/app/admin" access="permitAll"/>
        <security:intercept-url pattern="/app/common" access="hasRole('User')"/>
        <security:intercept-url pattern="/viol/home" access="permitAll"/>
        <security:intercept-url pattern="/app/users" access="permitAll"/>
        <security:intercept-url pattern="/admin/edit/*" access="hasRole('Administrator')"/>

        <security:form-login
                login-page="/auth/login" 
                authentication-failure-url="/auth/loginFailure" 
                default-target-url="/auth/authorize"/>

        <security:logout 
                invalidate-session="true" 
                logout-success-url="/auth/login"
                logout-url="/logout"/>
    </security:http>

    <security:authentication-manager>
        <security:ldap-authentication-provider
            server-ref="ldapContextSource"
            user-search-filter="(sAMAccountName={0})"
            user-search-base="dc=myDomain,dc=com"
         />
    </security:authentication-manager>
</beans>

당신의 도움을 주셔서 대단히 감사합니다!

해결법

  1. ==============================

    1.EhCacheBasedUserCache를 구성하고 ldap-user-service를 사용하면 다음과 같이 cache를 사용할 수 있습니다.

    EhCacheBasedUserCache를 구성하고 ldap-user-service를 사용하면 다음과 같이 cache를 사용할 수 있습니다.

        <authentication-manager>
       <authentication-provider>
        <ldap-user-service 
           user-search-filter="(sAMAccountName={0})" user-search-base="dc=myDomain,dc=com" cache-ref="userCache" />
       </authentication-provider>
    </authentication-manager>
    
  2. ==============================

    2.클라이언트에서 LDAP 쿼리 결과를 캐싱하면 보안 위험이 발생할 수 있으므로 Spring이 클라이언트 측 LDAP 캐싱을 제공하지 않는다고 생각합니다. 캐시는 특정 시점에 부실 데이터를 확실히 보유하게됩니다. 사용자의 이메일 / 집 주소, 예를 들어 역할 할당 및 기타 인증 / 권한 부여 관련 데이터 서버 측을 확장하여로드를 처리 할 수 ​​있으므로 훨씬 더 효율적입니다.

    클라이언트에서 LDAP 쿼리 결과를 캐싱하면 보안 위험이 발생할 수 있으므로 Spring이 클라이언트 측 LDAP 캐싱을 제공하지 않는다고 생각합니다. 캐시는 특정 시점에 부실 데이터를 확실히 보유하게됩니다. 사용자의 이메일 / 집 주소, 예를 들어 역할 할당 및 기타 인증 / 권한 부여 관련 데이터 서버 측을 확장하여로드를 처리 할 수 ​​있으므로 훨씬 더 효율적입니다.

    Spring 3.1 이후로 캐싱을 도입하는 것은 뛰어난 지원을 제공하기 때문에 캐싱 도입이 쉽습니다. 귀하의 경우에는 다음과 같이 사용자 정의 LdapContextSource를 사용하면 충분합니다.

    public class CachingLdapContextSource extends AbstractContextSource {
    
        @Override
        protected DirContext getDirContextInstance(Hashtable environment) 
            throws NamingException 
        {
            InitialLdapContext context = new InitialLdapContext(environment, null);
            return new CachingDirContextWrapper(context);
        }
    }
    

    래퍼 클래스는 단순히 모든 DirContext 메서드를 기본 구현에 위임하고 @Cacheable로 캐시 할 메서드를 데코 레이팅합니다.

    class CachingDirContextWrapper implements DirContext {
    
        private final DirContext delegate;
    
        CachingDirContextWrapper(DirContext delegate) {
            this.delegate = delegate;
        }
    
        @Override
        @Cacheable(value = "search")
        public NamingEnumeration<SearchResult> search(...)
        {
            return delegate.search(name, matchingAttributes, attributesToReturn);
        }
    
        ...
    }
    

    Spring에서 사용할 캐시 저장 영역을 구성하는 방법에 대한 자세한 내용은 공식 문서 및이 자습서를 참조하십시오.

    그러나 다시 한번, 당신은 이것을하지 않는 것이 좋을 것이라고 생각합니다.

  3. from https://stackoverflow.com/questions/16057854/how-to-add-ldap-cache-in-spring-ldap by cc-by-sa and MIT license