Add authentication

This commit is contained in:
2023-10-05 21:11:22 +08:00
parent 78de04713f
commit 8e5375ab30
24 changed files with 580 additions and 15 deletions

View File

@@ -0,0 +1,14 @@
package top.fatweb.api.service
import com.baomidou.mybatisplus.extension.service.IService
import top.fatweb.api.entity.permission.User
/**
* <p>
* 用户 服务类
* </p>
*
* @author FatttSnake
* @since 2023-10-04
*/
interface IUserService : IService<User>

View File

@@ -0,0 +1,18 @@
package top.fatweb.api.service.impl
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import org.springframework.stereotype.Service
import top.fatweb.api.entity.permission.User
import top.fatweb.api.mapper.UserMapper
import top.fatweb.api.service.IUserService
/**
* <p>
* 用户 服务实现类
* </p>
*
* @author FatttSnake
* @since 2023-10-04
*/
@Service
class UserServiceImpl : ServiceImpl<UserMapper, User>(), IUserService

View File

@@ -0,0 +1,11 @@
package top.fatweb.api.service.permission
import top.fatweb.api.entity.permission.User
interface IAuthenticationService {
fun login(user: User): HashMap<String, String>
fun logout(token: String): Boolean
fun renewToken(token: String): HashMap<String, String>
}

View File

@@ -0,0 +1,61 @@
package top.fatweb.api.service.permission.impl
import org.springframework.security.authentication.AuthenticationManager
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.stereotype.Service
import top.fatweb.api.constant.SecurityConstants
import top.fatweb.api.entity.permission.LoginUser
import top.fatweb.api.entity.permission.User
import top.fatweb.api.service.permission.IAuthenticationService
import top.fatweb.api.util.JwtUtil
import top.fatweb.api.util.RedisUtil
import top.fatweb.api.util.WebUtil
import java.util.concurrent.TimeUnit
@Service
class AuthenticationServiceImpl(
private val authenticationManager: AuthenticationManager,
private val redisUtil: RedisUtil
) : IAuthenticationService {
override fun login(user: User): HashMap<String, String> {
val usernamePasswordAuthenticationToken = UsernamePasswordAuthenticationToken(user.username, user.password)
val authentication = authenticationManager.authenticate(usernamePasswordAuthenticationToken)
authentication ?: let {
throw RuntimeException("Login failed")
}
val loginUser = authentication.principal as LoginUser
loginUser.user.password = ""
val userId = loginUser.user.id.toString()
val jwt = JwtUtil.createJwt(userId)
jwt ?: let {
throw RuntimeException("Login failed")
}
val hashMap = hashMapOf("token" to jwt)
val redisKey = "${SecurityConstants.jwtIssuer}_login:" + jwt.substring(0, 32)
redisUtil.setObject(redisKey, loginUser, 20, TimeUnit.MINUTES)
return hashMap
}
override fun logout(token: String): Boolean =
redisUtil.delObject("${SecurityConstants.jwtIssuer}_login:" + token.substring(0, 32))
override fun renewToken(token: String): HashMap<String, String> {
val oldRedisKey = "${SecurityConstants.jwtIssuer}_login:" + token.substring(0, 32)
redisUtil.delObject(oldRedisKey)
val jwt = JwtUtil.createJwt(WebUtil.getLoginUserId().toString())
jwt ?: let {
throw RuntimeException("Login failed")
}
val hashMap = hashMapOf("token" to jwt)
val redisKey = "${SecurityConstants.jwtIssuer}_login:" + jwt.substring(0, 32)
redisUtil.setObject(redisKey, WebUtil.getLoginUser(), 20, TimeUnit.MINUTES)
return hashMap
}
}