在 Spring Security 中的 DaoAuthenticationProvider 身份验证期间访问 HttpServletRequest

2024-01-17

我需要访问HttpServletRequest来自我内部的对象DaoAuthenticationProvider在春季安全中。

安全组件扩展了DaoAuthenticationProvider我们重写authenticate方法来执行一些自定义身份验证/验证。需要进行额外的检查来验证用户的 IP 地址,该地址作为查询字符串参数出现在请求 url 中(例如:http://domain.com/context?ip=192.168.0.1).

我目前尝试的方法是利用RequestContextHolder线程本地并在我的自定义中获取http请求DAOAuthenticationProvider.

我在这里和春季论坛上读到的一些其他解决方案似乎建议注入AuthenticationDetailsSource, 用于custom-filter由于是 Spring Security 的新手,我不明白其他步骤。

我们将拥有不同的 Web 应用程序,它们使用相同的安全组件来执行身份验证。

有人可以指出我正确的方向或帮助我解决以前实施的任何方法吗?


您可以使用RequestContextHolder它实际上包含相同的请求,尽管 Spring Security 通常会包装传入的请求,因此您可能会得到不同的引用,具体取决于您是否放置了RequestContextFilter在 Spring Security 链之前或之后(请注意,您可以通过比较从RequestContextHolder与应用程序控制器中的请求)。

注入自定义也相对容易AuthenticationDetails正如你提到的:

package com.mycompany;

public class MyWebAuthenticationDetailsSource implements AuthenticationDetailsSource {
    public Object buildDetails(Object context) {
        return ((HttpServletRequest)context).getParameter("ip");
    }
}

Then use

<bean id="ads" class="com.mycompany.MyWebAuthenticationDetailsSource />

<bean id="formLoginFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
    <property name="authenticationDetailsSource" ref="ads" />
    <property name="authenticationManager" ref="authenticationmanager" />
</bean> 

并将其添加为自定义过滤器,如参考手册中所述。 3.1 命名空间直接支持这一点form-login element http://static.springsource.org/spring-security/site/docs/3.1.x/reference/appendix-namespace.html#nsa-form-login. Authentication.getDetails()然后将返回“ip”参数的值。

请注意,您可能不应该使用 3.0.4,因为它存在已知的安全漏洞。

另外,您能解释一下“ip”参数是如何设置的吗?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Spring Security 中的 DaoAuthenticationProvider 身份验证期间访问 HttpServletRequest 的相关文章

随机推荐