springboot(2)拦截器


静态资源

首先需要了解的是SpringBoot和之前的SpringMVC不太一样,SpringBoot对jsp页面的支持性很差,大家都知道jsp页面一般用在tomcat服务器上,而tomcat服务器一般需要war直接运行,SpringBoot一般不打war,一般打成jar。
 

Spring Boot 默认为我们提供了静态资源处理,使用 WebMvcAutoConfiguration 中的配置各种属性。
建议大家使用Spring Boot的默认配置方式,

拦截器

拦截器这玩意儿不多说了,一般写过SpringMVC的应该都写过,SpringBoot中本质上也差不多

要实现拦截器功能需要完成以下2个步骤:

  • 1.创建我们自己的拦截器类并实现 HandlerInterceptor 接口
  • 2.其实重写WebMvcConfigurerAdapter中的addInterceptors方法把自定义的拦截器类添加进来即可
package com.gwd.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
 
public class MyInterceptor implements HandlerInterceptor{
 
    @Override
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
        // TODO Auto-generated method stub
         System.out.println(">>>MyInterceptor>>>>>>>在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)");
    }
 
    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
            throws Exception {
        // TODO Auto-generated method stub
        System.out.println(">>>MyInterceptor>>>>>>>请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)");
    }
 
    @Override
    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
        // TODO Auto-generated method stub
         System.out.println(">>>MyInterceptor>>>>>>>在请求处理之前进行调用(Controller方法调用之前)");
         return true;// 只有返回true才会继续向下执行,返回false取消当前请求
    }
 
}

 

package com.gwd.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.gwd.interceptor.MyInterceptor;
 
@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter{
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // addPathPatterns 用于添加拦截规则
        // excludePathPatterns 用户排除拦截
        //addPathPatterns("/**")对所有请求都拦截,但是排除了/others和/login请求的拦截
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/*").excludePathPatterns("/login","/others");
        super.addInterceptors(registry);
    }
}
addPathPatterns对所有请求都拦截,但是排除了login和others(用逗号分隔开来)请求的拦截。

Controller:

package com.gwd.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class UserController {
    @RequestMapping("/hello")
    public String index() {
        return "hello";
    }
    
    @RequestMapping("/login")
    public String login() {
        return "login";
    }
}

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM