[英]Invalidate spring security session
我需要使用戶會話無效(或踢)。 應用程序僅限制用戶每個容器只登錄一個用戶。
我嘗試從會話注冊表調用removeSessionInformation,它完成解鎖用戶。 所以其他用戶可以使用踢腳會話用戶名登錄。
但是被踢的那個用戶的SessionContextHolder仍然是。 所以他們仍然擁有訪問受保護頁面的相同權限。
如何從指定的會話注冊表信息中失效或刪除SessionContextHolder的Principal?
ps:在我的舊應用程序中,我在UserDomain(UserDetails)中提供了一個包含HttpSession的變量。 當他們需要踢用戶時,我只是從指定的UserDomain中使HttpSession無效。 但是我不知道如何在春天這樣做(它更像是刪除了SessionContextHolder的Principal而不是HttpSession)。 SessionRegistryImpl在Spring中的實現幾乎與實現相同。
您可能想要考慮Spring Security Concurrency Control。 您可以將其配置為限制每個用戶的並發會話數,並在超過該數量時使現有會話到期(踢)。
這是我們配置的片段(Spring 3):
<http>
...
<session-management>
<concurrency-control max-sessions="1"/>
</session-management>
...
</http>
我猜這是做到這一點的方法:
SecurityContextHolder.getContext().setAuthentication(null)
來自SecurityContext.setAuthentication(Authentication)
Javadocs:
更改當前已驗證的主體,或刪除身份驗證信息。
參數: 身份驗證
- 新的身份驗證令牌,如果不存儲其他身份驗證信息,則為null
您還可以執行以下操作以清除SpringSecurity會話:
SecurityContextHolder.clearContext()
這是從我的application-security.xml中提取的
class="org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy"
p:maximumSessions="1" <br>
**p:exceptionIfMaximumExceeded="true"** >
<constructor-arg name="sessionRegistry" ref="sessionRegistry" />
嘗試在最大會話數后添加粗體字母
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.