SpringMVC 拦截器配置
一. SpringMVC 拦截器
public class Interceptor implements HandlerInterceptor{
private Logger logger = LoggerFactory.getLogger(Interceptor.class);
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
}
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
// TODO Auto-generated method stub
}
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
String flag = null;
flag = request.getParameter("auth");
if(StringUtils.isEmpty(flag) || !flag.equals("php")){
/*logger.error("error-auth:{}", flag);
response.getWriter().append("error-auth");
return false;*/
} else {
logger.info("通过校验!");
return true;
}
return true;
}
}
向 SpringBoot 注册拦截器:
@Configuration
public class MyWebAppConfigurer implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new Interceptor()).addPathPatterns("/**");
}
}
二. Servlet 过滤器
/*
* 如果是一个过滤器,在 MyFilter 类上加上 @WebFilter(filterName = "myFilter",urlPatterns = {"/*"}) ,入口类加上 @ServletComponentScan 即可
*/
public class MyFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println(request.getParameter("name"));
HttpServletRequest hrequest = (HttpServletRequest)request;
HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) response);
if(hrequest.getRequestURI().indexOf("/index") != -1 ||
hrequest.getRequestURI().indexOf("/asd") != -1 ||
hrequest.getRequestURI().indexOf("/online") != -1 ||
hrequest.getRequestURI().indexOf("/login") != -1
) {
chain.doFilter(request, response);
}else {
// wrapper.sendRedirect("/login");
chain.doFilter(request, response);
}
}
@Override
public void destroy() {
}
}
SpringBoot 项目配置过滤器:
@Configuration
public class MyWebAppConfigurer implements WebMvcConfigurer {
/**
* 注册过滤器
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Bean
public FilterRegistrationBean filterRegist() {
FilterRegistrationBean frBean = new FilterRegistrationBean();
frBean.setFilter(new MyFilter());
// frBean.setOrder(1);//多个过滤器时指定过滤器的执行顺序
frBean.addUrlPatterns("/*");
System.out.println("filter");
return frBean;
}
}
三. Servlet 监听器
/**
* @date: 2019/4/1
* @description: ServletRequestListener、 HttpSessionListener 、ServletContextListener ......
* 可直接在 MyListener 类上使用 @WebListener 注解,入口类加上 @ServletComponentScan 即可
*/
public class MyListener implements HttpSessionListener {
private Logger logger = LoggerFactory.getLogger(this.getClass());//用户 this.getClass(), 粘贴不容易出错
public static int online = 0;
@Override
public void sessionCreated(HttpSessionEvent se) {
online ++;
logger.info("online 在线人数为:" + online);
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
}
}
SpringBoot 项目配置监听器:
@Configuration
public class MyWebAppConfigurer implements WebMvcConfigurer {
/**
* 注册监听器
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Bean
public ServletListenerRegistrationBean listenerRegist() {
ServletListenerRegistrationBean srb = new ServletListenerRegistrationBean();
srb.setListener(new MyListener());
System.out.println("listener");
return srb;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
上一篇: 优雅的进行封装实体的参数校验
下一篇: 谈谈自己对于 AOP 的了解
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论