feat: new AccountServiceImpl

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