Spring Security学习异常处理

  • 认证异常
    匿名用户在认证过程中 所涉及的异常行为有很多,常见的有:

    1. 账号/密码错误 (用户不存在)
    2. 账号状态异常 (被锁定、过期)
    3. 凭证无效/过期

    Spring security 统一以AuthenticationException抛出. 由AuthenticationEntryPoint处理

Spring security 官方文档
8.2.1. AuthenticationEntryPoint
AuthenticationEntryPoint会被调用, 在用户请求一个安全HTTP资源,但是他们还没有被认证。一个对应的 AuthenticationException或 AccessDeniedException会被抛出, 由一个安全监听器在下面的调用栈中,触发入口点的 commence方法。这执行展示对应的响应给用户, 这样认证可以开始。我们这里使用的是 LoginUrlAuthenticationEntryPoint,它会把请求 重定向到另一个不同的URL(一般是一个登陆页面)。实际的使用使用会依赖 你希望使用到的认证机制。

异常 异常类 描述
账号状态异常 AccountStatusException
账号锁定状态 LockedException
账号禁用状态 DisabledException
账户过期状态 AccountExpiredException
证书过期状态 CredentialsExpiredException
凭证无效 BadCredentialsException
用户不存在 UsernameNotFoundException
认证信息不足 InsufficientAuthenticationException
需要AccessToken AccessTokenRequiredException
UnapprovedClientAuthenticationException
认证服务端异常 AuthenticationServiceException
InternalAuthenticationServiceException
记住我错误 RememberMeAuthenticationException
CookieTheftException
Cookie无效 InvalidCookieException
其他 SessionAuthenticationException
PreAuthenticatedCredentialsNotFoundException
Provider未查找到 ProviderNotFoundException
AuthenticationCredentialsNotFoundException
Nonce已经过期/超时 NonceExpiredException
  • 鉴权异常
    认证用户访问无权限资源时的异常为AccessDeniedException, 可以自定义AccessDeniedHandler进行处理

你可能感兴趣的:(微服务)