springmvc+shiro自定義過濾器的實(shí)現(xiàn)代碼
實(shí)現(xiàn)需求:
1.用戶未登錄,跳轉(zhuǎn)到登錄頁,登錄完成后會跳到初始訪問頁。
2.用戶自定義處理(如需要激活),跳轉(zhuǎn)到激活頁面,激活完成后會跳到初始訪問頁。
使用到的框架
springmvc 的攔截器
shiro 自定義過濾器
實(shí)現(xiàn):
1.編寫攔截器通過session保存初始訪問的頁面地址,便于后面回跳這個頁面做準(zhǔn)備。
import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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; /** * 用戶登錄以后跳轉(zhuǎn)回之前頁面的攔截器 攔截對象: 除登錄,注冊之外的所有跳轉(zhuǎn)頁面的請求 因?yàn)橛脩綦S時可能進(jìn)行登錄操作 * * @version 1.0.0 * @date 2018 -10-19 */ public class ForwardBeforeUrlInteceptor implements HandlerInterceptor { private Logger logger = LoggerFactory.getLogger(this.getClass()); @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { } @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception { // 過濾掉ajax請求 if (request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) { return true; } // 獲取當(dāng)前會話 HttpSession session = request.getSession(true); // 拿到上一個頁面地址 String uri = request.getRequestURI(); // 去掉項(xiàng)目地址長度的字符(因?yàn)槲业哪J(rèn)項(xiàng)目地址是給出的) String path = uri.substring(request.getContextPath().length()); // 得到參數(shù) String query = request.getQueryString(); if (query == null) { query = ""; } else { query = "?" + query; } String beforePath = path + query; session.setAttribute("beforePath", beforePath); session.setAttribute("method", request.getMethod()); logger.debug("beforePath :{}, method:{}", beforePath, request.getMethod()); return true; } }
2.在spring的xml配置文件中配置攔截器,例如application.xml
<mvc:interceptors> <!-- 使用bean定義一個Interceptor,直接定義在mvc:interceptors根下面的Interceptor將攔截所有的請求 --> <!-- 配置用于跳回登錄之前的頁面的攔截器--> <mvc:interceptor> <!-- 進(jìn)行攔截:/**表示攔截所有url及其子路徑 --> <mvc:mapping path="/**" /> <!-- ajax請求的action不進(jìn)行攔截 --> <mvc:exclude-mapping path="/*.ajax" /> <mvc:exclude-mapping path="/resources/**" /> <mvc:exclude-mapping path="/activation" /> <bean class="com.xxx.xxx.xxx.interceptor.ForwardBeforeUrlInteceptor" /> </mvc:interceptor> </mvc:interceptors>
注意:<mvc:exclude-mapping path="/activation" />
此處是界面可以直接進(jìn)入激活頁面,此處是排除攔截激活頁面,防止頁面出現(xiàn)不停的回跳到自己頁面。
3.自定義過濾器。
import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * 課程攔截器,當(dāng)會員過期或未激活時自動跳轉(zhuǎn)到激活頁面 * * @version 1.0.0 * @date 2018 -10-19 */ public class MemberFilter extends com.bwjf.framework.shiro.filter.UserFilter { @Override protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) { HttpServletResponse httpServletResponse = (HttpServletResponse) response; HttpServletRequest httpServletRequest = (HttpServletRequest) request; if (httpServletRequest.getRequestURI().indexOf("activation") > 0) { return true; } MyShiroUser myShiroUser = MyUserUtil.getCurrentShiroUser(); if (!CheckEmptyUtil.isEmpty(myShiroUser) && CheckEmptyUtil.isEmpty(myShiroUser.getActiveDate())) { try { // 瀏覽器跳轉(zhuǎn)到激活頁面 httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/activation"); } catch (IOException e) { e.printStackTrace(); } } return true; } }
4.shiro.xml配置自定義過濾器
5.controller激活處理后跳轉(zhuǎn)到初始頁面
總結(jié)
以上所述是小編給大家介紹的springmvc+shiro自定義過濾器的實(shí)現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring Bean生命周期之Bean元信息的配置與解析階段詳解
這篇文章主要為大家詳細(xì)介紹了Spring Bean生命周期之Bean元信息的配置與解析階段,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03Springboot重寫addInterceptors()方法配置攔截器實(shí)例
這篇文章主要介紹了Springboot重寫addInterceptors()方法配置攔截器實(shí)例,spring?boot拋棄了復(fù)雜的xml配置,我們可以自定義配置類(標(biāo)注@Configuration注解的類)來實(shí)現(xiàn)WebMvcConfigurer接口,并重寫addInterceptors()方法來配置攔截器,需要的朋友可以參考下2023-09-09深入學(xué)習(xí)Java同步機(jī)制中的底層實(shí)現(xiàn)
在多線程編程中我們會遇到很多需要使用線程同步機(jī)制去解決的并發(fā)問題,這些同步機(jī)制是如何實(shí)現(xiàn)的呢?下面和小編來一起學(xué)習(xí)吧2019-05-05解決SpringBoot連接SqlServer出現(xiàn)的問題
在嘗試通過SSL與SQL?Server建立安全連接時,如果遇到“PKIX?path?building?failed”錯誤,可能是因?yàn)槲茨苷_配置或信任服務(wù)器證書,當(dāng)"Encrypt"屬性設(shè)置為"true"且"trustServerCertificate"屬性設(shè)置為"false"時,要求驅(qū)動程序使用安全套接字層(SSL)加密與SQL?Server建立連接2024-10-10