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