4

我正在编写几个基于 spring security 和 spring security saml extension (RC2) 的 Web 应用程序。

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

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

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

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

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

提前致谢

4

1 回答 1

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);
            }
        }

    }

}

然后,您将在 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"/>

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

于 2014-06-25T19:39:29.670 回答