0

我已经为高用户流量开发了一个带有 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;
  }
}

现在我的问题是哪个实现会给我更快的输出?

4

1 回答 1

1

正如 Afridi 已经指出的那样,您的第一个版本正是 DaoAuthenticationProvider 应该做的。我强烈反对重新实现其功能,因为您可能会引入新的安全相关错误。

如果您确实需要自定义身份验证方法,那么当然无法绕过自定义身份验证方法。为了衡量这个实现的总体性能或与标准实现的比较,您应该简单地定义一个测试场景(例如 homlis83 建议的 20000 个虚拟身份验证)并在分析器中运行程序。这将告诉您在身份验证方法上花费了多少时间,甚至哪一部分花费的时间最多。

我认为最流行的 Java 分析器是 VisualVM,根据您的 IDE,可能有一个插件可以进一步简化其使用。那里也有很多关于 Java 分析的教程,但这绝对是您获得性能可靠数据的方法。

于 2017-08-24T08:15:03.837 回答