Package eu.franzoni.domain

Source Code of eu.franzoni.domain.UserService

package eu.franzoni.domain;

import java.util.Collections;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.*;
import org.springframework.stereotype.Service;

import eu.franzoni.domain.User;

@Service("userService")
public class UserService implements UserDetailsService {
 
  @Autowired
  private UserRepository userRepository;
 
  @PostConstruct 
  protected void initialize() {
    userRepository.save(new User("user", "demo", "ROLE_USER"));
    userRepository.save(new User("admin", "admin", "ROLE_ADMIN"));
  }
 
  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    User user = userRepository.findByUsername(username);
    if(user == null) {
      throw new UsernameNotFoundException("user not found");
    }
    GrantedAuthority authority = new SimpleGrantedAuthority(user.getRole());
    return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), Collections.singleton(authority));
  }

}
TOP

Related Classes of eu.franzoni.domain.UserService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.