org.fon.documentmanagementsystem.config.security.CustomUserDetailsService.java Source code

Java tutorial

Introduction

Here is the source code for org.fon.documentmanagementsystem.config.security.CustomUserDetailsService.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 org.fon.documentmanagementsystem.config.security;

import org.fon.documentmanagementsystem.domain.User;
import org.fon.documentmanagementsystem.dto.UserDto;
import org.fon.documentmanagementsystem.services.UserService;
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;

/**
 *
 * @author Vukasin
 */
@Component
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.findOne(username);
        if (user == null) {
            throw new UsernameNotFoundException("UserName " + username + " not found");
        }
        UserDto userDto = new UserDto(user.getUsername(), user.getPassword(), user.getIme(), user.getPrezime(),
                user.getIdRole());
        return userDto;
    }

}