From bb30b88306e33fde05fb72b74b23597c5fe1f277 Mon Sep 17 00:00:00 2001 From: longtb <67264931+longtb976@users.noreply.github.com> Date: Mon, 23 May 2022 09:54:11 +0800 Subject: [PATCH] [Fix-10076] queryUserByToken use server timezone (#10177) * [Fix-10076] queryUserByToken use server timezone * [Fix-10076] queryUserByToken use server timezone (cherry picked from commit bd34e66b8a493a2aa14af5884f2f7c13819004f2) --- .../api/interceptor/LoginHandlerInterceptor.java | 4 +++- .../api/interceptor/LoginHandlerInterceptorTest.java | 6 +++++- .../org/apache/dolphinscheduler/dao/mapper/UserMapper.java | 6 ++++-- .../org/apache/dolphinscheduler/dao/mapper/UserMapper.xml | 2 +- .../apache/dolphinscheduler/dao/mapper/UserMapperTest.java | 2 +- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/interceptor/LoginHandlerInterceptor.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/interceptor/LoginHandlerInterceptor.java index 2013348ed8..243928960b 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/interceptor/LoginHandlerInterceptor.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/interceptor/LoginHandlerInterceptor.java @@ -37,6 +37,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; +import java.util.Date; + /** * login interceptor, must log in first */ @@ -71,7 +73,7 @@ public class LoginHandlerInterceptor implements HandlerInterceptor { return false; } } else { - user = userMapper.queryUserByToken(token); + user = userMapper.queryUserByToken(token, new Date()); if (user == null) { response.setStatus(HttpStatus.SC_UNAUTHORIZED); logger.info("user token has expired"); diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/interceptor/LoginHandlerInterceptorTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/interceptor/LoginHandlerInterceptorTest.java index 67d34192d4..467cfa0e07 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/interceptor/LoginHandlerInterceptorTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/interceptor/LoginHandlerInterceptorTest.java @@ -17,6 +17,8 @@ package org.apache.dolphinscheduler.api.interceptor; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; import org.apache.dolphinscheduler.api.ApiApplicationServer; @@ -45,6 +47,8 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.transaction.annotation.Transactional; +import java.util.Date; + @ActiveProfiles(value = {ProfileType.H2}) @RunWith(SpringRunner.class) @SpringBootTest(classes = ApiApplicationServer.class) @@ -81,7 +85,7 @@ public class LoginHandlerInterceptorTest { // test token String token = "123456"; when(request.getHeader("token")).thenReturn(token); - when(userMapper.queryUserByToken(token)).thenReturn(mockUser); + when(userMapper.queryUserByToken(eq(token), any(Date.class))).thenReturn(mockUser); Assert.assertTrue(interceptor.preHandle(request, response, null)); // test disable user diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/UserMapper.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/UserMapper.java index bd611417c1..d31f4b460b 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/UserMapper.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/UserMapper.java @@ -21,6 +21,7 @@ import org.apache.dolphinscheduler.dao.entity.User; import org.apache.ibatis.annotations.Param; +import java.util.Date; import java.util.List; import org.springframework.cache.annotation.CacheConfig; @@ -83,7 +84,7 @@ public interface UserMapper extends BaseMapper { /** * user page * - * @param page page + * @param page page * @param userName userName * @return user IPage */ @@ -126,9 +127,10 @@ public interface UserMapper extends BaseMapper { * query user by token * * @param token token + * @param now now date * @return user */ - User queryUserByToken(@Param("token") String token); + User queryUserByToken(@Param("token") String token, @Param("now") Date now); /** * query user by queue name diff --git a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/UserMapper.xml b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/UserMapper.xml index b105679970..e38688939c 100644 --- a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/UserMapper.xml +++ b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/UserMapper.xml @@ -97,7 +97,7 @@ from t_ds_user u ,t_ds_access_token t - where u.id = t.user_id and token=#{token} and t.expire_time > NOW() + where u.id = t.user_id and token=#{token} and t.expire_time > #{now}