Skip to content

Instantly share code, notes, and snippets.

@sophea
Last active January 23, 2020 14:02
Show Gist options
  • Save sophea/cd4e881df5609c2a5b6324c427a929c9 to your computer and use it in GitHub Desktop.
Save sophea/cd4e881df5609c2a5b6324c427a929c9 to your computer and use it in GitHub Desktop.
JwtUserDetailsService
package com.sma.security.service;
@Service
public class JwtUserDetailsService implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@Autowired
private PasswordEncoder bcryptEncoder;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserEntity user = userRepository.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("User not found with username: " + username);
}
return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(),
AuthorityUtils.createAuthorityList("ROLE_USER"));
}
//save new user
public UserEntity save(UserDTO user) {
UserEntity newUser = new UserEntity();
newUser.setUsername(user.getUsername());
newUser.setPassword(bcryptEncoder.encode(user.getPassword()));
return userRepository.save(newUser);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment