2012-11-20 127 views
5

我開發了一個使用spring安全性的web應用程序。對於登錄,它從LDAP訪問。現在我想用彈簧安全本身管理會話,我可以通過使用authentication.getName()來獲得username,我也可以獲得sessionID使用Spring Security的會話管理:併發會話

現在我想確定,如果同一用戶試圖使用其他瀏覽器從同一系統登錄,他應該會收到一條消息,說他已經登錄了他的帳戶。

任何人都可以提供一個想法如何實現這?

<security:session-management 
     invalid-session-url="/login.jsp?error=sessionExpired" 
     session-authentication-error-url="/login.jsp?error=alreadyLogin"> 
    <security:concurrency-control 
       max-sessions="1" 
       expired-url="/login.jsp?error=sessionExpiredDuplicateLogin" 
       error-if-maximum-exceeded="false" /> 
</security:session-management> 

當我使用這一點,並嘗試使用一些其他的瀏覽器,它給了我下面的錯誤登錄:

HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalStateException: Cannot call sendError() after the response has been committed 
enter code here 

回答

6

我可能失去了一些東西,但我已盡力了一個配置,它可以作爲預計:

<!-- more configuration stuff --> 

<sec:form-login login-page="/login.jsp" 
    default-target-url="/defaultTarget.jsp" 
    authentication-failure-url="/login.jsp?error=true" 
    login-processing-url="/login" always-use-default-target="true" /> 

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

當我嘗試使用其他瀏覽器相同的用戶登錄,它需要我/login.jsp並顯示錯誤信息:Maximum sessions of 1 for this principal exceeded

編輯:你也需要把這個在您的web.xml

<listener> 
    <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class> 
</listener> 
+0

我有fsame配置你的建議。加上JdbcTokenRepositoryImpl。 所有這一切,我用一個瀏覽器登錄;然後我再次登錄另一個。看起來第二個登錄了,確實如此。 我的問題是,我clik從第一個瀏覽器的鏈接我得到以下異常: 'Estado HTTP 500 - PreparedStatementCallback;錯誤的SQL語法[從persistent_logins中刪除,其中username =?]; [blah blah]:表'gbt.persistent_logins'不存在'。 而表格實際上是從DB中消失的! 有沒有人知道發生了什麼? – kazbeel