Search in sources :

Example 6 with EmailValidator

use of org.hibernate.validator.internal.constraintvalidators.hv.EmailValidator in project jhipster-sample-app-cassandra by jhipster.

the class DomainUserDetailsService method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(final String login) {
    log.debug("Authenticating {}", login);
    if (new EmailValidator().isValid(login, null)) {
        Optional<User> userByEmailFromDatabase = userRepository.findOneByEmailIgnoreCase(login);
        return userByEmailFromDatabase.map(user -> createSpringSecurityUser(login, user)).orElseThrow(() -> new UsernameNotFoundException("User with email " + login + " was not found in the database"));
    }
    String lowercaseLogin = login.toLowerCase(Locale.ENGLISH);
    Optional<User> userByLoginFromDatabase = userRepository.findOneByLogin(lowercaseLogin);
    return userByLoginFromDatabase.map(user -> createSpringSecurityUser(lowercaseLogin, user)).orElseThrow(() -> new UsernameNotFoundException("User " + lowercaseLogin + " was not found in the database"));
}
Also used : EmailValidator(org.hibernate.validator.internal.constraintvalidators.hv.EmailValidator) java.util(java.util) UserRepository(io.github.jhipster.sample.repository.UserRepository) Logger(org.slf4j.Logger) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) LoggerFactory(org.slf4j.LoggerFactory) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Collectors(java.util.stream.Collectors) GrantedAuthority(org.springframework.security.core.GrantedAuthority) User(io.github.jhipster.sample.domain.User) Component(org.springframework.stereotype.Component) UserDetails(org.springframework.security.core.userdetails.UserDetails) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) EmailValidator(org.hibernate.validator.internal.constraintvalidators.hv.EmailValidator) User(io.github.jhipster.sample.domain.User)

Aggregations

User (io.github.jhipster.sample.domain.User)6 UserRepository (io.github.jhipster.sample.repository.UserRepository)6 java.util (java.util)6 Collectors (java.util.stream.Collectors)6 EmailValidator (org.hibernate.validator.internal.constraintvalidators.hv.EmailValidator)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 GrantedAuthority (org.springframework.security.core.GrantedAuthority)6 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)6 UserDetails (org.springframework.security.core.userdetails.UserDetails)6 UserDetailsService (org.springframework.security.core.userdetails.UserDetailsService)6 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)6 Component (org.springframework.stereotype.Component)6 Transactional (org.springframework.transaction.annotation.Transactional)4