// 注入spring容器
@Component
// 定义filterName 和过滤的url
@WebFilter(filterName = "my2Filter" ,urlPatterns = "/*")
public class My2Filter implements Filter {@Override
br/>@Override建宁网站建设公司创新互联公司,建宁网站设计制作,有大型网站制作公司丰富经验。已为建宁近千家提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的建宁做网站的公司定做!
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("filter2");
}
@Override
public void destroy() {
}
}
接下来实现一个springboot里面的拦截器的例子。
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new GlobalInterceptor()).addPathPatterns("/**");
}
}
import cn.vcinema.tool.security.session.SessionValidateResultEnum;
import cn.vcinema.tool.security.session.UserSessionInfo;
import cn.vcinema.tool.security.session.UserSessionUtils;
import com.pumpkin.conf.MallConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GlobalInterceptor implements HandlerInterceptor {
private final static Logger LOGGER = LoggerFactory.getLogger(GlobalInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String userId = request.getHeader("user_id");
String sessionId = request.getHeader("session_id");
String deviceId = request.getHeader("device_id");
LOGGER.info("headers:user_id=" + userId + ",session_id=" + sessionId + ",device_id=" + deviceId);
UserSessionInfo info = new UserSessionInfo(userId, sessionId, deviceId);
SessionValidateResultEnum resultEnum = UserSessionUtils.validateSession(MallConfig.getRunMode(), info);
return true;
}
}