SpringBoot中如何实现拦截器配置的实例代码示例?

2026-05-26 07:572阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计583个文字,预计阅读时间需要3分钟。

SpringBoot中如何实现拦截器配置的实例代码示例?

步骤:

1.实现 WebMvcConfigurer 配置类

2.实现拦截器

3.将拦截器添加到配置中

4.添加需要拦截的请求

5.添加需要排除的请求

java

package com.zp.springbootdemo.interceptor;

import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configurationpublic class WebMvcConfig implements WebMvcConfigurer {

@Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyInterceptor()) .addPathPatterns(/**) .excludePathPatterns(/exclude/**); }}

步骤:

1.实现WebMvcConfigurer配置类

2.实现拦截器 3

. 把拦截器添加到配置中

4.添加需要拦截的请求

5.添加需要排除的请求

package com.zp.springbootdemo.interceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * 和springmvc的webmvc拦截配置一样 * @author zp */ @Configuration public class WebConfigurer implements WebMvcConfigurer { /** * 拦截器配置 * @param registry */ @Override public void addInterceptors(InterceptorRegistry registry) { // addPathPatterns拦截所有请求,excludePathPatterns排除特殊的请求 //registry.addInterceptor(LoginInterceptor()).addPathPatterns("/**"); registry.addInterceptor(AuthorityInterceptor()).addPathPatterns("/**"); //excludePathPatterns 排除的请求 // registry.addInterceptor(LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/user/**"); // registry.addInterceptor(AuthorityInterceptor()).addPathPatterns("/**").excludePathPatterns("/user/**"); } /** * 拦截器一 * @return */ @Bean public LoginInterceptor LoginInterceptor() { return new LoginInterceptor(); } /** * 拦截器二 * @return */ @Bean public AuthorityInterceptor AuthorityInterceptor() { return new AuthorityInterceptor(); } }

基于URL实现的拦截器:

package com.zp.springbootdemo.interceptor; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.127.0.0.1:8080/user/getUser?token=123

结果:

基于注解的拦截器

①创建注解:

package com.zp.springbootdemo.interceptor; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 在需要登录验证的Controller的方法上使用此注解 */ @Target({ElementType.METHOD})// 可用在方法名上 @Retention(RetentionPolicy.RUNTIME)// 运行时有效 public @interface LoginRequired { }

②创建拦截器:

package com.zp.springbootdemo.interceptor; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.127.0.0.1:8080/user/getUser?token=123

SpringBoot中如何实现拦截器配置的实例代码示例?

结果:

测试接口代码2:

package com.zp.springbootdemo.system.controller; import com.zp.springbootdemo.interceptor.LoginRequired; import com.zp.springbootdemo.system.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.Map; @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/getUser") @LoginRequired public Map<String,String> getUser(@RequestParam Map<String,String> user){ return userService.getUser(user); } }

请求地址:

127.0.0.1:8080/user/getUser?token=123

结果:

到此这篇关于SpringBoot配置拦截器方式实例代码的文章就介绍到这了,更多相关SpringBoot配置拦截器内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计583个文字,预计阅读时间需要3分钟。

SpringBoot中如何实现拦截器配置的实例代码示例?

步骤:

1.实现 WebMvcConfigurer 配置类

2.实现拦截器

3.将拦截器添加到配置中

4.添加需要拦截的请求

5.添加需要排除的请求

java

package com.zp.springbootdemo.interceptor;

import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configurationpublic class WebMvcConfig implements WebMvcConfigurer {

@Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyInterceptor()) .addPathPatterns(/**) .excludePathPatterns(/exclude/**); }}

步骤:

1.实现WebMvcConfigurer配置类

2.实现拦截器 3

. 把拦截器添加到配置中

4.添加需要拦截的请求

5.添加需要排除的请求

package com.zp.springbootdemo.interceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * 和springmvc的webmvc拦截配置一样 * @author zp */ @Configuration public class WebConfigurer implements WebMvcConfigurer { /** * 拦截器配置 * @param registry */ @Override public void addInterceptors(InterceptorRegistry registry) { // addPathPatterns拦截所有请求,excludePathPatterns排除特殊的请求 //registry.addInterceptor(LoginInterceptor()).addPathPatterns("/**"); registry.addInterceptor(AuthorityInterceptor()).addPathPatterns("/**"); //excludePathPatterns 排除的请求 // registry.addInterceptor(LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/user/**"); // registry.addInterceptor(AuthorityInterceptor()).addPathPatterns("/**").excludePathPatterns("/user/**"); } /** * 拦截器一 * @return */ @Bean public LoginInterceptor LoginInterceptor() { return new LoginInterceptor(); } /** * 拦截器二 * @return */ @Bean public AuthorityInterceptor AuthorityInterceptor() { return new AuthorityInterceptor(); } }

基于URL实现的拦截器:

package com.zp.springbootdemo.interceptor; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.127.0.0.1:8080/user/getUser?token=123

结果:

基于注解的拦截器

①创建注解:

package com.zp.springbootdemo.interceptor; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 在需要登录验证的Controller的方法上使用此注解 */ @Target({ElementType.METHOD})// 可用在方法名上 @Retention(RetentionPolicy.RUNTIME)// 运行时有效 public @interface LoginRequired { }

②创建拦截器:

package com.zp.springbootdemo.interceptor; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.127.0.0.1:8080/user/getUser?token=123

SpringBoot中如何实现拦截器配置的实例代码示例?

结果:

测试接口代码2:

package com.zp.springbootdemo.system.controller; import com.zp.springbootdemo.interceptor.LoginRequired; import com.zp.springbootdemo.system.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.Map; @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/getUser") @LoginRequired public Map<String,String> getUser(@RequestParam Map<String,String> user){ return userService.getUser(user); } }

请求地址:

127.0.0.1:8080/user/getUser?token=123

结果:

到此这篇关于SpringBoot配置拦截器方式实例代码的文章就介绍到这了,更多相关SpringBoot配置拦截器内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!