Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use custom interceptor in Spring

Shulou Source: shulou.com Published: 2022-06-01 17:51:57 09月25日 Update

Editor to share with you how to use custom interceptors in Spring, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

1. Create a custom interceptor class (UserTokenInterceptor) and implement the HandlerInterceptor interface, and then rewrite the method. The code is as follows:

Before public class UserTokenInterceptor implements HandlerInterceptor {/ * @ description visits Controller, execute * / @ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {log.info ("enter interceptor,"); return true / / true can be released, but false will not release} / * * @ description request to access Controller, before rendering view * / @ Override public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {} / * * @ description request to access Controller After rendering the view * / @ Override public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {}}

2. Configure the Web interceptor (WebMvcConfig) and implement the WebMvcConfigurer interface of Spring

In Spring Boot version 1.5, custom interceptors, message converters, etc., are added by rewriting WebMvcConfigurerAdapter. After SpringBoot 2.0, the class is marked @ Deprecated (deprecated). It is officially recommended to implement WebMvcConfigurer directly or inherit WebMvcConfigurationSupport directly. Method 1 implements the WebMvcConfigurer interface (recommended)

Copy the addInterceptors method and add your own interceptor

Import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @ Configurationpublic class WebMvcConfig implements WebMvcConfigurer {@ Override public void addInterceptors (InterceptorRegistry registry) {/ / register interceptor registry.addInterceptor (getUserTokenInterceptor ()) .addPathPatterns ("/ index/session"); / / be sure to add WebMvcConfigurer.super.addInterceptors (registry);} / @ Bean public UserTokenInterceptor getUserTokenInterceptor () {return new UserTokenInterceptor ();}}

3. Write test Controller

@ RestController@RequestMapping ("/ index") public class IndexController {@ Deprecated @ GetMapping ("/ session") public Object getSession (HttpServletRequest request) {HttpSession session = request.getSession (); session.setAttribute ("name", "lequal"); session.setMaxInactiveInterval (3600); String name = (String) session.getAttribute ("name"); System.out.println ("obtained name is" + name); return name }}

4. Access the URL address

If the return false in the preHandle method, the browser access will not see the returned content, because it is blocked, it is equivalent to being stuck there.

The above is all the content of the article "how to use Custom interceptors in Spring". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Interceptors methods content interfaces articles views recommendations not much code names addresses most official methods more tags browsers messages versions knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Docker vpn MariaDB Shulou Tech Info