Skip to content

Commit f7d1eac

Browse files
committed
[Fix-16627] [dolphinscheduler-api] LoginHandlerInterceptor.preHandle check session without expire time check
1 parent 4416548 commit f7d1eac

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/interceptor/LoginHandlerInterceptor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
9696
}
9797

9898
@Override
99-
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
100-
ModelAndView modelAndView) throws Exception {
99+
public void postHandle(HttpServletRequest request,
100+
HttpServletResponse response,
101+
Object handler,
102+
ModelAndView modelAndView) {
101103
ThreadLocalContext.getTimezoneThreadLocal().remove();
102104

103105
int code = response.getStatus();

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/security/impl/AbstractAuthenticator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,14 @@ public User getAuthUser(HttpServletRequest request) {
119119
sessionId = cookie.getValue();
120120
}
121121
}
122-
Session session = sessionService.getSession(sessionId);
122+
final Session session = sessionService.getSession(sessionId);
123123
if (session == null) {
124124
return null;
125125
}
126+
if (sessionService.isSessionExpire(session)) {
127+
sessionService.expireSession(session.getUserId());
128+
return null;
129+
}
126130
// get user object from session
127131
return userService.queryUser(session.getUserId());
128132
}

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/security/impl/pwd/PasswordAuthenticator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
import org.apache.dolphinscheduler.api.security.impl.AbstractAuthenticator;
2121
import org.apache.dolphinscheduler.dao.entity.User;
2222

23+
import lombok.NonNull;
24+
2325
public class PasswordAuthenticator extends AbstractAuthenticator {
2426

2527
@Override
26-
public User login(String userName, String password) {
28+
public User login(@NonNull String userName, String password) {
2729
return userService.queryUser(userName, password);
2830
}
2931
}

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/SessionServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void expireSession(Integer userId) {
9494

9595
@Override
9696
public boolean isSessionExpire(Session session) {
97-
return System.currentTimeMillis() - session.getLastLoginTime().getTime() <= Constants.SESSION_TIME_OUT * 1000;
97+
return System.currentTimeMillis() - session.getLastLoginTime().getTime() >= Constants.SESSION_TIME_OUT * 1000;
9898
}
9999

100100
}

dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/LoginControllerTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@
2727
import org.apache.dolphinscheduler.api.utils.Result;
2828
import org.apache.dolphinscheduler.common.constants.Constants;
2929
import org.apache.dolphinscheduler.common.utils.JSONUtils;
30+
import org.apache.dolphinscheduler.dao.entity.Session;
31+
import org.apache.dolphinscheduler.dao.repository.SessionDao;
3032

33+
import org.apache.http.HttpStatus;
34+
35+
import java.util.Date;
3136
import java.util.Map;
3237

3338
import javax.servlet.http.Cookie;
@@ -36,6 +41,7 @@
3641
import org.junit.jupiter.api.Test;
3742
import org.slf4j.Logger;
3843
import org.slf4j.LoggerFactory;
44+
import org.springframework.beans.factory.annotation.Autowired;
3945
import org.springframework.http.MediaType;
4046
import org.springframework.mock.web.MockHttpServletResponse;
4147
import org.springframework.test.web.servlet.MvcResult;
@@ -49,6 +55,9 @@ public class LoginControllerTest extends AbstractControllerTest {
4955

5056
private static final Logger logger = LoggerFactory.getLogger(LoginControllerTest.class);
5157

58+
@Autowired
59+
private SessionDao sessionDao;
60+
5261
@Test
5362
public void testLogin() throws Exception {
5463
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
@@ -85,6 +94,18 @@ public void testSignOut() throws Exception {
8594
logger.info(mvcResult.getResponse().getContentAsString());
8695
}
8796

97+
@Test
98+
void testSignOutWithExpireSession() throws Exception {
99+
final Session session = sessionDao.queryById(sessionId);
100+
session.setLastLoginTime(new Date(System.currentTimeMillis() - Constants.SESSION_TIME_OUT * 1000 - 1));
101+
sessionDao.updateById(session);
102+
103+
mockMvc.perform(post("/signOut")
104+
.header("sessionId", sessionId))
105+
.andExpect(status().is(HttpStatus.SC_UNAUTHORIZED))
106+
.andReturn();
107+
}
108+
88109
@Test
89110
void testClearCookie() throws Exception {
90111
MvcResult mvcResult = mockMvc.perform(delete("/cookies")

0 commit comments

Comments
 (0)