2017-08-24 148 views
0

我已經開發了一個應用程序與春季mvc高用戶流量。假設至少有20,000個併發用戶。我已經通過兩種方式實現了Spring安全定製認證提供程序。
1個是:春季安全:自定義身份驗證提供程序

@Override 
public Authentication authenticate(Authentication authentication) 
throws AuthenticationException { 

    String username = authentication.getName(); 
    String password = authentication.getCredentials().toString(); 
    CustomUser user = _userDetailService.loadUserByUsername(username); 
    if (user == null || !user.getUsername().equalsIgnoreCase(username)) { 
     throw new BadCredentialsException("Username not found."); 
    } 
    if (!BCrypt.checkpw(password, user.getPassword())) { 
     throw new BadCredentialsException("Wrong password."); 
    } 
    Collection < ? extends GrantedAuthority > authorities = user.getAuthorities(); 
    return new UsernamePasswordAuthenticationToken(user, password, authorities); 
} 

第二個是:

@Override 
public Authentication authenticate(Authentication authentication) 
throws AuthenticationException { 
    try { 
    Authentication auth = super.authenticate(authentication); 
    //if reach here, means login success, else an exception will be thrown 
    //reset the user_attempts 
    return auth; 

    } catch (BadCredentialsException e) { 
    //invalid login, update to user_attempts 
    throw e; 
    } 
} 

現在的問題是whice實施會給我更快的輸出?

+0

Spring已經在第1版中執行了相同的操作(在DaoAuthenticationProvider中)。所以不需要customAuthentication提供程序。 – Afridi

+0

我必須在登錄時檢查更多條件。但是DaoAuthenticationProvider有一些特定的條件。那就是爲什麼我需要實現自定義身份驗證提供程序@Afridi –

+0

最快的?用20000次迭代計時測試,你會看到。 – holmis83

回答

1

正如Afridi指出的那樣,您的第一個版本正是DaoAuthenticationProvider應該做的。我強烈建議不要重新實現其功能,因爲您可能會引入新的安全相關錯誤。

如果您確實需要自定義身份驗證方法,當然無法使用自定義身份驗證方法。爲了一般地衡量這個實現的性能或者與標準實現相比較,你應該簡單地定義一個測試場景(例如,建議使用20040個虛擬身份驗證),然後在分析器中運行該程序。這將如何確切地花費多少時間在您的身份驗證方法上,甚至哪個部分花費最多的時間。

我認爲最流行的Java剖析器是VisualVM,並且根據您的IDE,可能會有一個插件進一步簡化了它的使用。還有很多關於Java分析的教程,但這是您獲得可靠數據以獲得性能的最佳途徑。

+0

感謝您的答覆。在應用程序中有幾個條件的帳戶禁用,並需要顯示不同的消息取決於條件。那我該如何實現它? –

+0

所以你的問題是爲你的認證編寫一個自動性能測試? – stefanhgm

相關問題