gpt4 book ai didi

spring-security - 更改 Spring Security 配置

转载 作者:行者123 更新时间:2023-12-04 17:39:15 25 4
gpt4 key购买 nike

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

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

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

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

有没有其他方法可以实现这一目标?

请让我知道如果我们能做到这一点以及如何做到这一点?

提前致谢。

拉吉

最佳答案

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

关于spring-security - 更改 Spring Security 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8324816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com