1. 程式人生 > >Spring Security應用開發(10) 並發控制之基本介紹

Spring Security應用開發(10) 並發控制之基本介紹

authorize 失效 session report 表示 container 頁面 屬性 ren

同一個用戶使用不同的瀏覽器登錄,將會導致什麽結果呢?Spring Security提供了多種選項。

<!-- session管理 -->

 <sec:session-management

  session-fixation-protection="changeSessionId"

  session-authentication-error-url="/login/session_error.action?id=max_session_error"

  invalid-session-url="/login/session_error.action?id=invalid_session_error"
> <sec:concurrency-control max-sessions="1" expired-url="/login/session_error.action?id=session_expired_error" error-if-maximum-exceeded="false" /> </sec:session-management>

session-management配置的最終效果受到session-fixation-protection屬性和max-sessions屬性以及error-if-maximum-exceeded

屬性的共同影響。

(1)對於session-management標簽的session-fixation-protection屬性,Eclipse給出的API DOC信息如下:

Attribute : session-fixation-protection

Indicates how session fixation protection will be applied when a user authenticates. If set to

"none", no protection will be applied. "newSession" will create a new empty session, with only

Spring Security-related attributes migrated. "migrateSession" will create a new session and copy

all session attributes to the new session. In Servlet 3.1 (Java EE 7) and newer containers,

specifying "changeSessionId" will keep the existing session and use the container-supplied

session fixation protection (HttpServletRequest#changeSessionId()). Defaults to

"changeSessionId" in Servlet 3.1 and newer containers, "migrateSession" in older containers.

Throws an exception if "changeSessionId" is used in older containers.

session-fixation-protection屬性有4個可能的值用來控制用戶在登錄前後的session特征。

(a)none:不進行Session fixation固化保護。

(b)newSession:創建新的session,僅僅Spring Security相關的屬性會遷移到新的session

(c)migrateSession:創建新的session,所有屬性會遷移到新的session

(d)changeSessionId:保留登錄之前的session不變。

(2)max-sessions屬性控制同一個用戶的最多的session個數,當為1時,只能某個瀏覽器上處於已登錄狀態。當為-1時表示不受限制。

(3)error-if-maximum-exceeded屬性控制是否在用戶的已登錄session的個數超出max-sessions時報告錯誤。

true:該用戶已登錄的session個數超出時報告錯誤導致登錄失敗,跳轉到

session-authentication-error-url指定URL

false:超出時不報告錯誤,但是會導致之前已經登錄的session失效過期,之前已經登錄的session在做任何頁面跳轉操作時會跳轉到expired-url指定的URL

API DOC原文如下:

Specifies that an unauthorized error should be reported when a user attempts to login when they

already have the maximum configured sessions open. The default behaviour is to expire the original session. If the session-authentication-error-url attribute is set on the session-management URL, the user will be redirected to this URL

(4)invalid-session-url

在設置了此屬性的情況下,在用戶先前的登錄session被後面的登錄session擠掉之後,先前的session無效了。此時在先前登錄的瀏覽器中進行頁面跳轉操作時,將會跳轉到invalid-session-url指定的URL

(5)session-authentication-error-url。在error-if-maximum-exceededtrue時,如果session個數超出限制則使用此URL來報告錯誤。但是,如果form-login結點配置了authentication-failure-url這個屬性,則會以form-login結點的配置為準。

(6)expired-url

用於控制在登錄session被後來的登錄session擠掉後,再在先登錄session中進行頁面跳轉時,將跳轉到expired-url指定的URL

使用過程中發現如下現象:

在設置了invalid-session-url時,沒發現跳轉到此URL的情況,在再次登錄時都是跳轉到了invalid-session-url。在沒有設置invalid-session-url時,才跳轉到expired-url

API DOC

The URL a user will be redirected to if they attempt to use a session which has been "expired"

because they have logged in again.

Spring Security應用開發(10) 並發控制之基本介紹