feat: new AccountServiceImpl
This commit is contained in:
@@ -11,10 +11,13 @@ import kotlinx.serialization.Serializable
|
||||
@TableName("account")
|
||||
data class Account(
|
||||
@TableId(type = IdType.AUTO)
|
||||
val id: Int? = null,
|
||||
val id: Long? = null,
|
||||
val username: String = "",
|
||||
val password: String = "",
|
||||
val email: String = "",
|
||||
val role: String = "",
|
||||
val registerTime: LocalDateTime? = null
|
||||
val email: String? = null,
|
||||
val fullName: String? = null,
|
||||
val isActive: Boolean = true,
|
||||
val isStaff: Boolean = false,
|
||||
val createdAt: LocalDateTime? = null,
|
||||
val updatedAt: LocalDateTime? = null
|
||||
) : DataCopy
|
||||
+9
-11
@@ -10,7 +10,7 @@ import club.itstudio.itmanager.entity.vo.response.ReloginVO
|
||||
import club.itstudio.itmanager.mapper.AccountMapper
|
||||
import club.itstudio.itmanager.service.AccountService
|
||||
import club.itstudio.itmanager.utils.Const
|
||||
import club.itstudio.itmanager.utils.DateUtils.getCurrentDateTime
|
||||
|
||||
import club.itstudio.itmanager.utils.FlowUtils
|
||||
import club.itstudio.itmanager.utils.JwtUtils
|
||||
import org.springframework.amqp.core.AmqpTemplate
|
||||
@@ -31,7 +31,6 @@ class AccountServiceImpl(
|
||||
private val amqpTemplate: AmqpTemplate,
|
||||
private val stringRedisTemplate: ReactiveStringRedisTemplate,
|
||||
private val encoder: PasswordEncoder
|
||||
|
||||
) : ServiceImpl<AccountMapper, Account>(), AccountService {
|
||||
|
||||
override fun findByUsername(username: String): Mono<UserDetails> {
|
||||
@@ -40,7 +39,7 @@ class AccountServiceImpl(
|
||||
.map { account ->
|
||||
User.withUsername(username)
|
||||
.password(account.password)
|
||||
.roles(account.role)
|
||||
.roles(if (account.isStaff) "STAFF" else "USER")
|
||||
.build()
|
||||
}
|
||||
}
|
||||
@@ -82,7 +81,7 @@ class AccountServiceImpl(
|
||||
val (email, code, username, password) = vo
|
||||
|
||||
return verifyCode(email, code).flatMap { error ->
|
||||
if (!error.isBlank()) {
|
||||
if (error.isNotBlank()) {
|
||||
return@flatMap Mono.just(error)
|
||||
}
|
||||
|
||||
@@ -98,12 +97,11 @@ class AccountServiceImpl(
|
||||
|
||||
val encodedPassword = encoder.encode(password)
|
||||
val account = Account(
|
||||
null,
|
||||
username,
|
||||
encodedPassword!!,
|
||||
email,
|
||||
"user",
|
||||
getCurrentDateTime()
|
||||
username = username,
|
||||
password = encodedPassword!!,
|
||||
email = email,
|
||||
isActive = true,
|
||||
isStaff = false
|
||||
)
|
||||
|
||||
Mono.fromCallable { this.save(account) }
|
||||
@@ -124,7 +122,7 @@ class AccountServiceImpl(
|
||||
val (email, code, password) = vo
|
||||
|
||||
return verifyCode(email, code).flatMap { error ->
|
||||
if (!error.isBlank()) {
|
||||
if (error.isNotBlank()) {
|
||||
return@flatMap Mono.just(error)
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.auth0.jwt.JWT
|
||||
import com.auth0.jwt.JWTVerifier
|
||||
import com.auth0.jwt.algorithms.Algorithm
|
||||
import com.auth0.jwt.interfaces.DecodedJWT
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.data.redis.core.StringRedisTemplate
|
||||
import org.springframework.security.core.userdetails.User
|
||||
@@ -16,14 +17,15 @@ import java.util.concurrent.TimeUnit
|
||||
class JwtUtils(
|
||||
@param:Value($$"${spring.security.jwt.key}")
|
||||
private val key: String,
|
||||
|
||||
@param:Value($$"${spring.security.jwt.expire-hours}")
|
||||
private val expireHours: Int,
|
||||
|
||||
private val template: StringRedisTemplate,
|
||||
private val algorithm: Algorithm = Algorithm.HMAC256(key),
|
||||
private val jwtVerifier: JWTVerifier = JWT.require(algorithm).build()
|
||||
private val template: StringRedisTemplate
|
||||
) {
|
||||
private val algorithm: Algorithm = Algorithm.HMAC256(key)
|
||||
private val jwtVerifier: JWTVerifier = JWT.require(algorithm).build()
|
||||
private val logger = LoggerFactory.getLogger(this::class.java)
|
||||
|
||||
fun invalidateJwt(headerToken: String?): Boolean {
|
||||
val token = this.convertToToken(headerToken) ?: return false
|
||||
try {
|
||||
@@ -31,7 +33,7 @@ class JwtUtils(
|
||||
val id = jwt.id
|
||||
return deleteToken(id, jwt.expiresAt)
|
||||
} catch (e: Exception) {
|
||||
println(e.message)
|
||||
logger.error("Error occurred while invalidating JWT", e)
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -62,14 +64,14 @@ class JwtUtils(
|
||||
else decodedJWT
|
||||
|
||||
} catch (e: Exception) {
|
||||
println(e.message)
|
||||
logger.error("Error occurred while resolving JWT", e)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
fun createJwt(
|
||||
details: UserDetails,
|
||||
id: Int,
|
||||
id: Long,
|
||||
username: String
|
||||
): String {
|
||||
return JWT.create()
|
||||
@@ -94,7 +96,7 @@ class JwtUtils(
|
||||
.build()
|
||||
}
|
||||
|
||||
fun toId(jwt: DecodedJWT) = jwt.claims["id"]?.asInt() ?: -1
|
||||
fun toId(jwt: DecodedJWT) = jwt.claims["id"]?.asLong() ?: -1L
|
||||
|
||||
fun expiresTime(): Date {
|
||||
val cal = Calendar.getInstance()
|
||||
|
||||
Reference in New Issue
Block a user