How to record operation logs when a Tomcat session times out to meet security audit requirements

Source: Internet
Author: User

How to record operation logs when a Tomcat session times out to meet security audit requirements

As we all know, in actual Web applications, session management generally uses the Web Container session management function.

This is also true for using Tomcat as a Web server. In terms of security, try to avoid modifying and interfering with the session management function of Web containers.

The Tomcat session management function is certainly more comprehensive and reliable than we did. Besides, Tomcat is maintained by mainstream open-source communities and is developed and maintained by a dedicated team. Once a security vulnerability is discovered, it can also be quickly repaired.


In actual development, in order to meet the security audit requirements, once a Web application cancels a session, it should record the operation log, logout is generally divided into active logout by the operator, abnormal attack detected by the application, active logout session, timeout logout session.

For active logout and attack logout sessions, operation logs can be easily recorded to meet the requirements.

However, many users feel unable to cancel timeout sessions. A common method is to develop a heartbeat program and send heartbeat requests to the server through the client page, the server receives heartbeat and training rounds through threads to log out and record operation logs.

Although this can achieve the effect of recording operation logs, but the client sends many requests to the server, once the system concurrency is high, the service pressure on the server will increase, not a good way to deal.


The Web can easily configure the Session Timeout time. Should it be able to listen to the creation and cancellation of sessions? The answer is yes. Tomcat can indeed be implemented through HttpSessionListener. The Session creation event occurs every time a new session is created, similarly, a Session failure event occurs when a Session fails.

This interface also contains only two methods, corresponding to Session creation and failure:
Public void sessionCreated (HttpSessionEvent se );

Public void sessionDestroyed (HttpSessionEvent se );

So we only need to implement HttpSessionListener, and the operation log can be recorded in the sessionDestroyed method.


Specifically, we need to configure the listener class in the Web. xml file of our web application. The sample code and web. xml configuration are as follows:

1) Sample Code

public class SessionManager implements HttpSessionListener{    public void sessionCreated(HttpSessionEvent se)    {        // ....    }    public void sessionDestroyed(HttpSessionEvent se)    {        // ......    }}

2) web. xml configuration
<session-config>  <session-timeout>30</session-timeout>  </session-config> <listener>  <listener-class>com.test.SessionManager</listener-class>  </listener>  


How to record tomcat logs?

Is the project information recorded under/logs?

After the project is deployed in tomcat, will the printStackTrace () tomcat automatically record logs?

Generally, Exception logs are not recorded. You can use the log4j mechanism to embed tomcat logging and use the Logger class static method of log4j to write logs in catch blocks that throw exceptions, the premise is to configure the log4j log output information.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.