com.jevontech.wabl.services.implementation.UserDetailsServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.jevontech.wabl.services.implementation.UserDetailsServiceImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jevontech.wabl.services.implementation;

/**
 *
 * @author jeroen
 */
import com.jevontech.wabl.entities.User;
import com.jevontech.wabl.factories.SecurityUserFactory;
import com.jevontech.wabl.repositories.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    private static final Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);

    private UserRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        if (userRepository == null) {
            throw new NullPointerException("userRepository== null");

        }

        User user = this.userRepository.findByUsername(username);

        if (user == null) {
            throw new UsernameNotFoundException(String.format("No user found with username '%s'.", username));
        } else {
            return SecurityUserFactory.create(user);
        }
    }

    public void setRepository(UserRepository userRepository) {

        log.info("UserDetailsServiceImpl: setRepository");

        if (userRepository == null) {
            throw new NullPointerException();
        }

        this.userRepository = userRepository;

    }

}