【发布时间】:2017-08-24 06:07:24
【问题描述】:
我使用 spring mvc 开发了一个应用程序,用于高用户流量。假设至少有 20,000 个并发用户。我已经通过两种方式实现了 Spring Security 自定义身份验证提供程序。
第一个是:
@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;
}
}
现在我的问题是哪个实现会给我更快的输出?
【问题讨论】:
-
Spring 已经在做同样的事情(在 DaoAuthenticationProvider 中)你已经在第一个版本中实现了。所以不需要 customAuthentication 提供者。
-
我必须在登录时检查更多条件。但是 DaoAuthenticationProvider 有一些特定的条件。这就是为什么我需要实现自定义身份验证提供程序。 @Afridi
-
最快的?用 20000 次迭代为测试计时,你会看到。
标签: spring-mvc spring-security custom-authentication