package com.sheetsj.account;
import java.util.Collections;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import com.sheetsj.util.BusinessValidationException;
public class UserService implements UserDetailsService {
@Autowired
private AccountRepository accountRepository;
@Value("${another.property}")
private String anotherProperty;
@PostConstruct
protected void initialize() {
if (accountRepository.findByEmail("user") == null) {
accountRepository.save(new Account("user", "demo", "ROLE_USER"));
accountRepository.save(new Account("admin", "admin", "ROLE_ADMIN"));
}
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
if (username == null || username.length() < 3) {
throw new BusinessValidationException("username", "username.too.short", username);
}
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());
}
protected String getAnotherProperty() {
return anotherProperty;
}
}