Change Spring Security Configuration

We have a typical requirement in our application.

We have two Spring Security Configurations: 1. CAS Server 2. LDAP (NTLM)

So, now we need to check whether the CAS server is available or not, and use the CAS or LDAP security configuration based on the availability of the CAS server.

I tried to dynamically change the Entrypoint URL, however both configuration files use different beans / classes.

Is there any other way to achieve this?

Please let me know how we can achieve this and how?

Thanks in advance.

Rajah

+5
source share
1 answer

DelegatingAuthenticationEntryPoint, CasAuthenticationEntryPoint, CAS LoginUrlAuthenticationEntryPoint. :

public class DelegatingAuthenticationEntryPoint implements AuthenticationEntryPoint {
    private AuthenticationEntryPoint casAuthenticationEntryPoint;
    private AuthenticationEntryPoint ldapAuthenticationEntryPoint;

    public DelegatingAuthenticationEntryPoint(AuthenticationEntryPoint casAuthenticationEntryPoint,
        AuthenticationEntryPoint ldapAuthenticationEntryPoint) {
        this.casAuthenticationEntryPoint = casAuthenticationEntryPoint;
        this.ldapAuthenticationEntryPoint = ldapAuthenticationEntryPoint;
    }

    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
        throws IOException, ServletException {
        if(casServerAvailable()) {
            casAuthenticationEntryPoint.commence(request, response, authException);
        } else {
            ldapAuthenticationEntryPoint.commence(request, response, authException);
        }
    }

    private boolean casServerAvailable() {
        // TODO implement this method
        return false;
    }
}

DelegatingAuthenticationEntryPoint entry-point-ref, :

    <sec:http entry-point-ref="delegateEntryPoint">
      ...
    </sec:http>
<bean id="delegateEntryPoint" class="sample.DelegatingAuthenticationEntryPoint">
    <constructor-arg>
        <bean class="org.springframework.security.cas.web.CasAuthenticationEntryPoint"
            p:serviceProperties-ref="serviceProperties" 
            p:loginUrl="https://example.com/cas/login" />
    </constructor-arg>
    <constructor-arg>
        <bean class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"
            p:loginFormUrl="/login"/>
    </constructor-arg>
</bean>
+7

All Articles