How to do it...

Aside from JEE Filter implementation, let us use aspect to intercept some request-response transactions:

  1. Aside from implementing Filter, one obvious solution to monitor sessions during pre and post login transactions is to create interceptors. In the package org.packt.aop.transaction.controller, create a LoginController that will provide a login form and an employee list results page:
@Controller 
public class LoginController { 
   
  @Autowired 
  private EmployeeService employeeServiceImpl; 
   
  @RequestMapping(value="/login_emps.html", 
      method=RequestMethod.GET) 
  public String login(Model model, HttpServletRequest req){ 
     
    int browserNo = (Integer)     
        req.getAttribute("browserNo"); 
    if(browserNo == 3){ 
      model.addAttribute("error", "Browser Not Supported"); 
      return "browser_error"; 
    } 
    return "login";   
  } 
   
  @RequestMapping(value="/login_emps.html", 
      method=RequestMethod.POST) 
  public ModelAndView loginSubmit(ModelMap model, 
    @RequestParam("username") String username, 
    @RequestParam("password") String password){ 
 
    // refer to sources 
    return new    
       ModelAndView("redirect:/menu_emps.html",model); 
  } 
   
  @RequestMapping(value="/menu_emps.html", 
      method=RequestMethod.GET) 
  public String menu(Model model, HttpServletRequest req){ 
     
    List<Employee> emps =    
       employeeServiceImpl.readEmployees(); 
    model.addAttribute("emps", emps); 
    return "menu"; 
  } 
   
  @RequestMapping(value="/empty_login.html", 
      method=RequestMethod.GET) 
  public String emptylogin(){   
      // refer to sources   
  } 
   
  @RequestMapping(value="/browser_error.html", 
      method=RequestMethod.GET) 
  public String browserError(){ 
       // refer to sources  
   } 
} 
  1. Create an @Aspect class that will intercept /login_emps.html in processing the incoming HttpServletRequest. The class will filter and check the browser type of the user and will trigger the /browser_error.html page if the application is accessed through Internet Explorer:
@Component 
@Aspect 
public class LoginProxyAspect { 
   
  private Logger logger = 
      Logger.getLogger(LoginProxyAspect.class); 
   
  @Pointcut("within(@org.springframework 
  .stereotype.Controller *))") 
  public void classPointcut() {  } 
   
  @Pointcut("execution(*    
  org.packt.aop.transaction.controller 
  .LoginController.login(..))") 
  public void loginPointcut() {  } 
   
  @Before("classPointcut() && loginPointcut() 
  && args(model,req) && @annotation(mapping)") 
  public String browserCheck(JoinPoint joinPoint,  
     Model model, HttpServletRequest req,  
    RequestMapping mapping) throws ServletException,  
      IOException{ 
   
     logger.info("executing " + 
        joinPoint.getSignature().getName()); 
     logger.warn("MVC application trying to 
check browser type..."); 
     String loginRequestMethod =    
          mapping.method()[0].name(); 
     String username = req.getParameter("username"); 
     String password = req.getParameter("password"); 
     req.setAttribute("username", username); 
     req.setAttribute("password", password); 
     logger.info("executing " + 
        joinPoint.getSignature().getName() + " which is a " 
           + loginRequestMethod + " request"); 
     if(loginRequestMethod.equalsIgnoreCase("GET")){ 
          Enumeration<String> headers =     
              req.getHeaderNames(); 
          while(headers.hasMoreElements()){ 
          String headerName = headers.nextElement();    
          if(headerName.equalsIgnoreCase("user-agent")){ 
              String browserType =  
                  req.getHeader(headerName); 
              if(browserType.contains("Chrome")){ 
                 req.setAttribute("browserNo", 1);   
                 logger.info("MVC application uses  
                     Chrome..."); 
              }else if (browserType.contains("Firefox")){ 
                  req.setAttribute("browserNo", 2); 
                  logger.info("MVC application uses 
                     Firefox..."); 
                  }else{ 
                     req.setAttribute("browserNo", 3); 
                     logger.info("MVC appstops..."); 
                   } 
                 break; 
              } 
            } 
       } 
    return "login"; 
  } 
} 
  1. Save all files. Then clean, build, and deploy the ch05 project.
..................Content has been hidden....................

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