Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. import com.kotlinspringvue.backend.repository.UserRepository
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired
  4. import org.springframework.security.core.userdetails.UserDetails
  5. import org.springframework.security.core.userdetails.UserDetailsService
  6. import org.springframework.security.core.userdetails.UsernameNotFoundException
  7. import org.springframework.stereotype.Service
  8. import org.springframework.security.core.GrantedAuthority
  9. import org.springframework.security.core.authority.SimpleGrantedAuthority
  10. import java.util.stream.Collectors
  11.  
  12. @Service
  13. class UserDetailsServiceImpl: UserDetailsService {
  14.  
  15. @Autowired
  16. lateinit var userRepository: UserRepository
  17.  
  18. @Throws(UsernameNotFoundException::class)
  19. override fun loadUserByUsername(username: String): UserDetails {
  20. val user = userRepository.findByUsername(username).get()
  21. ?: throw UsernameNotFoundException("User '$username' not found")
  22.  
  23. val authorities: List<GrantedAuthority> = user.roles!!.stream().map({ role -> SimpleGrantedAuthority(role.name)}).collect(Collectors.toList<GrantedAuthority>())
  24.  
  25. return org.springframework.security.core.userdetails.User
  26. .withUsername(username)
  27. .password(user.password)
  28. .authorities(authorities)
  29. .accountExpired(false)
  30. .accountLocked(false)
  31. .credentialsExpired(false)
  32. .disabled(false)
  33. .build()
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement