簡體   English   中英

在Spring Security中,如何查找會話是否已因多次登錄而無效?

[英]In spring security, how to find if the session has been invalidated by multiple logins?

當用戶使用相同的用戶名從另一台計算機登錄而沒有從第一台計算機注銷時,默認行為是第一個會話無效。 現在,當用戶返回第一台計算機時 ,我想告訴他,由於他已經從其他計算機登錄,因此他的會話已失效。 我怎樣才能做到這一點 ?

我想到了兩種方法:

  • 允許多次登錄,並且當用戶從第二台計算機登錄時,向所有會話(其ID是從sessionRegistry獲得的)添加一個布爾標志,並且當用戶從第一台計算機返回登錄時,請檢查當前會話布爾標志為true。 如果是,則使會話無效,並向用戶發送消息。 這將在CustomSuccessHandler完成。

缺點 :可能無法通過會話ID(這是會話注冊表提供的所有功能)獲取會話對象

  • 不允許多次登錄,並且當用戶返回其第一台計算機時,會以某種方式找出導致會話無效的原因。 如果由於多次登錄而使其無效,則顯示正確的消息。

Flipside :在使會話無效時似乎無法添加無效原因,並且我不知道如何從第一台計算機訪問此信息(如果存在)

不允許多次登錄。 您可以使用Spring Security輕松實現這一目標。

concurrent-session-control添加到Spring安全性。

<concurrent-session-control max-sessions="1" exception-if-maximum-exceeded="false" expired-url="/jsp/invalidate.do"/> 
//you can also set expired-url to your custom invalidate page rather than create a mapping

為無效用戶創建自定義requestMapping。

@RequestMapping( value = "/invalidate.do", method = RequestMethod.GET )
    public String invalidate(HttpServletRequest request)
    {
       request.setAttribute("invalidate",true);
       return "login";
    }

然后你的登錄頁面

<c:if test="${not empty invalidate}">
<script type="text/javascript">
    alert("You have been Logged Out. Someone signed in using your account.");
</script>
</script>

另一種方法是將您的error-if-maximum-exceedtrue ,因此如果用戶嘗試登錄另一個會話,它將標記錯誤。

<security:session-management>
            <security:concurrency-control error-if-maximum-exceeded="true" max-sessions="1"/>
</security:session-management>

然后在您的message.properties中創建自定義消息

ConcurrentSessionControlStrategy.exceededAllowed=You have been Logged Out. Someone signed in using your account.

希望能幫助到你。

鏈接: http : //codehustler.org/blog/spring-security-tutorial-form-login/ Spring Security中的最大並發用戶數

暫無
暫無

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

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