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

到此這篇關(guān)于Springboot重寫addInterceptors()方法配置攔截器實(shí)例的文章就介紹到這了,更多相關(guān)重寫addInterceptors()配置攔截器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java時(shí)間段查詢將00:00:00更換成23:59:59
本文主要介紹了java時(shí)間段查詢將00:00:00更換成23:59:59,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
SpringBoot深入理解之內(nèi)置web容器及配置的總結(jié)
今天小編就為大家分享一篇關(guān)于SpringBoot深入理解之內(nèi)置web容器及配置的總結(jié),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
java實(shí)現(xiàn)文件導(dǎo)入導(dǎo)出
這篇文章主要介紹了java實(shí)現(xiàn)文件導(dǎo)入導(dǎo)出的方法和具體示例代碼,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下2016-04-04
Maven項(xiàng)目引用第三方j(luò)ar包找不到類ClassNotFoundException
這篇文章主要為大家介紹了Maven項(xiàng)目引用第三方j(luò)ar包找不到類ClassNotFoundException解決及原因分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Java編程刪除鏈表中重復(fù)的節(jié)點(diǎn)問題解決思路及源碼分享
這篇文章主要介紹了Java編程刪除鏈表中重復(fù)的節(jié)點(diǎn)問題解決思路及源碼分享,具有一定參考價(jià)值,這里分享給大家,供需要的朋友了解。2017-10-10
微信js sdk invalid signature簽名錯(cuò)誤問題的解決方法分析
這篇文章主要介紹了微信js sdk invalid signature簽名錯(cuò)誤問題的解決方法,結(jié)合實(shí)例形式分析了微信簽名錯(cuò)誤問題相關(guān)解決方法,需要的朋友可以參考下2019-04-04

