This article introduces the use of interceptors in Spring-Boot. They generally handle cross-domain processing in interceptors, allowing cross-domain access to projects. For detailed information on the use of interceptors, please refer to the official website.
Steps to implement a custom interceptor:
1. Create a class and implement the HandlerInterceptor interface.
2. Create a Java class to inherit WebMvcConfigurerAdapter and override the addInterceptors method.
2. Hand over the custom interceptor to spring management, and then manually add the object to the interceptor chain (add in the addInterceptors method).
Create an interceptor class
package com.example.springboot.config.intercepter;import org.springframework.stereotype.Component;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * @desc Custom interceptor, use @Component to let Spring manage its life cycle* @Author wangsh * @date 2018/5/6 17:06 * @return */@Componentpublic class TestIntercepter implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println(">>>preHandle>>>>>>>>>>>> Call before the request processing (before the Controller method call)"); setCrossDomain(response); return true;// Only when true returns will continue to execute downwards, return false to cancel the current request} @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // Call after the request is processed, but before the view is rendered (after the Controller method is called); System.out.println(">>>postHandle>>>>>>>>>>>>>>>>Call after the request is processed, but before the view is rendered (after the Controller method is called)"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // Called after the entire request ends, that is, executed after the DispatcherServlet renders the corresponding view (mainly for resource cleaning); System.out.println(">>>postHandle>>>>>>>>>>>>>> is called after the entire request ends, that is, after the DispatcherServlet renders the corresponding view (mainly for resource cleaning)"); } /** * @param @param response Setting file* @return void Return type* @throws * @Title: setCrossDomain * @Description: TODO (set cross-domain problem) */ private void setCrossDomain(HttpServletResponse response) { response.addHeader("Access-Control-Allow-Origin", "*"); response.addHeader("Access-Control-Allow-Methods", "POST,GET"); response.addHeader("Access-Control-Allow-Credentials", "true"); }}Add a custom interceptor to the interceptor chain
package com.example.springboot.config.config;import com.example.springboot.config.intercepter.TestIntercepter;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;/** * Register bean */@Configurationpublic class InterceptorConfig extends WebMvcConfigurerAdapter { @Autowired private TestInterceptor testInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(testInterceptor); }}Create controller class
package com.example.springboot.config.conroller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/user")public class UserController { @RequestMapping("/hello") public String hello(){ return "hello"; }}Create a startup service class
package com.example.springboot.config;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class SpringbootConfigApplication { public static void main(String[] args) { SpringApplication.run(SpringbootConfigApplication.class, args); }}Start the service test, visit http://localhost:8088/hello in the browser, print the log as follows, you can see that the interceptor has passed.
Summarize
The above is a detailed explanation of the use of springboot config interceptors introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message. The editor will reply to everyone in time!