将请求信息传递给Spring AuthenticationProvider

时间:2016-03-02 14:55:55

标签: java spring spring-mvc spring-security

如何在不创建自定义AuthenticationProvider的情况下,将请求特定信息从Filter传递给Authentication

AuthenticationProvider签名如下:

interface AuthenticationProvider {
    Authentication authenticate(Authentication authentication);
}

我正在考虑与静态SecurityContextHolder.getContext().getAuthentication()类似的内容,但需要特定于请求的信息。

背景

我想要它的原因是因为我想实现一个切换身份验证提供程序,该提供程序在运行时委托给另一个AuthenticationProvider

public class SwitchingAuthenticationProvider implements AuthenticationProvider {

    private AuthenticationProvider[] authProviders = // ...

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        return authProvider[GetCurrentContext.getIndex()].authenticate(authentication);
    }
}

1 个答案:

答案 0 :(得分:0)

您可以提供自己的AuthenticationEntryPoint实施,可以访问HttpServletRequest。在AuthenticationEntryPoint实施中,将AuthenticationProvider中所需的所有属性放在RequestContextHolder中,如下所示

RequestContextHolder.currentRequestAttributes().setAttribute(attributeName, attibuteValue, scope);

最后,您可以在身份验证提供程序中检索这些属性,如下所示

RequestContextHolder.currentRequestAttributes().getAttribute(attributeName,scope);
相关问题