Spring MVC中攔截器的繼承HandlerInterceptorAdapter類,並根據需求實現其中的preHandle方法(預處理)、postHandle方法(返回處理),afterCompletion方法(後處理)。java
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; } }
自定義的攔截器類web
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 { } }