How to do it...

To apply an interceptor that will monitor and evaluate the /login.html request:

  1. Let us create a class of HandlerInterceptor type that keeps track of the access time of each user:
public class LoginInterceptor implements     
    HandlerInterceptor{ 
 
@Override 
public void afterCompletion(HttpServletRequest request,  
  HttpServletResponse response, Object handler,  
Exception ex) throws Exception { 
         System.out.println("INFO LOG ......  
       Fully Done login transaction....."); 
} 
 
@Override 
public boolean preHandle(HttpServletRequest request,  
  HttpServletResponse response, Object handler) 
    throws Exception { 
System.out.println("INFO LOG ......  
Beginning login transaction....."); 
Long startLog = System.currentTimeMillis(); 
Cookie startTime = new Cookie("startLog", 
   startLog.toString()); 
response.addCookie(startTime); 
System.out.println("INFO LOG ......  
Done Computing Start Time....."); 
return true; 
} 
 
@Override 
public void postHandle(HttpServletRequest request,  
  HttpServletResponse response, Object handler, 
ModelAndView modelAndView) throws Exception { 
System.out.println("INFO LOG ......  
       User Successfuly logged in.....");   
 } 
} 

The interface org.springframework.web.servlet.HandlerInterceptor has been used to implement the three helper methods, namely preHandle(), postHandle(), and afterCompletion().

  1. Implement another interceptor using the class org.springframework.web.servlet.handler.HandlerInterceptorAdapter, whose task is to audit the time duration the user has spent with the application. This component also logs the end of the whole MVC transactions:
public class AfterLogoutInterceptor extends  
  HandlerInterceptorAdapter{ 
   
  @Override 
  public boolean preHandle(HttpServletRequest request, 
     HttpServletResponse response, Object handler) 
      throws Exception { 
     
System.out.println("INFO LOG ......  
Entering After Logout transaction....."); 
   Long startLog = null; 
   Cookie[] allCookies = request.getCookies(); 
   for(Cookie c : allCookies){ 
      if(c.getName().equalsIgnoreCase("startLog")){ 
        startLog = Long.parseLong(c.getValue()); 
        System.out.println(c.getValue()); 
        break; 
      } 
    } 
   long elapsed = System.currentTimeMillis() -  
          startLog.longValue(); 
   System.out.println("----------Time Elapsed: " +  
        (elapsed/1000) + " sec ---------------"); 
   return true; 
  } 
   
  @Override 
  public void afterCompletion(HttpServletRequest request,  
    HttpServletResponse response, Object handler,  
      Exception ex) throws Exception { 
      System.out.println("INFO LOG ......  
        Fully Done Logout transaction....."); 
  } 
} 
  1. To make the interceptors work, open the root context SpringDispatcherConfig and override the addInterceptors() to register the two custom interceptors, LoginInterceptor and AfterLogoutInterceptor:
@Override 
public void addInterceptors(InterceptorRegistry registry) { 
  registry.addInterceptor( 
new LoginInterceptor()).addPathPatterns("/login.html"); 
  registry.addInterceptor( 
new AfterLogoutInterceptor()) 
.addPathPatterns("/after_logout.html*"); 
} 
  1. Save all files. Clean, build, and deploy the ch04 project.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.158.36