Springboot重寫addInterceptors()方法配置攔截器實例
Springboot重寫addInterceptors()方法配置攔截器實例
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");
//判斷用戶是否有權(quán)限進入商家管理后臺系統(tǒng)
if(person != null && person.getUserId() > 0
&& person.getEnableStatus() == 1 && person.getPersonType() == 2){
//如果驗證通過,則返回true,放行請求,即用戶接下來的操作可以正常執(zhí)行
return true;
}
//如果不滿足登陸驗證,則跳轉(zhuǎn)到登陸頁面
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");
//判斷用戶是否有權(quán)限進入超級管理員后臺系統(tǒng)
if(person != null && person.getUserId() > 0
&& person.getEnableStatus() == 1 && person.getPersonType() == 3){
//如果驗證通過,則返回true,放行請求,即用戶接下來的操作可以正常執(zhí)行
return true;
}
//如果不滿足登陸驗證,則跳轉(zhuǎn)到登陸頁面
response.sendRedirect("/o2o/local/to_login");
return false;
}
}使用SSM開發(fā)web應(yīng)用時,配置攔截器的方式:
<!--攔截器鏈-->
<mvc:interceptors>
<!--攔截器1,對商家管理系統(tǒng)進行權(quán)限驗證-->
<mvc:interceptor>
<!--指定攔截的請求-->
<mvc:mapping path="/shop_admin/**"/>
<!--指定使用的自定義攔截器類-->
<bean class="com.cd.o2o.interceptor.ShopAdminInterceptor"/>
</mvc:interceptor>
<!--攔截器2,對超級管理員系統(tǒng)進行權(quán)限驗證-->
<mvc:interceptor>
<!--指定攔截的請求-->
<mvc:mapping path="/super_admin/**"/>
<!--指定不攔截的請求-->
<mvc:exclude-mapping path="/super/toLogin"/>
<!--指定使用的自定義攔截器類-->
<bean class="com.cd.o2o.interceptor.SuperAdminInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>spring boot拋棄了復(fù)雜的xml配置,我們可以自定義配置類(標注@Configuration注解的類)來實現(xiàn)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,對商家管理系統(tǒng)進行權(quán)限驗證
InterceptorRegistration registration1 = registry.addInterceptor(new ShopAdminInterceptor());
//指定攔截器1要攔截的請求(支持*通配符)
registration1.addPathPatterns("/shop_admin/**");
//注冊攔截器2,對超級管理員系統(tǒng)進行權(quán)限驗證
InterceptorRegistration registration2 = registry.addInterceptor(new SuperAdminInterceptor());
/*指定攔截器2要攔截的請求(支持*通配符)*/
registration2.addPathPatterns("/super_admin/**");
//指定攔截器2不攔截的請求(支持*通配符)
registration2.excludePathPatterns("/super/toLogin");
}
}以java形式定制MVC配置時,實現(xiàn)WebMvcConfigurer接口即可,不要在@Configuration class上標注@EnableWebMvc (因為@EnableWebMvc是默認沒有靜態(tài)資源放行的,即.css .jpg .js等文件默認會被DispatcherServlet以/形式攔截)
@Configuration
@EnableWebMvc
public class MyWebAppConfiguration implements WebMvcConfigurer{
}否則所有的靜態(tài)資源css,js等都會被攔截

到此這篇關(guān)于Springboot重寫addInterceptors()方法配置攔截器實例的文章就介紹到這了,更多相關(guān)重寫addInterceptors()配置攔截器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot深入理解之內(nèi)置web容器及配置的總結(jié)
今天小編就為大家分享一篇關(guān)于SpringBoot深入理解之內(nèi)置web容器及配置的總結(jié),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
Maven項目引用第三方j(luò)ar包找不到類ClassNotFoundException
這篇文章主要為大家介紹了Maven項目引用第三方j(luò)ar包找不到類ClassNotFoundException解決及原因分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
Java編程刪除鏈表中重復(fù)的節(jié)點問題解決思路及源碼分享
這篇文章主要介紹了Java編程刪除鏈表中重復(fù)的節(jié)點問題解決思路及源碼分享,具有一定參考價值,這里分享給大家,供需要的朋友了解。2017-10-10
微信js sdk invalid signature簽名錯誤問題的解決方法分析
這篇文章主要介紹了微信js sdk invalid signature簽名錯誤問題的解決方法,結(jié)合實例形式分析了微信簽名錯誤問題相關(guān)解決方法,需要的朋友可以參考下2019-04-04

