devstory

Le Tutoriel de Spring MVC Interceptor

  1. Qu'est ce que Spring Interceptor?
  2. Créer le projet Maven
  3. Déclarer Maven et web.xml
  4. Configurer Spring MVC
  5. Interceptor classes
  6. Controllers
  7. L'exécution des applications

1. Qu'est ce que Spring Interceptor?

Lorsque vous arrivez à une entreprise et que vous souhaitez rencontrer le directeur de l'entreprise. Vous devez traverser les intercepteurs (interceptors) qui peuvent être un portier, un réceptionniste, ...

Dans Spring, lorsqu'une demande est envoyée au Controller de ressort, elle devra traverser Interceptor(0 ou plus) avant d'être traitée par Controller.
Spring Interceptor est un concept qui est assez similaire à Servlet Filter.

Spring Interceptor est seulement appliqué aux commandes qui sont envoyées à un Controller.
Vous pouvez utiliser Interceptor pour effectuer quelques tâches, telles que l'éciture Log, l'ajout ou la mise à jour la configuration avant la commande soit traitée par Controller, ...
Une des applications Spring MVC utilisant Interceptor comme "Application web multilingue". Vous pouvez voir plus l'application ici:
Votre classe Interceptor doit mettre en oeuvre l'interface org.springframework.web.servlet.HandlerInterceptor ou extendre de la classe org.springframework.web.servlet.handler.HandlerInterceptorAdapter.
Vous devez mettre en oeuvre 3 méthodes abstraires:
  • public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
  • public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,ModelAndView modelAndView)
  • public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
Remarque: La méthode preHandle renvoie true ou false. Si elle renvoie true cela signifie que la demande continuera à atteindre sa destination (est un Controller) .
Chaque demande peut passer par beaucoup d'Interceptor. La figure ci-dessous illustre cela.

2. Créer le projet Maven

  • File/New/Other..
  • Group Id: org.o7planning
  • Artifact Id: SpringMVCInterceptor
  • Package: org.o7planning.springmvcinterceptor
Le projet est créé.
Assurez- vous que vous utilisez Java >=6.
Project Properties:

3. Déclarer Maven et web.xml

Utilisez Servlet API >= 3.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
   
    <display-name>SpringMVCInterceptor</display-name>
   
</web-app>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">


<modelVersion>4.0.0</modelVersion>
<groupId>org.o7planning</groupId>
<artifactId>SpringMVCInterceptor</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringMVCInterceptor Maven Webapp</name>
<url>http://maven.apache.org</url>


  <dependencies>
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
      </dependency>


      <!-- Servlet API -->
      <!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
          <scope>provided</scope>
      </dependency>


      <!-- Jstl for jsp page -->
      <!-- http://mvnrepository.com/artifact/javax.servlet/jstl -->
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
      </dependency>


      <!-- JSP API -->
      <!-- http://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
      <dependency>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>jsp-api</artifactId>
          <version>2.2</version>
          <scope>provided</scope>
      </dependency>



      <!-- Spring dependencies -->
      <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>4.3.3.RELEASE</version>
      </dependency>

      <!-- http://mvnrepository.com/artifact/org.springframework/spring-web -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>4.3.3.RELEASE</version>
      </dependency>

      <!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>4.3.3.RELEASE</version>
      </dependency>


  </dependencies>
 
 
  <build>
      <finalName>SpringMVCInterceptor</finalName>
      <plugins>

          <!-- Config: Maven Tomcat Plugin -->
          <!-- http://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin -->
          <plugin>
              <groupId>org.apache.tomcat.maven</groupId>
              <artifactId>tomcat7-maven-plugin</artifactId>
              <version>2.2</version>
            
              <!-- Config: contextPath and Port (Default - /SpringMVCInterceptor : 8080) -->
            
              <!--
              <configuration>
                  <path>/</path>
                  <port>8899</port>
              </configuration>
              -->
          </plugin>
      </plugins>
  </build>
 
</project>

4. Configurer Spring MVC

SpringWebAppInitializer.java
package org.o7planning.springmvcinterceptor.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class SpringWebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
        appContext.register(ApplicationContextConfig.class);

        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher",
                new DispatcherServlet(appContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }

}
ApplicationContextConfig.java
package org.o7planning.springmvcinterceptor.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration

@ComponentScan("org.o7planning.springmvcinterceptor.*")
public class ApplicationContextConfig {

    @Bean(name = "viewResolver")
    public InternalResourceViewResolver getViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

        viewResolver.setPrefix("/WEB-INF/pages/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }
 
}
Des Interceptor seront enregistrés avec l'application par la classe WebMvcConfig.
WebMvcConfig.java
package org.o7planning.springmvcinterceptor.config;

import org.o7planning.springmvcinterceptor.interceptor.AdminInterceptor;
import org.o7planning.springmvcinterceptor.interceptor.LogInterceptor;
import org.o7planning.springmvcinterceptor.interceptor.OldLoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    // Static Resource Config
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    //
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // LogInterceptor apply to all URLs.
        registry.addInterceptor(new LogInterceptor());

        // This URL is old, It no longer use.
        // Using OldURLInterceptor to redirecto to new URL.
        registry.addInterceptor(new OldLoginInterceptor())//
                .addPathPatterns("/admin/oldLogin");

        // This interceptor apply to URL like /admin/*
        // Exclude /admin/oldLogin
 
        registry.addInterceptor(new AdminInterceptor())//
                .addPathPatterns("/admin/*")//
                .excludePathPatterns("/admin/oldLogin");
    }

}

5. Interceptor classes

LogInterceptor est appliqué à toutes les demandes venant à un Controller. (Voir la configuration dans WebMvcConfig).
LogInterceptor.java
package org.o7planning.springmvcinterceptor.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class LogInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        long startTime = System.currentTimeMillis();
        System.out.println("\n-------- LogInterception.preHandle --- ");
        System.out.println("Request URL: " + request.getRequestURL());
        System.out.println("Start Time: " + System.currentTimeMillis());

        request.setAttribute("startTime", startTime);
         
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        System.out.println("\n-------- LogInterception.postHandle --- ");
        System.out.println("Request URL: " + request.getRequestURL());

        // You can add attributes in the modelAndView
        // and use that in the view page
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        System.out.println("\n-------- LogInterception.afterCompletion --- ");

        long startTime = (Long) request.getAttribute("startTime");
        long endTime = System.currentTimeMillis();
        System.out.println("Request URL: " + request.getRequestURL());
        System.out.println("End Time: " + endTime);

        System.out.println("Time Taken: " + (endTime - startTime));
    }

}
OldLoginInterceptor est un intercepteur (interceptor), si un utilisateur saisit le lien /admin/oldLogin,Il redirige vers le nouveau lien /admin/login.
OldLoginInterceptor.java
package org.o7planning.springmvcinterceptor.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class OldLoginInterceptor extends HandlerInterceptorAdapter {

   @Override
   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
           throws Exception {

       System.out.println("\n-------- OldLoginInterceptor.preHandle --- ");
       System.out.println("Request URL: " + request.getRequestURL());
       System.out.println("Sorry! This URL is no longer used, Redirect to /admin/login");
 
       response.sendRedirect(request.getContextPath()+"/admin/login");
       return false;
   }

   @Override
   public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
           ModelAndView modelAndView) throws Exception {
     
       // This code will never be run.
       System.out.println("\n-------- OldLoginInterceptor.postHandle --- ");
   }

   @Override
   public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
           throws Exception {
       // This code will never be run.
       System.out.println("\n-------- QueryStringInterceptor.afterCompletion --- ");
   }

}
AdminInterceptor.java
package org.o7planning.springmvcinterceptor.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class AdminInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        System.out.println("\n-------- AdminInterceptor.preHandle --- ");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        System.out.println("\n-------- AdminInterceptor.postHandle --- ");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        System.out.println("\n-------- AdminInterceptor.afterCompletion --- ");
    }

}

6. Controllers

MainController.java
package org.o7planning.springmvcinterceptor.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MainController {

   @RequestMapping(value = { "/", "/test" })
   public String test(Model model) {

       System.out.println("\n-------- MainController.test --- ");

       System.out.println(" ** You are in Controller ** ");

       return "test";
   }

   // This path is no longer used.
   // It will be redirected by OldLoginInterceptor or
   @Deprecated
   @RequestMapping(value = { "/admin/oldLogin" })
   public String oldLogin(Model model) {
       // Code here never run.
       return "oldLogin";
   }

   @RequestMapping(value = { "/admin/login" })
   public String login(Model model) {

       System.out.println("\n-------- MainController.login --- ");

       System.out.println(" ** You are in Controller ** ");

       return "login";
   }

}

7. L'exécution des applications

Exécutez le lien:
Et ici est Log observé sur l'écran Console:
Exécutez URL:
Observez Log: