您可以创建DelegatingAuthenticationEntryPoint,如果CAS服务器已启动或委派给LoginUrlAuthenticationEntryPoint,它将委派给标准CasAuthenticationEntryPoint。实现看起来如下所示
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; } }
然后,您将使用类似于以下内容的entry-point-ref属性连接DelegatingAuthenticationEntryPoint:
<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>