복붙노트

[SPRING] 사용자 이름과 원격 IP 주소에 따라 다른 AuthenticationProvider 사용

SPRING

사용자 이름과 원격 IP 주소에 따라 다른 AuthenticationProvider 사용

Spring Security 3.2 기반 애플리케이션에서는 사용자 이름과 원격 IP 주소의 특정 패턴에 따라 두 개의 다른 공급자에 대해 사용자를 인증해야합니다.

특정 규칙과 일치하는 경우에는 ActiveDirectoryLdapAuthenticationProvider에 대해 인증되어야하며, 그렇지 않으면 이미 존재하는 UserDetailsService 사용자 지정 구현을 사용하는 표준 AuthenticationProvider로 인증되어야합니다.

연장하려면 무엇이 필요합니까? AuthenticationManager 또는 AuthenticationProvider? 모든 예제 코드는 매우 높이 평가 될 것입니다 :-)

참고 : 이미 에 두 개의 노드를 추가하려고 시도했지만 제대로 작동했습니다. 하지만 그것은 내 Ldap-server가 모든 인증 시도 (심지어 그 용도로 사용되지 않는 경우도)

해결법

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

    1.위임자가 null을 리턴하는 호출과 일치하는 경우 / ip-address 패턴 검사를 수행하는 랩퍼를 작성할 수 있습니다.

    위임자가 null을 리턴하는 호출과 일치하는 경우 / ip-address 패턴 검사를 수행하는 랩퍼를 작성할 수 있습니다.

    public class FilteringAuthenticationProvider implements AuthenticationProvider {
        private final AuthenticationProvider delegate;
    
        public FilteringAuthenticationProvider(AuthenticationProvider delegate) { this.delegate=delegate;}
    
        public Authentication authenticate(Authentication authentication) throws AuthenticationException {
            Object details = authentication.getDetails();
            String username = authentication.getPrincipal().toString();
            String remoteAddress = null;
            if (details instanceof WebAuthenticationDetails) {
                remoteAddress = ((WebAuthenticationDetails) details).getRemoteAddress(); 
            }
    
            if (matches(remoteAddress, username)) {
                return delegate.authenticate(authentication);
            }
            return null
        }
    
        private boolean matches(String remoteAddress, String Username) {
            // your checking logic here
        }       
    }
    

    이 같은. 그런 다음 보안 구성에서 구성하고 ActiveDirectoryLdapAuthenticationProvider를 래핑하십시오.

    <sec:authentication-manager>
        <sec:authentication-provider ref="filteringLdapProvider" />
        <sec:authentication-provider>
            <user-service ref="customUserDetailsService" />
        </sec:authentication-provider>
    </sec:authentication-manager>
    
    <bean id="filteringLdapProvider" class="FilteringAuthenticationProvider">
        <constructor-arg ref="ldapProvider" />
    </bean>
    
    <bean id="ldapProvider" class="ActiveDirectoryLdapAuthenticationProvider">
    ...
    </bean>
    

    이 같은.

  2. from https://stackoverflow.com/questions/21381893/use-different-authenticationprovider-depending-on-username-and-remote-ip-address by cc-by-sa and MIT license