用java监控系统的平台访问和花费时间

发布于 2024-12-02 06:03:05 字数 280 浏览 4 评论 0原文

如何使用或不使用 spring-security 在项目中实现以下功能。

1. LoggedIn Count:用户登录系统的次数。

2.花费时间:用户花费的总时间。

到目前为止,我已经考虑过使用javascript的解决方案,其中定期向服务器发送一个请求。在服务器内部,它将增加花费时间变量。

仍然想知道,spring-security 本身是否有任何东西。或者任何其他处理此类功能的想法。

任何想法/建议将不胜感激......

How can I implement following functionality in a project using or without using spring-security.

1. LoggedIn Count: Number of times the user has logged into the system.

2. Time Spend: Total time spent by user.

As of now, I have thought about the solution using javascript, in which one request will be sent to the server periodically. And inside the server it will increment the spendtime variable.

Still would like to know that, is there anything coming alongwith spring-security itself or not. Or any other idea to handle such functionality.

Any idea/suggestion would be highly appreciated...

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

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

发布评论

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

评论(3

感受沵的脚步 2024-12-09 06:03:05

我过去使用过完全相同的技术(只是不是 SpringSecurity)并且做了类似的事情,我想我可以让你知道如何做到这一点。我认为它会起作用:

1
要计算成功登录的名称,您将需要实体中的变量。
当用户成功登录时,该变量需要加一。我认为增加该值的最佳时机是验证正确并且授予访问权限(您可以在验证方法中实现)。
因此,您要做的就是使用 Hibernate 查询语言获取该用户的变量的当前值,加 1,然后更新该行。

2
对于这个,我不确定 100%,但你可以尝试一下:
User 表中名为 loginTime 的变量需要以 @ApplicationState 形式存在。

然后在注销函数中,您将创建一个 tempVariable 来保存当前时间的时间(一个长变量)。
您将执行减法操作(logOutTime - logInTime),您将获得该会话花费的时间(以毫秒为单位)。
因此,在擦除会话并真正注销之前,只需使用结果更新要注销的用户的数据库中的列即可。

另外我想提一下,这两个想法可能会使用编程方法解决问题,但我确信,如果您调查 Spring Security,您可能会找到在某种日志或类似日志中查找该信息的方法(从未使用过) Spring Security之前)

我希望我正确地解释了我的自我。
祝你好运

I worked with those exactly same technologies in the past(just not SpringSecurity) and i did a similar thing, i think i can give you an idea of how you could do that. I think it will work:

1
To count the name of successful logins, you will need a variable in an entity.
That variable needs to increase by one when the user successfully logs. I think the best time to increase that value is if the validation is correct and the access is granted(You could implement that in the validation method).
So what you do is get the current value of the variable for that user using the Hibernate Query Language, add 1 and then update the row.

2
For this one i am not sure at 100% but you can give it a try:
A variable in the User table called loginTime needs to exist as @ApplicationState.

Then in your logout function you will create a tempVariable that will hold the time(a long variable) at that current time.
You will do a substract operation(logOutTime - logInTime) and you will get the time in milliseconds expend on that session.
So just before erasing the session and loging out for real, just update a column in the DB for the user that is going to logout with the result.

Also i would like to mention that this 2 ideas might solve the problem using a programmatical approach, but i am sure that if you investigate about Spring Security, you might find the way of finding that info in some kind of logs or similar(Never used Spring Security before)

I hope i explained my self correctly.
Good Luck

嘿看小鸭子会跑 2024-12-09 06:03:05

1) 对于登录次数,最好的方法是使用 AuthenticationSuccessHandler 插入 Spring Security,如 @user395072 所说,并将结果保存在 numOfSuccessfulLogins 的用户表中,添加 +1每次成功登录时。

2) 对于在网站上花费的时间,您需要的只是一个会话变量,我们将其称为$MY_TIME。然后,在每个请求上,如果会话中不存在 $MY_TIME,则将其设置为当前日期(即 System.currentTimeMillis()) 。如果它确实存在,那么您将获取当前时间并减去 $MY_TIME 值,您将获得当前会话到目前为止所花费的时间。对于同样的请求,您需要坚持下去。当您将 $MY_TIME 设置为会话并每隔一段时间更新最新记录时,您可以有一个单独的表来添加新记录。或者您可以在用户表中有2个字段:cumulativeTimecurrentSessionTime,并且您添加更新cumulativeTime += currentSessionTimecurrentSessionTime = 0$MY_TIME 设置为会话时,以及在任何其他请求时更新 currentSessionTime = System.currentTimeMillis() - $MY_TIME

1) For the numer of logins the best way is to plug in to Spring Security using AuthenticationSuccessHandler, as @user395072 said and hold the result in user table in numOfSuccessfulLogins adding +1 on every successful login.

2) For the time spent on the site all You need is a session variable, lets'a call it $MY_TIME. Then on every request if $MY_TIME doesn't exist in session, you set it to the current date (i.e. System.currentTimeMillis()). If it does exist, than You take the current time and substract the $MY_TIME value and You have the time spent so far in current session. On the same request You need need to persist it. You can have a separete table for it adding a new record when You set the $MY_TIME to session and updating the newest one every other time. Or You can have 2 fields in user table: cumulativeTime and currentSessionTime, and You add update cumulativeTime += currentSessionTime and currentSessionTime = 0 when settin $MY_TIME to session, and on any other request update currentSessionTime = System.currentTimeMillis() - $MY_TIME.

我最亲爱的 2024-12-09 06:03:05

有很多方法可以做到这一点,这取决于您所遵循的架构。

您可以使用以下

protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, Authentication authResult) {
        SecurityContextHolder.getContext().setAuthentication(authResult);
        if (this.eventPublisher != null) {
            eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(authResult, this.getClass()));
        }
    }

使用 Spring Security

 <bean id="authenticationFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"
     p:sessionAuthenticationStrategy-ref="sas"
     p:authenticationManager-ref="authenticationManager"
    p:authenticationFailureHandler-ref="customAuthenticationFailureHandler"
    p:authenticationSuccessHandler-ref="customAuthenticationSuccessHandler"/>

或使用任何自定义过滤器

人们通常用于用户跟踪/分析的更多指针

  1. 捕获用户代理和用户信息
  2. 捕获登录和注销/会话到期时间
  3. 捕获 IP 地址
  4. 捕获不成功的登录尝试
  5. 通过登录捕获会话标识符

一旦所有这些信息都存储在持久存储中,他们通常会在其之上创建一个报告/过程以获取所有这些信息,例如一天中有多少用户,或者用户有多少次一个月内或多长时间内登录过他是否在该网站上度过了一个月等等...

There are many ways you can do this, it depends on the architecture that you are following.

you can use following

protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, Authentication authResult) {
        SecurityContextHolder.getContext().setAuthentication(authResult);
        if (this.eventPublisher != null) {
            eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(authResult, this.getClass()));
        }
    }

or

of using spring security

 <bean id="authenticationFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"
     p:sessionAuthenticationStrategy-ref="sas"
     p:authenticationManager-ref="authenticationManager"
    p:authenticationFailureHandler-ref="customAuthenticationFailureHandler"
    p:authenticationSuccessHandler-ref="customAuthenticationSuccessHandler"/>

or using any custom filters

Few more pointers which people generally use for user tracking/analysis

  1. capturing the user agent with user information
  2. capturing the login and logout / session expiry time
  3. capturing the IP address
  4. capturing the unsuccessful login attempts
  5. capturing the session identifier with login

Once all this information is stored in a persistent store, they usually create a report / procedure on top of it to get all those information like how many users in a single day, or how many times a user have logged-in during a month or how long did he spent on the site during a month etc...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文