Search in sources :

Example 1 with TooManyLoginAttempts

use of pl.pollub.cs.pentagoncafe.flare.exception.auth.TooManyLoginAttempts in project Flare-event-calendar by PollubCafe.

the class UserDetailsServiceImpl method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String usernameOrEmail) throws UsernameNotFoundException {
    String ip = getClientIP();
    if (bruteForceAttackGuard.isBlocked(ip)) {
        throw new TooManyLoginAttempts();
    }
    User user;
    if (usernameOrEmail.contains("@")) {
        user = userRepository.findByEmail(usernameOrEmail).orElseThrow(() -> new UsernameNotFoundException(messages.get("login.userAccount.notFound.ByEmail")));
    } else {
        user = userRepository.findByNick(usernameOrEmail).orElseThrow(() -> new UsernameNotFoundException(messages.get("login.userAccount.notFound.ByUsername")));
    }
    boolean enabled = user.isEnabled();
    boolean accountNonLocked = !user.isBanned();
    return new UserDetailsImpl(user.getNick(), user.getPassword(), user.getEmail(), enabled, accountNonLocked, user.getRole());
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetailsImpl(pl.pollub.cs.pentagoncafe.flare.DTO.security.UserDetailsImpl) User(pl.pollub.cs.pentagoncafe.flare.domain.User) TooManyLoginAttempts(pl.pollub.cs.pentagoncafe.flare.exception.auth.TooManyLoginAttempts)

Aggregations

UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1 UserDetailsImpl (pl.pollub.cs.pentagoncafe.flare.DTO.security.UserDetailsImpl)1 User (pl.pollub.cs.pentagoncafe.flare.domain.User)1 TooManyLoginAttempts (pl.pollub.cs.pentagoncafe.flare.exception.auth.TooManyLoginAttempts)1