mirror of
https://github.com/SOR2171/Springboot4-Vue3_Demo.git
synced 2026-10-26 21:11:10 +08:00
refactor: AccountServiceImpl.kt
This commit is contained in:
+77
-45
@@ -10,104 +10,136 @@ import org.springframework.security.core.userdetails.UserDetails
|
|||||||
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 reactor.core.publisher.Mono
|
import reactor.core.publisher.Mono
|
||||||
|
import reactor.core.scheduler.Schedulers
|
||||||
|
import javax.security.auth.login.AccountNotFoundException
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
class AccountServiceImpl : ServiceImpl<AccountMapper, Account>(), AccountService {
|
class AccountServiceImpl : ServiceImpl<AccountMapper, Account>(), AccountService {
|
||||||
|
|
||||||
val logger = LoggerFactory.getLogger(this::class.java)!!
|
private val logger = LoggerFactory.getLogger(this::class.java)
|
||||||
|
|
||||||
|
override fun findByUsername(
|
||||||
|
username: String
|
||||||
|
): Mono<UserDetails> {
|
||||||
|
|
||||||
override fun findByUsername(username: String): Mono<UserDetails> {
|
|
||||||
return findAccountByName(username)
|
return findAccountByName(username)
|
||||||
.switchIfEmpty(Mono.error(UsernameNotFoundException("Account with name $username not found")))
|
.map {
|
||||||
.map { account ->
|
User.withUsername(it.username)
|
||||||
User.withUsername(username)
|
.password(it.password)
|
||||||
.password(account.password)
|
.roles(it.role)
|
||||||
.roles(account.role)
|
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
.onErrorMap(
|
||||||
|
AccountNotFoundException::class.java
|
||||||
|
) {
|
||||||
|
UsernameNotFoundException(
|
||||||
|
"User not found"
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun findAccountByName(username: String): Mono<Account> =
|
override fun findAccountByName(username: String): Mono<Account> =
|
||||||
findAccount("name", username) {
|
findAccount("username") {
|
||||||
ktQuery()
|
ktQuery()
|
||||||
.eq(Account::username, username)
|
.eq(Account::username, username)
|
||||||
.one()
|
.one()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun findAccountByEmail(email: String): Mono<Account> =
|
override fun findAccountByEmail(email: String): Mono<Account> =
|
||||||
findAccount("email", email) {
|
findAccount("email") {
|
||||||
ktQuery()
|
ktQuery()
|
||||||
.eq(Account::email, email)
|
.eq(Account::email, email)
|
||||||
.one()
|
.one()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun <T : Any> blockingCall(
|
||||||
|
block: () -> T?
|
||||||
|
): Mono<T> =
|
||||||
|
Mono.fromCallable(block)
|
||||||
|
.subscribeOn(Schedulers.boundedElastic())
|
||||||
|
.flatMap {
|
||||||
|
Mono.just(it)
|
||||||
|
}
|
||||||
|
|
||||||
private fun findAccount(
|
private fun findAccount(
|
||||||
fieldName: String,
|
fieldName: String,
|
||||||
value: String,
|
|
||||||
query: () -> Account?
|
query: () -> Account?
|
||||||
): Mono<Account> =
|
): Mono<Account> {
|
||||||
Mono.fromCallable {
|
return blockingCall {
|
||||||
logger.info("Find account with {} {}", fieldName, value)
|
logger.info(
|
||||||
|
"Searching account by {}",
|
||||||
|
fieldName
|
||||||
|
)
|
||||||
query()
|
query()
|
||||||
}.switchIfEmpty(
|
}.switchIfEmpty(
|
||||||
Mono.error(
|
Mono.error(
|
||||||
UsernameNotFoundException("Account with $fieldName $value not found")
|
AccountNotFoundException()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
override fun findAccountByNameOrEmail(text: String): Mono<Account> =
|
|
||||||
findAccountByEmail(text).onErrorResume(
|
|
||||||
UsernameNotFoundException::class.java
|
|
||||||
) {
|
|
||||||
findAccountByName(text)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun existAccountByName(username: String): Mono<Boolean> =
|
override fun findAccountByNameOrEmail(
|
||||||
Mono.fromCallable {
|
text: String
|
||||||
logger.info("Determine if name {} exists", username)
|
): Mono<Account> {
|
||||||
|
return blockingCall {
|
||||||
|
logger.info(
|
||||||
|
"Searching account by identifier"
|
||||||
|
)
|
||||||
|
ktQuery()
|
||||||
|
.and {
|
||||||
|
it.eq(Account::email, text)
|
||||||
|
.or()
|
||||||
|
.eq(Account::username, text)
|
||||||
|
}
|
||||||
|
.one()
|
||||||
|
}.switchIfEmpty(
|
||||||
|
Mono.error(AccountNotFoundException())
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun existAccountByName(
|
||||||
|
username: String
|
||||||
|
): Mono<Boolean> {
|
||||||
|
return blockingCall {
|
||||||
ktQuery()
|
ktQuery()
|
||||||
.eq(Account::username, username)
|
.eq(Account::username, username)
|
||||||
.exists()
|
.exists()
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun existAccountByEmail(email: String): Mono<Boolean> =
|
override fun existAccountByEmail(
|
||||||
Mono.fromCallable {
|
email: String
|
||||||
logger.info("Determine if email {} exists", email)
|
): Mono<Boolean> {
|
||||||
|
return blockingCall {
|
||||||
ktQuery()
|
ktQuery()
|
||||||
.eq(Account::email, email)
|
.eq(Account::email, email)
|
||||||
.exists()
|
.exists()
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun resetPasswordByEmail(
|
override fun resetPasswordByEmail(
|
||||||
email: String,
|
email: String,
|
||||||
encodedPassword: String
|
encodedPassword: String
|
||||||
): Mono<Void> =
|
): Mono<Void> {
|
||||||
existAccountByEmail(email)
|
return blockingCall {
|
||||||
.flatMap { exists ->
|
|
||||||
if (!exists) {
|
|
||||||
return@flatMap Mono.error(
|
|
||||||
UsernameNotFoundException(
|
|
||||||
"Account with email $email not found"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
Mono.fromCallable {
|
|
||||||
ktUpdate()
|
ktUpdate()
|
||||||
.eq(Account::email, email)
|
.eq(Account::email, email)
|
||||||
.set(Account::password, encodedPassword)
|
.set(
|
||||||
|
Account::password,
|
||||||
|
encodedPassword
|
||||||
|
)
|
||||||
.update()
|
.update()
|
||||||
}
|
}
|
||||||
}
|
.flatMap { updated ->
|
||||||
.flatMap { success ->
|
if (updated) {
|
||||||
if (success) {
|
|
||||||
Mono.empty()
|
Mono.empty()
|
||||||
} else {
|
} else {
|
||||||
Mono.error(
|
Mono.error(
|
||||||
IllegalStateException(
|
AccountNotFoundException()
|
||||||
"Failed to reset password"
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user