SpringBoot自定义拦截器
SpringMVC具有统一的入口DispatcherServlet,所有的请求都通过DispatcherServlet。
DispatcherServlet是前置控制器,配置在web.xml文件中的。拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据某某规则分发到目标Controller来处理。
SpringMVC的拦截器HandlerInterceptor接口对应提供了三个preHandle,postHandle,afterCompletion方法。preHandle在业务处理器处理请求之前被调用,
postHandle在业务处理器处理请求执行完成后,生成视图之前执行,afterCompletion在DispatcherServlet完全处理完请求后被调用,可用于清理资源等。
在SpringBoot中使用拦截器首先需要实现HandlerInterceptor接口,然后将拦截器注册到MVC中,由MVC根据请求来执行不同的拦截器。
第一步:创建拦截器
package com.bjqdl.wzy.web.interceptors; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import com.bjqdl.wzy.core.ThreadLocalMap; import com.bjqdl.wzy.core.UserContext; import com.bjqdl.wzy.util.AssertUtil; import com.bjqdl.wzy.util.json.JSONUtil; import com.bjqdl.wzy.web.entity.User; import com.bjqdl.wzy.web.service.UserInfoService; import com.bjqdl.wzy.web.utils.HttpUtils; public class UserInfoInterceptor implements HandlerInterceptor{ private static final Logger logger = LoggerFactory.getLogger(UserInfoInterceptor.class); private UserInfoService userInfoService; public UserInfoInterceptor(UserInfoService userInfoService) { super(); this.userInfoService = userInfoService; } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { logger.debug("UserInfoInterceptor.preHandle"); String openid = request.getParameter("openid"); if (request.getRequestURL().toString().contains("/user/")){ logger.debug("注册用户!"); return true; } if (!AssertUtil.isVal(openid)) { logger.error("用户信息不完整,请在url后追加用户信息 url?openid=xxxx"); return false; } if (openid.equals(UserContext.getOpenID())) { return true; } ThreadLocalMap.reset(); User user = this.userInfoService.getUser(openid); if (AssertUtil.isVal(user)) { ThreadLocalMap.put(UserContext.USER_CONTEXT, JSONUtil.parseJSON(user)); ThreadLocalMap.put(UserContext.USER_CONTEXT_IP, HttpUtils.getIpAddress(request)); return true; } else { return false; } } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
第二步:注册到MVC中
自定义MVCConfig类,继承WebMvcConfigurerAdapter类,重现public void addInterceptors(InterceptorRegistry registry)方法,具体实现见代码
package com.bjqdl.wzy.web.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import com.bjqdl.wzy.web.interceptors.UserInfoInterceptor; import com.bjqdl.wzy.web.service.UserInfoService; @Configuration public class MVCConfig extends WebMvcConfigurerAdapter { @Autowired private UserInfoService userInfoService; /* (non-Javadoc) * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#addInterceptors(org.springframework.web.servlet.config.annotation.InterceptorRegistry) */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new UserInfoInterceptor(userInfoService)).addPathPatterns("/**"); super.addInterceptors(registry); } }
注意:
1、注解@Configuration是必备的;
2、addPathPatterns("/**");方法用于通知过滤器针对那些url进行拦截