<repo_name>liutao1314/springboot-mybatis-demo-master<file_sep>/src/main/java/com/winter/interceptors/MyInterceptors.java
package com.winter.interceptors;

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;

/**
 * Created by liutao on 2018/4/20.
 */
public class MyInterceptors implements HandlerInterceptor {
    public static final Logger LOGGER = LoggerFactory.getLogger(MyInterceptors.class);

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        LOGGER.info("拦截器自定义");
        String uri = request.getRequestURI();
        LOGGER.info("请求URI:{}", uri);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }
}
<file_sep>/src/main/java/com/winter/mq/MessageProcessorImpl.java
package com.winter.mq;

import com.alibaba.rocketmq.common.message.MessageExt;
import org.springframework.stereotype.Component;

/**
 * Created by liutao on 2018/4/20.
 */
@Component
public class MessageProcessorImpl implements MessageProcessor {
    @Override
    public boolean handleMessage(MessageExt messageExt) {
        System.out.println("receive : " + messageExt.getBody().toString());
        return true;
    }
}
<file_sep>/src/main/java/com/winter/listener/SessionListener.java
package com.winter.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * 实现HttpSessionListener接口，监视session的动态
 * @author Administrator
 *
 */
@WebListener
public class SessionListener implements HttpSessionListener{
    public static final Logger LOGGER = LoggerFactory.getLogger(SessionListener.class);

    @Override
    public void sessionCreated(HttpSessionEvent session) {
        LOGGER.info("session创建成功......");

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent session) {
        LOGGER.info("session销毁......");

    }
}
<file_sep>/src/main/java/com/winter/filters/CustomFilter.java
package com.winter.filters;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 实现javax.servlet.Filter,覆盖其三个方法
 * @author Administrator
 *
 */
@WebFilter(filterName = "CustomFilter", urlPatterns = "/*")
public class CustomFilter implements Filter{
    public static final Logger LOGGER = LoggerFactory.getLogger(CustomFilter.class);

    /**
     * 封装，不需要过滤的list列表
     */
    protected static List<Pattern> patterns = new ArrayList<Pattern>();

    @Override
    public void destroy() {
        LOGGER.info("CustomFilter过滤器销毁");

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        LOGGER.info("CustomFilter指定过滤器操作......");
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        String url = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length());
        if (url.startsWith("/") && url.length() > 1) {
            url = url.substring(1);
        }

        if (isInclude(url)){
            chain.doFilter(httpRequest, httpResponse);
            return;
        } else {
            HttpSession session = httpRequest.getSession();
            if (session.getAttribute("") != null){
                // session存在
                chain.doFilter(httpRequest, httpResponse);
                return;
            } else {
                // session不存在 准备跳转失败
                /* RequestDispatcher dispatcher = request.getRequestDispatcher(path);
                    dispatcher.forward(request, response);*/
                chain.doFilter(httpRequest, httpResponse);
                return;
            }
        }
    }

    /**
     * 是否需要过滤
     * @param url
     * @return
     */
    private boolean isInclude(String url) {
        for (Pattern pattern : patterns) {
            Matcher matcher = pattern.matcher(url);
            if (matcher.matches()) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        LOGGER.info("CustomFilter初始化......");
    }
}
