Search in sources :

Example 86 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project jhipster-sample-app-mongodb 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)

Example 87 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project jhipster-sample-app-websocket by jhipster.

the class PersistentTokenRememberMeServices method onLoginSuccess.

@Override
protected void onLoginSuccess(HttpServletRequest request, HttpServletResponse response, Authentication successfulAuthentication) {
    String login = successfulAuthentication.getName();
    log.debug("Creating new persistent login for user {}", login);
    PersistentToken token = userRepository.findOneByLogin(login).map(u -> {
        PersistentToken t = new PersistentToken();
        t.setSeries(RandomUtil.generateSeriesData());
        t.setUser(u);
        t.setTokenValue(RandomUtil.generateTokenData());
        t.setTokenDate(LocalDate.now());
        t.setIpAddress(request.getRemoteAddr());
        t.setUserAgent(request.getHeader("User-Agent"));
        return t;
    }).orElseThrow(() -> new UsernameNotFoundException("User " + login + " was not found in the database"));
    try {
        persistentTokenRepository.saveAndFlush(token);
        addCookie(token, request, response);
    } catch (DataAccessException e) {
        log.error("Failed to save persistent token ", e);
    }
}
Also used : DataAccessException(org.springframework.dao.DataAccessException) 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) HttpServletResponse(javax.servlet.http.HttpServletResponse) PersistentToken(io.github.jhipster.sample.domain.PersistentToken) PersistentTokenRepository(io.github.jhipster.sample.repository.PersistentTokenRepository) Serializable(java.io.Serializable) TimeUnit(java.util.concurrent.TimeUnit) HttpServletRequest(javax.servlet.http.HttpServletRequest) Service(org.springframework.stereotype.Service) LocalDate(java.time.LocalDate) UserDetails(org.springframework.security.core.userdetails.UserDetails) RandomUtil(io.github.jhipster.sample.service.util.RandomUtil) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) Authentication(org.springframework.security.core.Authentication) org.springframework.security.web.authentication.rememberme(org.springframework.security.web.authentication.rememberme) JHipsterProperties(io.github.jhipster.config.JHipsterProperties) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) PersistentToken(io.github.jhipster.sample.domain.PersistentToken) DataAccessException(org.springframework.dao.DataAccessException)

Example 88 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project jhipster-sample-app-hazelcast by jhipster.

the class DomainUserDetailsService method loadUserByUsername.

@Override
@Transactional
public UserDetails loadUserByUsername(final String login) {
    log.debug("Authenticating {}", login);
    if (new EmailValidator().isValid(login, null)) {
        Optional<User> userByEmailFromDatabase = userRepository.findOneWithAuthoritiesByEmail(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.findOneWithAuthoritiesByLogin(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) Transactional(org.springframework.transaction.annotation.Transactional) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) EmailValidator(org.hibernate.validator.internal.constraintvalidators.hv.EmailValidator) User(io.github.jhipster.sample.domain.User) Transactional(org.springframework.transaction.annotation.Transactional)

Example 89 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException 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)

Example 90 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project jhipster-sample-app-dto by jhipster.

the class PersistentTokenRememberMeServices method onLoginSuccess.

@Override
protected void onLoginSuccess(HttpServletRequest request, HttpServletResponse response, Authentication successfulAuthentication) {
    String login = successfulAuthentication.getName();
    log.debug("Creating new persistent login for user {}", login);
    PersistentToken token = userRepository.findOneByLogin(login).map(u -> {
        PersistentToken t = new PersistentToken();
        t.setSeries(RandomUtil.generateSeriesData());
        t.setUser(u);
        t.setTokenValue(RandomUtil.generateTokenData());
        t.setTokenDate(LocalDate.now());
        t.setIpAddress(request.getRemoteAddr());
        t.setUserAgent(request.getHeader("User-Agent"));
        return t;
    }).orElseThrow(() -> new UsernameNotFoundException("User " + login + " was not found in the database"));
    try {
        persistentTokenRepository.saveAndFlush(token);
        addCookie(token, request, response);
    } catch (DataAccessException e) {
        log.error("Failed to save persistent token ", e);
    }
}
Also used : DataAccessException(org.springframework.dao.DataAccessException) 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) HttpServletResponse(javax.servlet.http.HttpServletResponse) PersistentToken(io.github.jhipster.sample.domain.PersistentToken) PersistentTokenRepository(io.github.jhipster.sample.repository.PersistentTokenRepository) Serializable(java.io.Serializable) TimeUnit(java.util.concurrent.TimeUnit) HttpServletRequest(javax.servlet.http.HttpServletRequest) Service(org.springframework.stereotype.Service) LocalDate(java.time.LocalDate) UserDetails(org.springframework.security.core.userdetails.UserDetails) RandomUtil(io.github.jhipster.sample.service.util.RandomUtil) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) Authentication(org.springframework.security.core.Authentication) org.springframework.security.web.authentication.rememberme(org.springframework.security.web.authentication.rememberme) JHipsterProperties(io.github.jhipster.config.JHipsterProperties) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) PersistentToken(io.github.jhipster.sample.domain.PersistentToken) DataAccessException(org.springframework.dao.DataAccessException)

Aggregations

UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)132 GrantedAuthority (org.springframework.security.core.GrantedAuthority)40 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)39 UserDetails (org.springframework.security.core.userdetails.UserDetails)36 Authentication (org.springframework.security.core.Authentication)24 Transactional (org.springframework.transaction.annotation.Transactional)20 Logger (org.slf4j.Logger)18 LoggerFactory (org.slf4j.LoggerFactory)18 java.util (java.util)16 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)15 Collectors (java.util.stream.Collectors)14 UserDetailsService (org.springframework.security.core.userdetails.UserDetailsService)14 Component (org.springframework.stereotype.Component)14 User (org.springframework.security.core.userdetails.User)13 ArrayList (java.util.ArrayList)12 HashSet (java.util.HashSet)11 UserRepository (io.github.jhipster.sample.repository.UserRepository)9 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)9 User (io.github.jhipster.sample.domain.User)6 Date (java.util.Date)6