Package $

Source Code of $.UserService

package ${package}.account;

import java.util.Collections;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.*;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.*;

public class UserService implements UserDetailsService {
 
  @Autowired
  private AccountRepository accountRepository;
 
  @PostConstruct 
  protected void initialize() {
    accountRepository.save(new Account("user", "demo", "ROLE_USER"));
    accountRepository.save(new Account("admin", "admin", "ROLE_ADMIN"));
  }
 
  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    Account account = accountRepository.findByEmail(username);
    if(account == null) {
      throw new UsernameNotFoundException("user not found");
    }
    return createUser(account);
  }
 
  public void signin(Account account) {
    SecurityContextHolder.getContext().setAuthentication(authenticate(account));
  }
 
  private Authentication authenticate(Account account) {
    return new UsernamePasswordAuthenticationToken(createUser(account), null, Collections.singleton(createAuthority(account)));   
  }
 
  private User createUser(Account account) {
    return new User(account.getEmail(), account.getPassword(), Collections.singleton(createAuthority(account)));
  }

  private GrantedAuthority createAuthority(Account account) {
    return new SimpleGrantedAuthority(account.getRole());
  }

}
TOP

Related Classes of $.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.