Skip to content

Instantly share code, notes, and snippets.

@iamprafful
Created January 16, 2022 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamprafful/f2fbd32d76a59c040025225299492e1a to your computer and use it in GitHub Desktop.
Save iamprafful/f2fbd32d76a59c040025225299492e1a to your computer and use it in GitHub Desktop.
package com.example.api.service;
import com.example.api.repository.UserRepository;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
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 org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class JwtUserDetailsService implements UserDetailsService {
final UserRepository userRepository;
public JwtUserDetailsService(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
com.example.api.model.User user = userRepository.findUserByUsername(username);
List<GrantedAuthority> authorityList = new ArrayList<>();
authorityList.add(new SimpleGrantedAuthority("USER_ROLE"));
return new User(user.getUserName(), user.getPassword(), authorityList);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment