码迷,mamicode.com
首页 > 编程语言 > 详细

springboot拦截器实现

时间:2017-12-22 15:01:35      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:bcf   context   ati   figure   models   handler   pattern   hit   pat   

package webapp.dao.bcf;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import webapp.dao.db.DbWaterApi;
import webapp.models.water.WhitelistModel;
import webapp.utils.HttpUtil;

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

/**
 * @Author:VIC
 * @Description:
 */
@Configuration
public class InterceptorConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new IPInterceptor()).addPathPatterns("/**");
    }

    public class IPInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
            //IP白名单校验
            String ip = HttpUtil.getIP(request);
            WhitelistModel ipWhite = DbWaterApi.getIPWhite(ip);
            if (ipWhite.row_id == 0)
                return false;
            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 {

        }
    }
}

  

springboot拦截器实现

标签:bcf   context   ati   figure   models   handler   pattern   hit   pat   

原文地址:http://www.cnblogs.com/vicF/p/8085582.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!