欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springmvc+shiro自定義過濾器的實(shí)現(xiàn)代碼

 更新時間:2018年10月29日 10:38:34   作者:itget  
這篇文章主要介紹了springmvc+shiro自定義過濾器的實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下

實(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元信息的配置與解析階段詳解

    Spring Bean生命周期之Bean元信息的配置與解析階段詳解

    這篇文章主要為大家詳細(xì)介紹了Spring Bean生命周期之Bean元信息的配置與解析階段,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • java對象轉(zhuǎn)型實(shí)例分析

    java對象轉(zhuǎn)型實(shí)例分析

    這篇文章主要介紹了java對象轉(zhuǎn)型的概念及用法,并以實(shí)例形式進(jìn)行了較為詳細(xì)的介紹,需要的朋友可以參考下
    2014-10-10
  • Spring集成Web環(huán)境的實(shí)例詳解

    Spring集成Web環(huán)境的實(shí)例詳解

    這篇文章主要介紹了Spring集成Web環(huán)境,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • Spring源碼解析容器初始化構(gòu)造方法

    Spring源碼解析容器初始化構(gòu)造方法

    這篇文章主要介紹了Spring源碼解析容器初始化構(gòu)造方法,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07
  • Springboot重寫addInterceptors()方法配置攔截器實(shí)例

    Springboot重寫addInterceptors()方法配置攔截器實(shí)例

    這篇文章主要介紹了Springboot重寫addInterceptors()方法配置攔截器實(shí)例,spring?boot拋棄了復(fù)雜的xml配置,我們可以自定義配置類(標(biāo)注@Configuration注解的類)來實(shí)現(xiàn)WebMvcConfigurer接口,并重寫addInterceptors()方法來配置攔截器,需要的朋友可以參考下
    2023-09-09
  • Springboot配置返回日期格式化五種方法詳解

    Springboot配置返回日期格式化五種方法詳解

    本文主要介紹了Springboot配置返回日期格式化五種方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 深入學(xué)習(xí)Java同步機(jī)制中的底層實(shí)現(xiàn)

    深入學(xué)習(xí)Java同步機(jī)制中的底層實(shí)現(xiàn)

    在多線程編程中我們會遇到很多需要使用線程同步機(jī)制去解決的并發(fā)問題,這些同步機(jī)制是如何實(shí)現(xiàn)的呢?下面和小編來一起學(xué)習(xí)吧
    2019-05-05
  • Mybatisplus如何存儲List、Map

    Mybatisplus如何存儲List、Map

    這篇文章主要介紹了Mybatisplus如何存儲List、Map問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 解決SpringBoot連接SqlServer出現(xiàn)的問題

    解決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
  • 詳解java中BigDecimal精度問題

    詳解java中BigDecimal精度問題

    這篇文章主要介紹了java BigDecimal精度問題,對精確計(jì)算感興趣的同學(xué),可以參考下
    2021-05-05

最新評論