SpringBoot配置拦截器

【配置步骤】

1.为类添加注解@Configuration,配置拦截器

2.继承WebMvcConfigurerAdapter类

3.重写addInterceptors方法,添加需要拦截的请求

@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter{
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //表示拦截所有的请求
        registry.addInterceptor(new InterceptorTest()).addPathPatterns("/*/**");
        super.addInterceptors(registry);
    }
}
public class InterceptorTest implements HandlerInterceptor{

    @Override
    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2) throws Exception {
        System.out.println("拦截成功");
        return false;
    }
        //HandlerInterceptor仍有其它方法,已省略
}

【测试拦截器】

1.配置mapping方法

    @RequestMapping("/interceptor")
    @ResponseBody
    String interceptorTest() {
        return "拦截器未开启";
    }

2.不开启拦截器,即把InterceptorTest.preHandle改为return true

3.开启拦截器,即把InterceptorTest.preHandle改为return false

原文地址:https://www.cnblogs.com/likailun/p/8734556.html

时间: 2024-08-26 14:13:02

SpringBoot配置拦截器的相关文章

springboot配置拦截器不能放行静态资源

新建一个拦截器类,实现 org.springframework.web.servlet.HandlerInterceptor 接口,重写preHandle.postHandle.afterCompletion方法分别是处理前.处理中.处理后. public class RequestInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, H

springboot+springmvc拦截器做登录拦截

springboot+springmvc拦截器做登录拦截 LoginInterceptor 实现 HandlerInterceptor 接口,自定义拦截器处理方法 LoginConfiguration 实现 WebMvcConfigurer 接口,注册拦截器 ResourceBundle 加载 properties文件数据,配置不进行拦截的路径 LoginInterceptor package com.ytkj.smart_sand.system.interceptor; import com.

27.Spring-Boot中拦截器中静态资源的处理(踩过坑)以及Spring mvc configuring拓展介绍

一.springboot中对静态资源的处理 默认情况下,springboot提供存放放置静态资源的文件夹: /static /public /resources /META-INF/resources 对于maven项目即就是存在src/main/resources 文件夹下. ? 如图:static文件夹就是springboot中默认的文件夹 在页面中这样写路径<link href="themes/bootstrap.min.css" rel="stylesheet&

spring原拦截器配置与新命名空间mvc:interceptors配置拦截器对照与注意事项

原先,我们是这么配置拦截器的 <bean id="openSessionInViewInterceptor"class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"> <property name="sessionFactory" ref="sessionFactory" /> </bean>

struts 2配置拦截器

在struts.xml文件中定义拦截器只需要为拦截器类指定一个拦截器名就可以了,拦截器使用<interceptor.../>元素来定义如: <interceptor name="defaultStack"/> 一般情况下,只需要通过上面的格式就可以完成拦截器的配置,如果需要在配置拦截器时传入拦截器参数,可使用<param.../>子元素.如: <interceptor name="defaultStack"/> <

spring原拦截器配置与新命名空间mvc:interceptors配置拦截器对比与注意事项

原先,我们是这么配置拦截器的 <bean id="openSessionInViewInterceptor"class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"> <property name="sessionFactory" ref="sessionFactory" /> </bean>

spring-boot 加入拦截器Interceptor

1.spring boot拦截器默认有 HandlerInterceptorAdapter AbstractHandlerMapping UserRoleAuthorizationInterceptor LocaleChangeInterceptor ThemeChangeInterceptor 2.配置spring mvc的拦截器WebMvcConfigurerAdapter public class WebAppConfig extends WebMvcConfigurerAdapter 3

SpringBoot的拦截器中依赖注入报空指针问题

原因:拦截器加载的时间点在springcontext之前,所以在拦截器中注入自然为null : 解决方法:配置拦截器链的类中先注入这个拦截器,示例: @Configuration public class WebConfig extends WebMvcConfigurerAdapter { //注入拦截器类 @Bean public LocaleInterceptor localeInterceptor  () { return new LocaleInterceptor(); } publi

spring boot 配置拦截器验证使用 token 登录

1.自定义登录注解 package io.xiongdi.annotation; import java.lang.annotation.*; /** * @author wujiaxing * @date 2019-07-12 * 登录校验 */ @Target(ElementType.METHOD) @Documented @Retention(RetentionPolicy.RUNTIME) public @interface Login { } 2.创建 token 实体类 packag