CustomLoadUserByUsername.java 1.26 KB
package com.daeucna.board.security;

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.Component;

import com.daeucna.board.security.dao.UserDao;
import com.daeucna.board.security.domain.UserDto;
import com.daeucna.board.security.domain.UserForm;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class CustomLoadUserByUsername implements UserDetailsService{

    @Autowired
    UserDao userDao;

  @Override
    public UserDetails loadUserByUsername(String loginId) throws UsernameNotFoundException {
	  	UserForm userForm = new UserForm();
	  	userForm.setEmail(loginId);
	  	
	  	UserDto user = null;
	  	try {
	        user = userDao.getOne(userForm);
	        if(user == null) {
	        	log.warn("Not Found User");
	        	throw new UsernameNotFoundException("Not Found User");
	        }
	  	} catch (Exception e) {	
	  		log.error("Not Found User Error");
	  		throw new UsernameNotFoundException("Not Found User(Error)");
	  	}
        return user;
    }

}