Support login with email

This commit is contained in:
2023-12-20 14:55:30 +08:00
parent 60353906ad
commit e7c8311b83
13 changed files with 34 additions and 43 deletions

View File

@@ -2,6 +2,7 @@ package top.fatweb.api.service.permission
import jakarta.servlet.http.HttpServletRequest
import top.fatweb.api.entity.permission.User
import top.fatweb.api.param.permission.LoginParam
import top.fatweb.api.vo.permission.LoginVo
import top.fatweb.api.vo.permission.TokenVo
@@ -16,7 +17,7 @@ interface IAuthenticationService {
* Login
*
* @param request
* @param user User object
* @param loginParam Login parameters
* @return LoginVo object
* @author FatttSnake, fatttsnake@gmail.com
* @since 1.0.0
@@ -24,7 +25,7 @@ interface IAuthenticationService {
* @see User
* @see LoginVo
*/
fun login(request: HttpServletRequest, user: User): LoginVo
fun login(request: HttpServletRequest, loginParam: LoginParam): LoginVo
/**
* Logout

View File

@@ -19,15 +19,15 @@ import top.fatweb.api.vo.permission.UserWithRoleInfoVo
*/
interface IUserService : IService<User> {
/**
* Get user with power by username
* Get user with power by username or email
*
* @param username Username
* @param account Username or email
* @return User object
* @author FatttSnake, fatttsnake@gmail.com
* @since 1.0.0
* @see User
*/
fun getUserWithPowerByUsername(username: String): User?
fun getUserWithPowerByAccount(account: String): User?
/**
* Get user information

View File

@@ -12,6 +12,7 @@ import top.fatweb.api.entity.permission.LoginUser
import top.fatweb.api.entity.permission.User
import top.fatweb.api.entity.system.EventLog
import top.fatweb.api.exception.TokenHasExpiredException
import top.fatweb.api.param.permission.LoginParam
import top.fatweb.api.properties.SecurityProperties
import top.fatweb.api.service.permission.IAuthenticationService
import top.fatweb.api.service.permission.IUserService
@@ -42,8 +43,9 @@ class AuthenticationServiceImpl(
private val logger: Logger = LoggerFactory.getLogger(this::class.java)
@EventLogRecord(EventLog.Event.LOGIN)
override fun login(request: HttpServletRequest, user: User): LoginVo {
val usernamePasswordAuthenticationToken = UsernamePasswordAuthenticationToken(user.username, user.password)
override fun login(request: HttpServletRequest, loginParam: LoginParam): LoginVo {
val usernamePasswordAuthenticationToken =
UsernamePasswordAuthenticationToken(loginParam.account, loginParam.password)
val authentication = authenticationManager.authenticate(usernamePasswordAuthenticationToken)
authentication ?: let {
throw RuntimeException("Login failed")
@@ -52,13 +54,13 @@ class AuthenticationServiceImpl(
val loginUser = authentication.principal as LoginUser
loginUser.user.password = ""
logger.info("用户登录 [用户名: '{}', IP: '{}']", user.username, request.remoteAddr)
logger.info("用户登录 [用户名: '{}', IP: '{}']", loginUser.username, request.remoteAddr)
userService.update(User().apply {
currentLoginIp = request.remoteAddr
currentLoginTime = LocalDateTime.now(ZoneOffset.UTC)
lastLoginIp = loginUser.user.currentLoginIp
lastLoginTime = loginUser.user.currentLoginTime
}, KtUpdateWrapper(User()).eq(User::username, user.username))
}, KtUpdateWrapper(User()).eq(User::username, loginUser.username))
val userId = loginUser.user.id.toString()
val jwt = JwtUtil.createJwt(userId)

View File

@@ -16,8 +16,8 @@ import top.fatweb.api.service.permission.IUserService
*/
@Service
class UserDetailsServiceImpl(val userService: IUserService) : UserDetailsService {
override fun loadUserByUsername(username: String): UserDetails {
val user = userService.getUserWithPowerByUsername(username)
override fun loadUserByUsername(account: String): UserDetails {
val user = userService.getUserWithPowerByAccount(account)
user ?: let { throw Exception("Username not found") }
return LoginUser(user)

View File

@@ -61,8 +61,8 @@ class UserServiceImpl(
private val userRoleService: IUserRoleService,
private val userGroupService: IUserGroupService
) : ServiceImpl<UserMapper, User>(), IUserService {
override fun getUserWithPowerByUsername(username: String): User? {
val user = baseMapper.selectOneWithPowerInfoByUsername(username)
override fun getUserWithPowerByAccount(account: String): User? {
val user = baseMapper.selectOneWithPowerInfoByAccount(account)
user ?: let { return null }
if (user.id == 0L) {
@@ -76,7 +76,7 @@ class UserServiceImpl(
}
override fun getInfo() = WebUtil.getLoginUsername()
?.let { username -> getUserWithPowerByUsername(username)?.let { UserConverter.userToUserWithPowerInfoVo(it) } }
?.let { username -> getUserWithPowerByAccount(username)?.let { UserConverter.userToUserWithPowerInfoVo(it) } }
override fun getPage(userGetParam: UserGetParam?): PageVo<UserWithRoleInfoVo> {
val userIdsPage = Page<Long>(userGetParam?.currentPage ?: 1, userGetParam?.pageSize ?: 20)