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 interceptor in springboot

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

Share

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

This article mainly introduces "how to use interceptor in springboot". In daily operation, I believe many people have doubts about how to use interceptor in springboot. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use interceptor in springboot". Next, please follow the editor to study!

Using interceptors in springboot

Interceptor Interceptor is the core content of SpringMVC. Using the AOP (Aspect Oriented Programming, aspect-oriented programming) feature of spring, it is very convenient to extract the user's business code horizontally and enhance the application function according to the specific business requirements.

Using Interceptor in SpringBoot with full annotations involves the following interfaces and classes:

HandlerInterceptor: processor interceptor. Handler is the processor. In springboot web development, the controller handles the web request, so handler specifically instructs the controller.

Using fully annotated development, the @ Configuration annotation allows a java object to be directed to the IOC container and used as a configuration object, where the JavaConfig class is equivalent to a xml configuration file

In previous xml configurations

(1) configure by introducing some tags. In JavaConfig, the configuration is achieved by inheriting a class or implementing an interface. Here, the inherited class and the implemented interface are equivalent to the introduced tags.

(3) Personalized configuration can be realized by setting the attributes and values of the introduced tags, and personalized configuration can be realized by overriding classes or interfaces in JavaConfig.

The following is a case study to implement a custom interceptor

Intercept requests at the beginning of / user/, not intercept / usr/login requests

1. Define interceptor package cn.eis220.web;import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class LoginInterceptor implements HandlerInterceptor {@ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println ("preHandle method of logininterceptor executed"); return true;} 2. Use JavaConfig to register the interceptor

The java configuration class is equivalent to the xml configuration file

Xml is configured by introducing interceptor tags, and java configuration classes are configured by implementing WebMvcController.

In xml, the attributes and values of tags are modified to personalize the configuration, and the java configuration class implements WebMvcController for personalized configuration.

Package cn.eis220.config;import cn.eis220.web.LoginInterceptor;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configurationpublic class MyAppCofnig implements WebMvcConfigurer {@ Override public void addInterceptors (InterceptorRegistry registry) {LoginInterceptor loginInterceptor = new LoginInterceptor (); String [] path = {"/ user/**"}; String [] excludePath = {"/ user/login"} Registry.addInterceptor (loginInterceptor) .addPathPatterns (path) .origindePathPatterns (excludePath);}} 3. Define the controller and test the interceptor package cn.eis220.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class BootController {@ RequestMapping ("/ user/account") @ ResponseBody public String userAccount () {return "/ user/account" } @ RequestMapping ("/ user/login") @ ResponseBody public String userLogin () {return "/ user/login";}} at this point, the study on "how to use interceptor in springboot" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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