springmvc+shiro自定義過濾器的實(shí)現(xiàn)代碼
實(shí)現(xiàn)需求:
1.用戶未登錄,跳轉(zhuǎn)到登錄頁(yè),登錄完成后會(huì)跳到初始訪問頁(yè)。
2.用戶自定義處理(如需要激活),跳轉(zhuǎn)到激活頁(yè)面,激活完成后會(huì)跳到初始訪問頁(yè)。
使用到的框架
springmvc 的攔截器
shiro 自定義過濾器
實(shí)現(xiàn):
1.編寫攔截器通過session保存初始訪問的頁(yè)面地址,便于后面回跳這個(gè)頁(yè)面做準(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)回之前頁(yè)面的攔截器 攔截對(duì)象: 除登錄,注冊(cè)之外的所有跳轉(zhuǎn)頁(yè)面的請(qǐng)求 因?yàn)橛脩綦S時(shí)可能進(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請(qǐng)求
if (request.getHeader("x-requested-with") != null
&& request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
return true;
}
// 獲取當(dāng)前會(huì)話
HttpSession session = request.getSession(true);
// 拿到上一個(gè)頁(yè)面地址
String uri = request.getRequestURI();
// 去掉項(xiàng)目地址長(zhǎ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定義一個(gè)Interceptor,直接定義在mvc:interceptors根下面的Interceptor將攔截所有的請(qǐng)求 -->
<!-- 配置用于跳回登錄之前的頁(yè)面的攔截器-->
<mvc:interceptor>
<!-- 進(jìn)行攔截:/**表示攔截所有url及其子路徑 -->
<mvc:mapping path="/**" />
<!-- ajax請(qǐng)求的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)入激活頁(yè)面,此處是排除攔截激活頁(yè)面,防止頁(yè)面出現(xiàn)不停的回跳到自己頁(yè)面。
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)會(huì)員過期或未激活時(shí)自動(dòng)跳轉(zhuǎn)到激活頁(yè)面
*
* @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)到激活頁(yè)面
httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/activation");
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
}
4.shiro.xml配置自定義過濾器

5.controller激活處理后跳轉(zhuǎn)到初始頁(yè)面

總結(jié)
以上所述是小編給大家介紹的springmvc+shiro自定義過濾器的實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
SprinBoot如何集成參數(shù)校驗(yàn)Validator及參數(shù)校驗(yàn)的高階技巧
這篇文章主要介紹了SprinBoot如何集成參數(shù)校驗(yàn)Validator及參數(shù)校驗(yàn)的高階技巧包括自定義校驗(yàn)、分組校驗(yàn),本文分步驟給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
springboot2.x整合redis知識(shí)點(diǎn)講解
在本篇文章中小編給大家分享的是一篇關(guān)于springboot2.x整合redis知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-01-01
SpringBoot實(shí)現(xiàn)elasticsearch 查詢操作(RestHighLevelClient 
這篇文章主要給大家介紹了SpringBoot如何實(shí)現(xiàn)elasticsearch 查詢操作,文中有詳細(xì)的代碼示例和操作流程,具有一定的參考價(jià)值,需要的朋友可以參考下2023-07-07
springboot對(duì)象為null的屬性在json中不顯示的解決
這篇文章主要介紹了springboot對(duì)象為null的屬性在json中不顯示的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09

