springboot工作总结

springboot使用拦截器

2017-07-30  本文已影响96人  二月_春风

看一下HandlerInterceptor接口定义,关于具体的HandlerInterceptor说明大家可以看看源码的官方doc说明,更加深入的了解这个接口,包括其一些实现:

HandlerInterceptor接口

preHandle:controller执行之前调用
postHandle:controller执行之后,且页面渲染之前调用
afterCompletion:页面渲染之后调用,一般用于资源清理操作

看一个demo,自己写一个拦截器实现HandlerInterceptor,重写三个方法,

package com.zhihao.miao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

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


public class LogHandlerInterceptor implements HandlerInterceptor {

    private Logger logger = LoggerFactory.getLogger(getClass());

    //请求执行之前调用
    @Override
    public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
        System.out.println("==========preHandle==========" + handler.getClass());
        return true;
    }

    //请求之后,且页面渲染之前调用
    @Override
    public void postHandle(HttpServletRequest request,HttpServletResponse response, Object handler,ModelAndView modelAndView) throws Exception {
       String UserAgent = request.getHeader("User-Agent");
       logger.info("UserAgent==="+UserAgent);
       String remoteIp = request.getRemoteHost()+","+request.getRequestURI();
       logger.info("remoteIp"+remoteIp);

    }

    //页面渲染之后调用,一般用于资源清理操作
    @Override
    public void afterCompletion(HttpServletRequest request,HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        System.out.println("===========afterCompletion=========");
    }
}

写一个类,继承WebMvcConfigurerAdapter抽象类,然后重写addInterceptors方法,并调用registry.addInterceptor注册拦截器并将该类加入到spring容器中,

package com.zhihao.miao;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootConfiguration
public class WebConfiguration extends WebMvcConfigurerAdapter {

    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LogHandlerInterceptor());
    }
}

定义一个Controller进行测试:

package com.zhihao.miao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @GetMapping("/user/home")
    public String home(){
        logger.info("user home");
        return "user home";
    }
}

启动类启动,

package com.zhihao.miao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

访问http://127.0.0.1:8080/user/home

总结:

编写一个拦截器步骤:

上一篇 下一篇

猜你喜欢

热点阅读