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) {
@ -35,8 +37,7 @@ public class MyUserDetailsService implements UserDetailsService {
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();
} }