Last active
January 23, 2020 14:02
-
-
Save sophea/cd4e881df5609c2a5b6324c427a929c9 to your computer and use it in GitHub Desktop.
JwtUserDetailsService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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