springboot使用拦截器

1、按照Spring mvc的方式编写一个拦截器类;

2、编写一个配置类继承WebMvcConfigurationSupport或者WebMvcConfigurerAdapter类(在SpringBoot2.0及Spring 5.0 WebMvcConfigurerAdapter已经被废弃),或者实现WebMvcConfigurer接口

3、为该配置类添加@Configuration注解,标注此类为一个配置类,让Spring boot 扫描到;

4、覆盖其中的方法并添加已经编写好的拦截器:
@Override
public void addInterceptors(InterceptorRegistry registry) {
    //对来自 /api/** 链接的请求进行拦截,对登录请求/api/login不进行拦截
    registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/api/**").excludePathPatterns("/api/login");

}


*******************************************************************************************************************

配置类



import com.qjc.springboot.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Configuration 定义此类为一个配置类,spring容器启动后,这个配置类会作为一个bean存在于spring容器
 */
/*@Configuration
public class MyWebConfig extends WebMvcConfigurerAdapter {
//方式一
//WebMvcConfigurerAdapter Spring5.0以被废弃

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //对来自 /api/** 链接的请求进行拦截,对登录请求/api/login不进行拦截
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns(pathPatterns).excludePathPatterns(excludePathPatterns);

        //添加第二个拦截器
        //registry.addInterceptor(new LoginInterceptor2()).addPathPatterns(pathPatterns).excludePathPatterns(excludePathPatterns);
    }
}*/


/*@Configuration
public class MyWebConfig implements WebMvcConfigurer {

//方式二
//可以使用
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //需要拦截的
        String[] pathPatterns = {
                "/api/**"
        };

        //不需要拦截的
        String[] excludePathPatterns = {
                "/api/",
                "/api/hi",
        };
        //对来自 /api/** 链接的请求进行拦截,对登录请求/api/login不进行拦截
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns(pathPatterns).excludePathPatterns(excludePathPatterns);

    }
}*/


@Configuration
public class MyWebConfig extends WebMvcConfigurationSupport {
    //方式三
    //推荐使用WebMvcConfigurationSupport

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        //需要拦截的
        String[] pathPatterns = {
                "/api/**"
        };

        //不需要拦截的
        String[] excludePathPatterns = {
                "/api/",
                "/api/hi",
        };
        //对来自 /api/** 链接的请求进行拦截,对登录请求/api/login不进行拦截
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns(pathPatterns).excludePathPatterns(excludePathPatterns);

    }

//    @Override
//    protected void addViewControllers(ViewControllerRegistry registry) {
//
//        //registry.addViewController("/index").setViewName("index");
//
//    }

}

*****************************************************************************************************************

拦截器



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

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

/**
 * 登录拦截器
 *
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("已经进入登录拦截器......");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }
}

***************************************************************************************************************************

controller

package com.qjc.springboot.controller;

import com.qjc.springboot.config.ConfigInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class IndexController {

    /**读取自定义配置*/
    @Value("${qjc.name}")
    private String name;

    /**读取自定义配置*/
    @Value("${qjc.address}")
    private String address;

    @Autowired
    private ConfigInfo configInfo;

    @RequestMapping("/api/")
    public @ResponseBody String i() {
        return "Hello Springboot" + "--" + name + "--" + address + "--" + configInfo.getName() + "--" + configInfo.getAddress();
    }

    //@RequestMapping(value="/hi", method = RequestMethod.GET)
    @GetMapping("/api/hi")
    public @ResponseBody String hi() {
        return "Hello Springboot" + "--" + name + "--" + address + "--" + configInfo.getName() + "--" + configInfo.getAddress();
    }

    @GetMapping("/api/hello")
    public @ResponseBody String hello() {
        return "Hello Springboot" + "--" + name + "--" + address + "--" + configInfo.getName() + "--" + configInfo.getAddress();
    }

    @RequestMapping("/api/index")
    public String index(Model model) {
        model.addAttribute("data", "jsp");
        return "index";
    }

    @RequestMapping("/index")
    public String index1(Model model) {
        model.addAttribute("data", "jsp");
        return "index";
    }
}

**************************************************************************************************************************

application.yml

spring:
  profiles:
    active: dev

application-dev.yml

server:
  port: 8080
  servlet:
    context-path: /01-springboot-web

#自定义配置
qjc:
  name: www.qjc.com
  address: beijing
spring:
  mvc:
    view:
      prefix: /
      suffix: .jsp

#配置视图展示采用jsp

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值