使用 Spring Security 登录时记录用户代理信息

发布于 2024-08-11 16:26:41 字数 304 浏览 0 评论 0原文

我在应用程序中使用 Spring Security 和 LDAP,并且希望在用户登录时记录用户代理。但是,我无法访问权限填充器中的请求对象,因此无法从那里访问用户代理信息。

我还尝试在记录器 (log4j) 中设置模式,以便它通过放入 %X{user-agent} 来记录用户代理,但这也不起作用。

我想要做的就是在用户登录时记录用户代理信息。因此,如果 Spring 的安全框架中有一个方法,我可以重写该方法,该方法将能够访问登录对象上的请求,这将是理想的。

或者

如果我可以让 log4j 在写入日志时记录用户代理,那也很好。

I am using Spring Security with LDAP for an application and I want to log the user-agent when the user logs in. However, I cannot access the request object in the Authorities Populator so I cannot access the user-agent information from there.

I also tried setting the pattern in our logger (log4j) so it would log the user agent by putting in %X{user-agent}, but that didn't work either.

All I want to do is log the user agent information when the user logs in. So if there is a method in Spring's security framework I can override that will have access to the request on login object that would be ideal.

OR

If I can get log4j to record the user agent whenver an entry to the log is made, that would be fine as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

未央 2024-08-18 16:26:41

扩展AuthenticationDetailsS​​ourceImpl

import javax.servlet.http.HttpServletRequest;
import org.acegisecurity.ui.AuthenticationDetailsSourceImpl;

public class UserAgentAuthenticationDetailsSourceImpl extends  
                               AuthenticationDetailsSourceImpl {

    public Object buildDetails(HttpServletRequest request) {
            String userAgent = request.getHeader("User-Agent");
            return super.buildDetails(request);
    }
}

在身份验证过滤器中设置:

<bean id="authenticationProcessingFilter"
      class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
    ...
    <property name="authenticationDetailsSource">
        <bean class="UserAgentAuthenticationDetailsSourceImpl"/>
    </property>
</bean>

Extend AuthenticationDetailsSourceImpl:

import javax.servlet.http.HttpServletRequest;
import org.acegisecurity.ui.AuthenticationDetailsSourceImpl;

public class UserAgentAuthenticationDetailsSourceImpl extends  
                               AuthenticationDetailsSourceImpl {

    public Object buildDetails(HttpServletRequest request) {
            String userAgent = request.getHeader("User-Agent");
            return super.buildDetails(request);
    }
}

Set it in the authentication filter:

<bean id="authenticationProcessingFilter"
      class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
    ...
    <property name="authenticationDetailsSource">
        <bean class="UserAgentAuthenticationDetailsSourceImpl"/>
    </property>
</bean>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文