From 2f76422054ea0535f8d64980b636cdeba6a636d6 Mon Sep 17 00:00:00 2001 From: SOR <3272194354@qq.com> Date: Sat, 25 Jul 2026 14:21:24 +0800 Subject: [PATCH] feat: new AccountServiceImpl --- .../itstudio/itmanager/entity/dto/Account.kt | 11 ++++++---- .../service/impl/AccountServiceImpl.kt | 20 +++++++++---------- .../club/itstudio/itmanager/utils/JwtUtils.kt | 18 +++++++++-------- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/beckend/src/main/kotlin/club/itstudio/itmanager/entity/dto/Account.kt b/src/beckend/src/main/kotlin/club/itstudio/itmanager/entity/dto/Account.kt index beb8ba9..e60b7e9 100644 --- a/src/beckend/src/main/kotlin/club/itstudio/itmanager/entity/dto/Account.kt +++ b/src/beckend/src/main/kotlin/club/itstudio/itmanager/entity/dto/Account.kt @@ -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 \ No newline at end of file diff --git a/src/beckend/src/main/kotlin/club/itstudio/itmanager/service/impl/AccountServiceImpl.kt b/src/beckend/src/main/kotlin/club/itstudio/itmanager/service/impl/AccountServiceImpl.kt index 4f75139..e909420 100644 --- a/src/beckend/src/main/kotlin/club/itstudio/itmanager/service/impl/AccountServiceImpl.kt +++ b/src/beckend/src/main/kotlin/club/itstudio/itmanager/service/impl/AccountServiceImpl.kt @@ -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(), AccountService { override fun findByUsername(username: String): Mono { @@ -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) } diff --git a/src/beckend/src/main/kotlin/club/itstudio/itmanager/utils/JwtUtils.kt b/src/beckend/src/main/kotlin/club/itstudio/itmanager/utils/JwtUtils.kt index 919399e..f1f431c 100644 --- a/src/beckend/src/main/kotlin/club/itstudio/itmanager/utils/JwtUtils.kt +++ b/src/beckend/src/main/kotlin/club/itstudio/itmanager/utils/JwtUtils.kt @@ -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()