为什么登录总是失败,Spring Security的详细错误提示在哪里?

2026-04-01 23:061阅读0评论SEO资源
  • 内容介绍
  • 相关推荐

本文共计120个文字,预计阅读时间需要1分钟。

为什么登录总是失败,Spring Security的详细错误提示在哪里?

在Spring Security默认情况下,不管用户名是否存在或密码是否错误,SS都会报Bad Credentials异常。

为什么登录总是失败,Spring Security的详细错误提示在哪里?

在springsecurity中默认情况下不管你是用户名不存在密码错误SS都会报出Badcredentials异常信 在spring security中默认情况下不管你是用户名不存在密码错误SS都会报出Bad credentials异常信息而不现实具体的错误,原因是在DaoAuthenticationProvider的父类AbstractUserDetailsAuthenticationProvider的authenticate方法中

try { user retrieveUser(username, (UsernamePasswordAuthenticationToken) authentication); } catch (UsernameNotFoundException notFound) { logger.debug("User " username " not found"); if (hideUserNotFoundExceptions) { throw new BadCredentialsException(messages.getMessage( "AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials")); } else { throw notFound; } } 这里有个hideUserNotFoundExceptions属性默认是true。这样的话即便我们抛出了UsernameNotFoundException它也会转为BadCredentialsException所以我们需要将hideUserNotFoundExceptions属性的值设为false 最后必须修改security的配置文件如下

而在前端则可以通过 ${sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message} 显示错误

本文共计120个文字,预计阅读时间需要1分钟。

为什么登录总是失败,Spring Security的详细错误提示在哪里?

在Spring Security默认情况下,不管用户名是否存在或密码是否错误,SS都会报Bad Credentials异常。

为什么登录总是失败,Spring Security的详细错误提示在哪里?

在springsecurity中默认情况下不管你是用户名不存在密码错误SS都会报出Badcredentials异常信 在spring security中默认情况下不管你是用户名不存在密码错误SS都会报出Bad credentials异常信息而不现实具体的错误,原因是在DaoAuthenticationProvider的父类AbstractUserDetailsAuthenticationProvider的authenticate方法中

try { user retrieveUser(username, (UsernamePasswordAuthenticationToken) authentication); } catch (UsernameNotFoundException notFound) { logger.debug("User " username " not found"); if (hideUserNotFoundExceptions) { throw new BadCredentialsException(messages.getMessage( "AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials")); } else { throw notFound; } } 这里有个hideUserNotFoundExceptions属性默认是true。这样的话即便我们抛出了UsernameNotFoundException它也会转为BadCredentialsException所以我们需要将hideUserNotFoundExceptions属性的值设为false 最后必须修改security的配置文件如下

而在前端则可以通过 ${sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message} 显示错误