[JAVA] SpringMVC-Interceptor function

What is Interceptor

In Spring Framework, the Controller class with \ @Controller annotation receives the request and returns the response. DispatcherServlet extracts the Controller class from the request URL and executes it. Inerceptor is an interrupt process that is called before and after screen processing and at the very end of the request. If you use Inerceptor, you can add processing before and after executing Controller. For example, use Interceptor to output the IP address of the access source to the log.

Quote [soracane 01. Basic concept: Overall processing flow](https://sites.google.com/site/soracane/home/springnitsuite/spring-mvc/1-ji-ben-gai-nian-quan-ti-dena-chu- lifuro)

How the Interceptor works

Actual: org.springframework.web.servlet.HandlerInterceptor interface DispatcherServlet is running the HandlerInterceptor interface.

Method Return value Execution timing
preHandle booean Before executing Controller
postHandle void After executing Controller-Before view rental
afterCompletion void view After renting

HandlerInterceptor API Reference

How to create an Interceptor

Try to output the IP address accessed using Interceptor to the log file.

1. Create an implementation class for org.springframework.web.servlet.HandlerInterceptor 1. Define an interceptor in applicationContext.xml

In addition, use the Spring MVC project created by the following procedure. Spring MVC Project


Version information of each module verified this time

module version
STS 3.9.9
JDK 1.8.0_221
Tomcat 8.5
SpringFramework 3.1.1
Thymeleaf 2.1.6

How to create an Interceptor ①

Create an implementation class for org.springframework.web.servlet.HandlerInterceptor and Output the IP address in the preHandle method.

HogeIntercepter.java


package hogehoge;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class HogeIntercepter implements HandlerInterceptor {

	private static final Logger logger = LoggerFactory.getLogger(HogeIntercepter.class);
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		//TODO auto-generated method stub
		logger.info("URI:" + request.getRequestURI()); 
		logger.info("IP address:" + request.getRemoteAddr()); 
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		//TODO auto-generated method stub

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		//TODO auto-generated method stub

	}
}

How to create an Interceptor ②

Define an interceptor in applicationContext.xml.

applicationContext.xml


<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

・
・
・

	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/**" />
			<beans:bean class="hogehoge.HogeIntercepter" />
		</mvc:interceptor>
	</mvc:interceptors>

</beans:beans>

Execution result

Log output contents INFO : hogehoge.HogeIntercepter - URI:/hogehoge/ INFO: hogehoge.HogeIntercepter --IP address: 0: 0: 0: 0: 0: 0: 0: 1

Reference site

soracane Spring-MVC Tokkan Software Spring MVC Interceptor

Recommended Posts

SpringMVC-Interceptor function
Login function
JavaScript function
Password change function
Image preview function
Image posting function
[Rails] Category function
Rails follow function
Implemented comment function
[Rails] Notification function