Adds JWT-based auth with refresh tokens
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package net.halfbinary.scavengerhuntapi.service
|
||||
|
||||
import net.halfbinary.scavengerhuntapi.repository.HunterRepository
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority
|
||||
import org.springframework.security.core.userdetails.User
|
||||
import org.springframework.security.core.userdetails.UserDetails
|
||||
import org.springframework.security.core.userdetails.UserDetailsService
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException
|
||||
import org.springframework.stereotype.Service
|
||||
import java.util.Collections
|
||||
|
||||
|
||||
@Service
|
||||
class HunterDetailsService(private val hunterRepository: HunterRepository): UserDetailsService {
|
||||
override fun loadUserByUsername(username: String): UserDetails {
|
||||
hunterRepository.findByEmail(username)
|
||||
?.let { hunter ->
|
||||
val hunterAuthorities =
|
||||
if (hunter.isAdmin) {
|
||||
SimpleGrantedAuthority("ROLE_ADMIN")
|
||||
} else {
|
||||
SimpleGrantedAuthority("ROLE_USER")
|
||||
}
|
||||
return User(
|
||||
hunter.email,
|
||||
hunter.password,
|
||||
Collections.singleton(hunterAuthorities)
|
||||
)
|
||||
}
|
||||
throw UsernameNotFoundException("User Not Found with username: $username")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user