springboot2.1版本使用拦截器

springboot2.1版本的拦截器实现的接口跟以前的旧版本不一样:

代码如下:

首先我们创建一个java文件,我这里起的是:SessionInterceptor.java里面代码如下:

package com.example.phone.demo.interceptor;




import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class SessionInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {
      System.out.println("preHandle");
//      String token= request.getParameter("access_token");

        response.getWriter().print("fail");
//        return HandlerInterceptor.super.preHandle(request,response,handler);
        return true;
    }

    @Override
    public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception{
        System.out.println("postHandle");
        HandlerInterceptor.super.postHandle(request,response,handler,modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception {
        System.out.println("afterCompletion");
        HandlerInterceptor.super.afterCompletion(request,response,handler,ex);
    }
}

这里我们写要拦截的内容信息,写完之后我们就要去拦截器那里注册这个文件进行拦截:

在创建一个文件,我的是SessionConfiguration.java,内容代码如下:

package com.example.phone.demo.interceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class SessionConfiguration implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(new SessionInterceptor()).addPathPatterns("/");
         WebMvcConfigurer.super.addInterceptors(registry);
    }
}

这里的 registry.addInterceptor(new SessionInterceptor()).addPathPatterns("/");的意思是刚才我们的那个文件注册成拦截器,addPathPatterns()这个是要拦截器的路劲,如果想在这个路劲里面除掉一些路径不用进行拦截,那么在后面加上

.excludePathPatterns("/")括号里面就是不进行拦截的路径。

记得注意下实现的接口哦!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中使用Mybatis拦截器,需要进行以下步骤: 1.定义拦截器类,实现Mybatis的Interceptor接口。 ```java @Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}) }) public class MyInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { //拦截逻辑 return invocation.proceed(); } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { //设置拦截器属性 } } ``` 2.在Spring Boot的配置文件中配置拦截器。 ```yaml mybatis: configuration: #配置拦截器 #注意:mybatis下的configuration属性是Mybatis的Configuration对象,不是Spring Boot的配置文件 #使用Mybatis的配置文件时需要使用mybatis.config-location属性 #使用Spring Boot的配置文件时需要使用mybatis.configuration属性 #两者不能同时使用 plugins: - com.example.MyInterceptor ``` 3.在Mybatis的Mapper接口中使用@Intercepts注解指定拦截器。 ```java @Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}) }) public interface UserMapper { List<User> selectAll(); } ``` 其中,@Intercepts注解用于指定拦截器,@Signature注解用于指定拦截的方法。 总的来说,在Spring Boot中使用Mybatis拦截器的过程和在其他环境下使用Mybatis拦截器的过程类似,只需要在Spring Boot的配置文件中配置拦截器即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值