2017-02-03 95 views
1

我的Grails應用程序(使用Spring Security插件提供)具有非常基本的支持'快速登錄'功能,基本上提示用戶只鍵入他們的密碼登錄,而userId存儲在加密的cookie。Grails 3 - springSecurity重新驗證和SessionRegistry

因此,在我的控制器中,我使用 springSecurityService.reauthenticate(userid)
手動驗證用戶。

我的應用程序也有要求不允許併發會話。因此,與前行以來,我加入

def authentication = SecurityContextHolder.context.authentication 
def sessions = sessionRegistry.getAllSessions(authentication.getPrincipal(), false) 
// [L] 
sessions.each { 
    it.expireNow() 
} 

的問題是,這些reauthentications不要在sessionRegistry條目反映,而這與上面的代碼的問題,因爲其中sessions我不會找任何該用戶的會話。

我不是目前能夠解決的問題是:

  • 用戶[U]登錄到位置[A]
  • [U]登錄到位置[B] =>會話[ A]被迫到期
  • [U]上導航/刷新[A]持續,並提示快速登錄
  • [U]再次登錄到[A] =>reauthenticate被觸發,沒有被添加到sessionRegistry
  • [U]繼續瀏覽廷/刷新[B],並且被提示快速登錄
  • [U]再次登錄到[B]
  • [U]被記錄在兩個[A]和[B]

我也試圖添加
sessionRegistry.registerNewSession(newSessionId, authentication.getPrincipal())
[L]的位置,但是這個會話和重新生成的會話似乎沒有任何關係。

歡迎任何建議!提前致謝。

配置

  • 的Grails 3.2.4
  • 春季安全插件(核心)3.1.1

回答

0

我想出了一個可行的解決方案

// injected dependencies 
AuthenticationManager authenticationManager 
SessionAuthenticationStrategy sessionAuthenticationStrategy 

// code 
Authentication auth = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(user.email, params.password)) 
SecurityContextHolder.context.authentication = authResult // need to reassign to context, otherwise onAuthentication fails (wrong password) 
sessionAuthenticationStrategy.onAuthentication(authResult, request, response) 
嘗試幾次後

這個詭計似乎在呼喚onAuthentication,觸發所有定義的請求過濾器,依賴於我的併發會話管理。