![](https://img-blog.csdnimg.cn/20191227165540122.jpg)
ShopAdminInterceptor (自定义拦截器类1)
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ShopAdminInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//获取用户登陆信息
Person person = (Person) request.getSession().getAttribute("person");
//判断用户是否有权限进入商家管理后台系统
if(person != null && person.getUserId() > 0
&& person.getEnableStatus() == 1 && person.getPersonType() == 2){
//如果验证通过,则返回true,放行请求,即用户接下来的操作可以正常执行
return true;
}
//如果不满足登陆验证,则跳转到登陆页面
response.sendRedirect("/o2o/local/to_login");
return false;
}
}
SuperAdminInterceptor (自定义拦截器类2)
import com.cd.o2o2.entity.Person;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SuperAdminInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//获取用户登陆信息
Person person = (Person) request.getSession().getAttribute("person");
//判断用户是否有权限进入超级管理员后台系统
if(person != null && person.getUserId() > 0
&& person.getEnableStatus() == 1 && person.getPersonType() == 3){
//如果验证通过,则返回true,放行请求,即用户接下来的操作可以正常执行
return true;
}
//如果不满足登陆验证,则跳转到登陆页面
response.sendRedirect("/o2o/local/to_login");
return false;
}
}
![](https://img-blog.csdnimg.cn/20191227172607417.jpg)
使用SSM开发web应用时,配置拦截器的方式:
<!--拦截器链-->
<mvc:interceptors>
<!--拦截器1,对商家管理系统进行权限验证-->
<mvc:interceptor>
<!--指定拦截的请求-->
<mvc:mapping path="/shop_admin/**"/>
<!--指定使用的自定义拦截器类-->
<bean class="com.cd.o2o.interceptor.ShopAdminInterceptor"/>
</mvc:interceptor>
<!--拦截器2,对超级管理员系统进行权限验证-->
<mvc:interceptor>
<!--指定拦截的请求-->
<mvc:mapping path="/super_admin/**"/>
<!--指定不拦截的请求-->
<mvc:exclude-mapping path="/super/toLogin"/>
<!--指定使用的自定义拦截器类-->
<bean class="com.cd.o2o.interceptor.SuperAdminInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
![](https://img-blog.csdnimg.cn/20191228151633121.jpg)
spring boot抛弃了复杂的xml配置,我们可以自定义配置类(标注@Configuration注解的类)来实现WebMvcConfigurer接口,并重写addInterceptors()方法来配置拦截器:
import com.cd.o2o2.interceptor.ShopAdminInterceptor;
import com.cd.o2o2.interceptor.SuperAdminInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyWebAppConfiguration implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
//注册拦截器1,对商家管理系统进行权限验证
InterceptorRegistration registration1 = registry.addInterceptor(new ShopAdminInterceptor());
//指定拦截器1要拦截的请求(支持*通配符)
registration1.addPathPatterns("/shop_admin/**");
//注册拦截器2,对超级管理员系统进行权限验证
InterceptorRegistration registration2 = registry.addInterceptor(new SuperAdminInterceptor());
/*指定拦截器2要拦截的请求(支持*通配符)*/
registration2.addPathPatterns("/super_admin/**");
//指定拦截器2不拦截的请求(支持*通配符)
registration2.excludePathPatterns("/super/toLogin");
}
}
![](https://img-blog.csdnimg.cn/20191228172153343.jpg)
以java形式定制MVC配置时,实现WebMvcConfigurer接口即可,不要在@Configuration class上标注@EnableWebMvc (因为@EnableWebMvc是默认没有静态资源放行的,即.css .jpg .js等文件默认会被DispatcherServlet以/形式拦截)
@Configuration
@EnableWebMvc
public class MyWebAppConfiguration implements WebMvcConfigurer{
}
否则所有的静态资源css,js等都会被拦截