Spring Security 的 AuthenticationException实现总结

引言

在Spring Security中,认证失败时会抛出 AuthenticationException异常,而 AuthenticationException 有些子类我们需要了解和记忆一下以便我们更好的使用。

需要注意的是 AuthenticationException 是运行时异常。

AuthenticationException子实现

  • UsernameNotFoundException:用户名未找到。
  • BadCredentialsException:认证错误。可以用于密码错误、验证码错误等。
  • RememberMeAuthenticationException:记住我 认证错误。 在登录中常常有 记住我 选项,因为这个地方导致的错误。
  • AccountStatusException:用户账户状态异常。例如被封号之类的。
  • NonceExpiredException:Nonce过期异常。
  • AuthenticationCredentialsNotFoundException:SecurityContext中不存在Authentication异常。
  • AuthenticationServiceException:认证服务异常。可能是后端数据库之类的出现问题等等情况。
  • ProviderNotFoundException:Provider找不到异常。由ProviderManager抛出。
  • SessionAuthenticationException:会话认证异常。通常是因为同一个用户打开了多个会话。
  • InsufficientAuthenticationException:认证信息不足异常?可能是由于 记住我 选项引发的。

重点

最常用的应该是这两种:
UsernameNotFoundException:用户名未找到。
BadCredentialsException:认证错误。可以用于密码错误、验证码错误等。

其他的可能用的比较少,再自定义实现AuthenticationException即可。

你可能感兴趣的:(Spring Security 的 AuthenticationException实现总结)