spring security tylko jedno logowanie dla konta

0

Witam,

Próbuje zrobić logowanie dla użytkowników przy użyciu spring security, z tylko jednym możliwym poprawnym zalogwaniem. Mam taką klasę i chciałbym sprawdzić czy hasło pasuje do tego, które jest przechowywane w bazie danych. Tak wiem spring sam je sprawdza ale mi jest ono potrzebne do tego aby konta nie zostały zablokowane przy nieudanej próbie logowania. Na chwie obecną konto jest blokowane nawet przy nieudanej próbie logowania. Moje pytanie brzmi jak uzyskać w klasie niżej hasło, które przyszło z formularza. Z góry dzięki.

public class CustomUserDetailsService implements UserDetailsService {

    private final AccountRepository accountRepository;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        Account user = accountRepository.findByLogin(userName);

        if(null != user){

            if("ROLE_STUDENT".equals(user.getPermission().getName())){
                if(user.getStudentDetails().isActive()) {
                    user.getStudentDetails().setActive(false);
                    accountRepository.save(user);
                    return new CustomUserDetails(user);
                }
            }
            if("ROLE_ADMIN".equals(user.getPermission().getName())) {
                return new CustomUserDetails(user);
            }
        }
        throw new UsernameNotFoundException("No user present with username: " + userName);
    }

}
1

Nie do końca rozumiem pytanie, ale jeśli coś chcesz zrobić/sprawdzić po poprawnym lub nie udanym logowaniu zaimplementuj sobie
AuthenticationSuccessHandler dla poprawnego zalogowania oraz AuthenticationFailureHandler. Przykłady z netu:

 public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
     @Override
     public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws
                                                                                                                                  IOException,
                                                                                                                                  ServletException {
         User principal = (User) authentication.getPrincipal();
         System.out.println("principal" + principal.getUsername());
         boolean isAdmin = false;
         Iterator<GrantedAuthority> grantedAuthorityIterator = principal.getAuthorities().iterator();
         while (grantedAuthorityIterator.hasNext()) {
             if (grantedAuthorityIterator.next().getAuthority().equalsIgnoreCase("ROLE_ADMIN")) {
                 isAdmin = true;
             }
         }
         if (isAdmin) {
             response.sendRedirect("/admin");
         } else {
             response.sendRedirect("/home");
         }
     }
 }

AuthenticationFailureHandler:

 public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler {
     @Override
     public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws
                                                                                                                              IOException,
                                                                                                                              ServletException {
         // write your custom code here
         response.sendRedirect("/loginFailed");
     }
 }

źródło: https://javadeveloperzone.com/spring-boot/spring-security-custom-success-or-fail-handler/

0

WIelkie dzięki inweo. Dokładnie o to chodziło. Temat do zamknięcia.

1 użytkowników online, w tym zalogowanych: 0, gości: 1