Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use custom interceptor in Spring

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report