簡體   English   中英

在Spring MVC過濾器或攔截器中處理會話管理的最佳方法

[英]Best way to handle session management in spring mvc filters or interceptors

需求:

在執行每個動作之前,我必須檢查會話是否有效。 如果會話有效,請檢查會話中用戶登錄詳細信息的可用性,否則應在會話中設置這些詳細信息。 如果會話無效,則應將其重定向到登錄頁面。

可以讓我知道哪個適合我的要求。

您還可以在調度程序Servlet中配置自定義interceptor

<mvc:interceptors>
    <bean class="com.CusInterceptor"/>
</mvc:interceptors>

並擴展HandlerInterceptorAdapter類,並重寫preHandle()方法以攔截所有請求。

public class CusInterceptorextends HandlerInterceptorAdapter {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

    // verify user session here and process it for further operations  
    }

為此使用Spring Security 它將在會話中存儲一個安全上下文對象,其中包含所有憑據等等。 安全篩選器鏈將確保此對象底線有效,或者將用戶重定向到登錄頁面。

在那一個上與@Stefan達成一致。 與Spring Security一起使用(我們在mvc應用程序中將其用於會話身份驗證/驗證)。 但是請記住,在使Ajax調用重定向到登錄頁面之前,需要進行一些調整。否則,您將收到錯誤頁面響應,您可能會或可能不會在JS(客戶端)級別上進行解析/處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM