Constructor injection user details

This commit is contained in:
Rohan Sircar 2020-05-27 17:09:15 +05:30
parent af024f4cef
commit 4a8e409539

View File

@ -12,14 +12,16 @@ import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@Service @Service
@Slf4j @Slf4j
@RequiredArgsConstructor
public class MyUserDetailsService implements UserDetailsService { public class MyUserDetailsService implements UserDetailsService {
@Autowired @Autowired
private UserService userService; private final UserService userService;
@Override @Override
public UserDetails loadUserByUsername(final String username) { public UserDetails loadUserByUsername(final String username) {
@ -32,11 +34,10 @@ public class MyUserDetailsService implements UserDetailsService {
} }
Set<UserRole> userRoles = user.getUserRoles(); Set<UserRole> userRoles = user.getUserRoles();
return User.withUsername(user.getUserName()) return User.withUsername(user.getUserName())
.password(user.getPassword()) .password(user.getPassword())
.roles(userRoles.stream() .roles(userRoles.stream().map(ur -> ur.getRole().getName())
.map(ur -> ur.getRole().getName())
.toArray(size -> new String[size])) .toArray(size -> new String[size]))
.build(); .build();
} }