com.alehuo.wepas2016projekti.service.CustomUserDetailsService.java Source code

Java tutorial

Introduction

Here is the source code for com.alehuo.wepas2016projekti.service.CustomUserDetailsService.java

Source

/*
 * Copyright (C) 2016 alehuo
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.alehuo.wepas2016projekti.service;

import com.alehuo.wepas2016projekti.domain.UserAccount;
import com.alehuo.wepas2016projekti.repository.UserAccountRepository;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
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;

/**
 * Kyttjtietopalvelu
 * @author alehuo
 */
@Service("userDetailsService")
public class CustomUserDetailsService implements UserDetailsService {

    /**
     * Kyttjrepository
     */
    @Autowired
    private UserAccountRepository userRepo;

    /**
     * Hakee kyttjn tietokannasta ja asettaa sille oikeat kyttoikeudet
     * @param string Kyttjtunnus
     * @return Kyttjtiedot
     * @throws UsernameNotFoundException 
     */
    @Override
    public UserDetails loadUserByUsername(String string) throws UsernameNotFoundException {

        UserAccount u = userRepo.findByUsernameIgnoreCase(string.trim());

        if (u == null) {
            throw new UsernameNotFoundException("Kyttjtunnusta " + string + " ei lydy");
        }

        return new org.springframework.security.core.userdetails.User(u.getUsername(), u.getPassword(), true, true,
                true, true, Arrays.asList(new SimpleGrantedAuthority(u.getRole().toString())));
    }

}