【问题标题】:Changing Spring Security configuration更改 Spring Security 配置
【发布时间】:2011-11-30 23:23:29
【问题描述】:

我们的应用程序中有一个典型的要求。

我们有两个 Spring Security 配置: 1.CAS服务器 2. LDAP (NTLM)

所以,现在我们需要检查 CAS 服务器是否可用,并根据 CAS 服务器的可用性使用 CAS 或 LDAP 安全配置。

我试图动态更改入口点 url,但是,两个配置文件都使用不同的 bean/类。

还有其他方法可以实现吗?

如果我们能做到这一点以及如何实现,请告诉我?

提前致谢。

拉吉

【问题讨论】:

    标签: spring-security


    【解决方案1】:

    您可以创建一个 DelegatingAuthenticationEntryPoint,如果 CAS 服务器已启动,则该 DelegatingAuthenticationEntryPoint 将委托给标准 CasAuthenticationEntryPoint,或者以其他方式委托给 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;
        }
    }
    

    然后,您将使用类似于以下的 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>
    

    【讨论】:

      猜你喜欢
      • 2012-01-23
      • 2011-01-08
      • 2020-03-04
      • 2017-10-18
      • 2017-07-17
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      相关资源
      最近更新 更多