Newer
Older
bremer / src / main / kotlin / service / LoginService.kt
yhornisse on 5 Aug 2023 993 bytes refactor and add tag
/*
 * Copyright (c) 2023. yo-saito. All Rights Reserved.
 */

package net.piedpiper.bremer.service

import net.piedpiper.bremer.model.api.BremerUser
import net.piedpiper.bremer.repository.UserRepository
import org.springframework.beans.factory.annotation.Qualifier
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 org.springframework.transaction.annotation.Transactional

@Service("bremer.service.LoginService")
class LoginService(
    @Qualifier("bremer.dao.UserRepository")
    private val userRepository: UserRepository
) : UserDetailsService {

    @Transactional
    override fun loadUserByUsername(name: String?): UserDetails? = name?.let {
        userRepository.findOneByName(name)?.let { BremerUser(it) }
    } ?: throw UsernameNotFoundException("Login failed!")
}