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)文章
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知識點(diǎn)講解
在本篇文章中小編給大家分享的是一篇關(guān)于springboot2.x整合redis知識點(diǎn)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-01-01
SpringBoot實(shí)現(xiàn)elasticsearch 查詢操作(RestHighLevelClient 
這篇文章主要給大家介紹了SpringBoot如何實(shí)現(xiàn)elasticsearch 查詢操作,文中有詳細(xì)的代碼示例和操作流程,具有一定的參考價值,需要的朋友可以參考下2023-07-07
springboot對象為null的屬性在json中不顯示的解決
這篇文章主要介紹了springboot對象為null的屬性在json中不顯示的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09

