Time for action – adding the data audit interceptor
Using a simple MVC interceptor, you can accomplish audit logging without making changes to your application code. Create an interceptor to record an audit log using the following steps:
Create a class called
AuditingInterceptor
under the packagecom.packt.webstore.interceptor
in the source foldersrc/main/java
and add the following code into it:package com.packt.webstore.interceptor; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; public class AuditingInterceptor extends HandlerInterceptorAdapter { Logger logger = Logger.getLogger("auditLogger"); private String user; private String productId; public boolean preHandle(HttpServletRequest request,HttpServletResponse arg1, Object handler) throws...