REM:
1. 修正登录时token超时判断不正常导致token无法正常更新问题
This commit is contained in:
HuangXin 2020-09-13 09:54:35 +08:00
parent 641b694541
commit 321379873e
5 changed files with 30 additions and 27 deletions

View File

@ -129,14 +129,15 @@ public class UserAccountManagerImpl implements UserAccountManager {
}
// 更新访问时间
user.setLastAccess(userAccountMapper.upgradeLastAccessTime(username));
userAccountMapper.upgradeLastAccessTime(username);
user.setLastAccess(Helper.getCurrentDatetime());
} else {
// 调用函数前确保用户一定存在
user = userAccountMapper.getUserByName(username);
// 超时或者以前未登录
if (tokenTimeout(user.getToken()) || user.getToken().length() == 0) {
if (tokenTimeout(user.getLastAccess()) || user.getToken().length() == 0) {
token = createUserToken(username);
userAccountMapper.upgradeToken(username, token);
log.info("Refresh {} token {} at {}", username, user.getToken(), user.getLastAccess());
@ -145,8 +146,10 @@ public class UserAccountManagerImpl implements UserAccountManager {
log.info("Get {} token:{}", username, token);
}
userAccountMapper.upgradeLastAccessTime(username);
// 更新访问时间
user.setLastAccess(userAccountMapper.upgradeLastAccessTime(username));
user.setLastAccess(Helper.getCurrentDatetime());
// 更新用户token
user.setToken(token);
@ -254,7 +257,8 @@ public class UserAccountManagerImpl implements UserAccountManager {
return ErrorCode.ERR_TOKENTIMEOUT;
} else {
// 更新用户最后一次访问时间戳
user.setLastAccess(userAccountMapper.upgradeLastAccessTime(user.getUsername()));
userAccountMapper.upgradeLastAccessTime(user.getUsername());
user.setLastAccess(Helper.getCurrentDatetime());
// 缓存当前用户token
userAccountCache.put(token, user);
@ -270,7 +274,8 @@ public class UserAccountManagerImpl implements UserAccountManager {
return ErrorCode.ERR_TOKENTIMEOUT;
} else {
// 更新用户最后一次访问时间戳
user.setLastAccess(userAccountMapper.upgradeLastAccessTime(user.getUsername()));
userAccountMapper.upgradeLastAccessTime(user.getUsername());
user.setLastAccess(Helper.getCurrentDatetime());
return ErrorCode.ERR_OK;
}
@ -449,7 +454,7 @@ public class UserAccountManagerImpl implements UserAccountManager {
return (System.currentTimeMillis() - Helper.getTimestampMilliSecond(lastAccess))
>= AuthConfigValue.TOKEN_EXPIRED_TIME_MS;
} catch (Exception ex) {
return false;
return true;
}
}
}

View File

@ -87,12 +87,11 @@ public interface UserAccountMapper {
void upgradeLoginTime(@Param("username") String username);
/**
* Upgrade last access time string.
* Upgrade last access time.
*
* @param username the username
* @return the string
*/
String upgradeLastAccessTime(@Param("username") String username);
void upgradeLastAccessTime(@Param("username") String username);
/**
* Upgrade token.

View File

@ -61,7 +61,6 @@ public class UserAccountServiceImpl implements UserAccountService {
return new MulReturnType<>(ErrorCode.ERR_UNSUPPORT, "");
}
userAccountManager.upgradeLoginTime(username);
UserAccount loginUser = userAccountManager.getUserByName(username);
// 该用户是否存在
@ -100,9 +99,12 @@ public class UserAccountServiceImpl implements UserAccountService {
}
}
return MulReturnType.<ErrorCode, String>builder()
.firstParam(ErrorCode.ERR_OK)
.secondParam(userAccountManager.getUserToken(username)).build();
String token = userAccountManager.getUserToken(username);
// 刷新用户登录时间
userAccountManager.upgradeLoginTime(username);
return new MulReturnType<>(ErrorCode.ERR_OK, token);
}
/**
@ -181,10 +183,8 @@ public class UserAccountServiceImpl implements UserAccountService {
.firstParam(err).build();
}
return MulReturnType.<ErrorCode, UserAccount>builder()
.firstParam(ErrorCode.ERR_OK)
.secondParam(userAccountManager.createUserAccount(username, password, optUser.getUsername()))
.build();
return new MulReturnType<>(ErrorCode.ERR_OK, userAccountManager.createUserAccount(username, password,
optUser.getUsername()));
}
/**

View File

@ -49,12 +49,12 @@
AND LENGTH(#{token}) > 0
</select>
<select id="upgradeLastAccessTime" resultType="java.lang.String">
<update id="upgradeLastAccessTime" >
UPDATE
user_account
SET lastAccess = CURRENT_TIMESTAMP
WHERE username = #{username, jdbcType=VARCHAR};
</select>
</update>
<update id="upgradeLoginTime">
UPDATE

View File

@ -1,6 +1,5 @@
package com.dispose.test.dev.mapper;
import com.dispose.common.Helper;
import com.dispose.common.ObjectStatus;
import com.dispose.mapper.UserAccountMapper;
import com.dispose.pojo.entity.UserAccount;
@ -117,13 +116,13 @@ public class UserAccountMapperTest {
@Test
public void a5_upgradeUserAccess() {
String dt = userAccountMapper.upgradeLastAccessTime("admin");
log.debug("Upgrade datetime: {}", dt);
if(dt!=null){
Assert.assertTrue(Helper.getTimestampDiffNow(dt) < 2);
}
// String dt = userAccountMapper.upgradeLastAccessTime("admin");
//
// log.debug("Upgrade datetime: {}", dt);
//
// if(dt!=null){
// Assert.assertTrue(Helper.getTimestampDiffNow(dt) < 2);
// }
}
@Test