Spring MVC中拦截器的继承HandlerInterceptorAdapter类,并根据需求实现其中的preHandle方法(预处理)、postHandle方法(返回处理),afterCompletion方法(后处理)。

package com.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


@Configuration
public class QihooWebAppConfigurer extends WebMvcConfigurerAdapter implements ApplicationContextAware {
    private ApplicationContext applicationContext;

    //拦截器 把自定义的拦截器类添加进来 AuthorityInterceptor
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AuthorityInterceptor()).addPathPatterns("/**").excludePathPatterns("/manage/user/user_login", "/login");
        super.addInterceptors(registry);
    }
    //配置静态访问资源
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/");
        registry.addResourceHandler("/templates/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/templates/");
        super.addResourceHandlers(registry);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;

    }
}

自定义的拦截器类

package com.interceptor;

import com.constant.SESSION_CONSTANT;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

public class AuthorityInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession(true);
        String user_account= (String) session.getAttribute(SESSION_CONSTANT.USER_ACCOUNT);
        if (user_account == null) {
            response.sendRedirect(request.getContextPath() + "/login");
            return false;
        }
        List<String> permission= (List<String>) session.getAttribute(SESSION_CONSTANT.USER_PERMISSION+user_account);
        if (permission == null) {
            response.sendRedirect(request.getContextPath() + "/login");
            return false;
        }else {
            String servletPath=request.getServletPath();
            String[] pp= servletPath.split("/");//"/xxement/xxation/index"
            if (pp.length>2 && pp[2].equals("store"))
                pp[2]="shop";
            if ((pp.length>2 && !permission.contains(pp[2])) &&  !"user".equals(pp[2])){
                System.out.println(pp[2]+" "+!permission.contains(pp[2])+ "  "+  !"user".equals(pp[2]));
                response.sendRedirect(request.getContextPath() + "/login");
                return false;
            }
        }
        return true;
    }

    @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 {

    }
}
03-20 19:11