IDP 的 Spring SAML 预身份验证检查

Pau*_*l H 4 java spring spring-security saml spring-saml

我正在编写几个基于 spring 安全和 spring 安全 saml 扩展 (RC2) 的 Web 应用程序。

我以基本方式(基于 spring saml 文档中定义的示例)单点登录与多个服务提供商和身份提供商合作。

当用户访问 SP 上的受保护资源时,他将被转发到 IDP 上的受保护资源。所以因为用户还没有登录,他们被重定向到一个登录页面(标准的 spring 安全内容)。登录后,播放原始请求并完成 authNRequest/Response 并将用户重定向到原始安全资源。

我现在有一个要求,确保所有服务提供者必须在每次请求之前询问身份提供者用户是否登录(而不是在 SP 本地进行)。

我的理解是在每个请求期间存储和查询本地 (SP) 和远程 (IDP) 安全上下文,如果没有有效上下文,则用户将被转发到身份提供者以完成身份验证过程。

所以我的问题是,有没有办法可以在 SP 端配置 saml/spring 安全性以始终“ping”或要求 IDP 检查当前用户是否已登录,或者这种事情是否不必要/不受支持。

提前致谢

Vla*_*fer 5

您是对的,Spring SAML 在每个请求期间查询本地安全上下文,并在用户变为无效时将用户转发给 IDP。

定义上下文何时变得无效的典型机制是使用 SAML 的属性SessionNotOnOrAfter。该属性包含在从 IDP 发回的断言的 AuthenticationStatement 中。一旦时间超过SessionNotOnOrAfter.

如果您想对每个请求重新进行身份验证,您可以例如添加一个类似于以下内容的新自定义过滤器:

package fi.test;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.FilterInvocation;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

public class ReAuthenticateFilter extends GenericFilterBean {

    private static final String FILTER_APPLIED = "__spring_security_filterReAuthenticate_filterApplied";

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        FilterInvocation fi = new FilterInvocation(request, response, chain);
        invoke(fi);
    }

    protected void invoke(FilterInvocation fi) throws IOException, ServletException {

        if ((fi.getRequest() != null) && (fi.getRequest().getAttribute(FILTER_APPLIED) != null)) {
            fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
        } else {
            if (fi.getRequest() != null) {
                fi.getRequest().setAttribute(FILTER_APPLIED, Boolean.TRUE);
            }
        }

       Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        try {
            fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
        } finally {
            if (authentication != null) {
                authentication.setAuthenticated(false);
            }
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

然后,您将在 Spring 配置中包含过滤器:

<security:http entry-point-ref="samlEntryPoint">
    <security:custom-filter after="SECURITY_CONTEXT_FILTER" ref="reAuthenticateFilter"/>
    ...
</security:http>

<bean id="reAuthenticateFilter" class="fi.test.ReAuthenticateFilter"/>
Run Code Online (Sandbox Code Playgroud)

对每个请求重新进行身份验证是相当昂贵的操作(通过用户浏览器到 IDP 的往返),并且可能导致应用程序的响应性较差。