使用若依框架的登录流程报错,输入的账号密码是对的,但是依然走错误的判断“账号或密码错误 ”

 在之前的若依版本中,没有登录时账号密码错误超过5次后会禁止登录10分钟,所以直接调用若依的 Authentication进行验证就可以,但是在 后来的若依版本 中更新了如果账号密码错误就会禁止登录10分钟后重试。所以在UserDetails中多了这么一行代码

 这行代码走到验证密码的时候就看不到后面的流程了,但是token为空,所以在前面的

 authentication = authenticationManager
                    .authenticate(new UsernamePasswordAuthenticationToken(userName, passWord));

authentication 为空,然后就会判断账号密码错误

要解决这个问题,有两个办法

第一,把 passwordService.validate(user);这行代码删除

第二,增加两行代码

UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userName, passWord);
            AuthenticationContextHolder.setContext(authenticationToken);

增加这两行代码会获取token,这样就不会报错

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: 使用Spring Security框架实现权限和登录验证时,可以使用AuthenticationException来判断账号密码是否错误。当账号密码错误时,Spring Security会抛出AuthenticationException异常。你可以在你的代码中使用try-catch语句来捕获这个异常,并根据具体的异常类型来判断账号错误还是密码错误。 例如: ``` try { authenticationManager.authenticate(authenticationToken); } catch (BadCredentialsException e) { // 密码错误 } catch (UsernameNotFoundException e) { // 账号错误 } ``` 还可以使用LdapAuthenticationProvider类来实现LDAP认证,并使用LdapAuthenticationException来判断账号密码是否错误。 例如: ``` try { ldapAuthenticationProvider.authenticate(authenticationToken); } catch (LdapAuthenticationException e) { if (e.getCause() instanceof InvalidCredentialsException) { // 密码错误 } else if (e.getCause() instanceof AuthenticationException) { // 账号错误 } } ``` ### 回答2: 在使用Spring Security框架实现权限和登录功能时,可以通过以下方式来判断账号还是密码错误: 1. 自定义认证失败处理器(AuthenticationFailureHandler):可以在该处理器中根据不同的认证失败原因进行处理。在认证失败时,可以获取到AuthenticationException的具体子类,如BadCredentialsException(密码错误)、UsernameNotFoundException(账号不存在)等。根据不同的异常类型,可以返回不同的错误提示信息或执行相应的操作。 示例代码: ```java public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler { @Override public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { if (exception instanceof BadCredentialsException) { // 密码错误处理逻辑 } else if (exception instanceof UsernameNotFoundException) { // 账号不存在处理逻辑 } else { // 其他错误处理逻辑 } } } ``` 2. 使用自定义的认证提供者(AuthenticationProvider):可以在自定义的认证提供者中根据账号密码验证进行详细判断。在用户登录时,通过重写`authenticate()`方法返回具体的认证结果,可以通过`Authentication`对象的isAuthenticated()方法判断是否认证成功,或通过getDetails()方法获取更详细的认证信息。 示例代码: ```java public class CustomAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String username = authentication.getName(); String password = authentication.getCredentials().toString(); // 调用自定义的认证逻辑判断账号密码是否正确 boolean isValid = customAuthenticationService.authenticate(username, password); if (isValid) { // 认证成功返回Authenticated认证结果 return new UsernamePasswordAuthenticationToken(username, password, new ArrayList<>()); } else { // 认证失败抛出对应的异常,在认证失败处理器中处理 throw new BadCredentialsException("密码错误"); } } @Override public boolean supports(Class<?> authentication) { return authentication.equals(UsernamePasswordAuthenticationToken.class); } } ``` 通过以上两种方式,可以根据不同的认证失败原因进行相应的处理,以判断账号还是密码错误,并返回相应的错误提示信息。 ### 回答3: 使用Spring Security框架实现权限和登录时,可以通过以下方式判断账号还是密码错误。 1. 配置登录页面 在Spring Security的配置文件中,可以指定登录页面的URL,并且可以配置登录表单提交的请求URL。通过配置这些信息,可以在登录表单提交时拦截请求并进行处理。 2. 自定义AuthenticationFailureHandler 可以自定义一个实现了AuthenticationFailureHandler接口的类,用于处理登录认证失败的情况。在该类中可以根据不同的认证失败原因进行不同的处理。 3. 处理认证异常 在自定义AuthenticationFailureHandler的实现类中,可以捕获到org.springframework.security.authentication.BadCredentialsException异常。该异常是在账号密码错误时抛出的。在捕获到此异常时,可以根据需要进行处理,例如记录日志、返回错误信息等。 以下是一个示例代码: ```java @Component public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler { @Override public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { if (exception instanceof BadCredentialsException) { // 账号密码错误 // 可以记录日志、返回错误信息等操作 } else { // 其他认证失败原因的处理 } // 可以根据需求进行跳转或返回JSON格式的错误信息 response.sendRedirect("/login?error"); } } ``` 在Spring Security的配置文件中,将该自定义AuthenticationFailureHandler设置为AuthenticationFailureHandler的实现类: ```java @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private CustomAuthenticationFailureHandler authenticationFailureHandler; @Override protected void configure(HttpSecurity http) throws Exception { http .formLogin() .loginPage("/login") .failureHandler(authenticationFailureHandler) .permitAll() .and() .authorizeRequests() .antMatchers("/login").permitAll() .antMatchers("/admin/**").hasRole("ADMIN") .anyRequest().authenticated(); } // 其他配置省略 } ``` 通过以上方式,可以根据不同的认证失败原因判断账号还是密码错误,并可进行相应处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值