春季安全:自定义身份验证提供程序
我已经开发了一个应用程序与春季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实施会给我更快的输出?
正如Afridi指出的那样,您的第一个版本正是DaoAuthenticationProvider应该做的。我强烈建议不要重新实现其功能,因为您可能会引入新的安全相关错误。
如果您确实需要自定义身份验证方法,当然无法使用自定义身份验证方法。为了一般地衡量这个实现的性能或者与标准实现相比较,你应该简单地定义一个测试场景(例如,建议使用20040个虚拟身份验证),然后在分析器中运行该程序。这将如何确切地花费多少时间在您的身份验证方法上,甚至哪个部分花费最多的时间。
我认为最流行的Java剖析器是VisualVM,并且根据您的IDE,可能会有一个插件进一步简化了它的使用。还有很多关于Java分析的教程,但这是您获得可靠数据以获得性能的最佳途径。
感谢您的答复。在应用程序中有几个条件的帐户禁用,并需要显示不同的消息取决于条件。那我该如何实现它? –
所以你的问题是为你的认证编写一个自动性能测试? – stefanhgm
Spring已经在第1版中执行了相同的操作(在DaoAuthenticationProvider中)。所以不需要customAuthentication提供程序。 – Afridi
我必须在登录时检查更多条件。但是DaoAuthenticationProvider有一些特定的条件。那就是为什么我需要实现自定义身份验证提供程序@Afridi –
最快的?用20000次迭代计时测试,你会看到。 – holmis83