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

View File

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

View File

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

View File

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

View File

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