Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix-16627] [dolphinscheduler-api] LoginHandlerInterceptor.preHandle check session without expire time check #16989

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
public void postHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler,
ModelAndView modelAndView) {
ThreadLocalContext.getTimezoneThreadLocal().remove();

int code = response.getStatus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,14 @@ public User getAuthUser(HttpServletRequest request) {
sessionId = cookie.getValue();
}
}
Session session = sessionService.getSession(sessionId);
final Session session = sessionService.getSession(sessionId);
if (session == null) {
return null;
}
if (sessionService.isSessionExpire(session)) {
sessionService.expireSession(session.getUserId());
return null;
}
// get user object from session
return userService.queryUser(session.getUserId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import org.apache.dolphinscheduler.api.security.impl.AbstractAuthenticator;
import org.apache.dolphinscheduler.dao.entity.User;

import lombok.NonNull;

public class PasswordAuthenticator extends AbstractAuthenticator {

@Override
public User login(String userName, String password) {
public User login(@NonNull String userName, String password) {
return userService.queryUser(userName, password);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void expireSession(Integer userId) {

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.constants.Constants;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.dao.entity.Session;
import org.apache.dolphinscheduler.dao.repository.SessionDao;

import org.apache.http.HttpStatus;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.Cookie;
Expand All @@ -36,6 +41,7 @@
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MvcResult;
Expand All @@ -49,6 +55,9 @@ public class LoginControllerTest extends AbstractControllerTest {

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

@Autowired
private SessionDao sessionDao;

@Test
public void testLogin() throws Exception {
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
Expand Down Expand Up @@ -85,6 +94,18 @@ public void testSignOut() throws Exception {
logger.info(mvcResult.getResponse().getContentAsString());
}

@Test
void testSignOutWithExpireSession() throws Exception {
final Session session = sessionDao.queryById(sessionId);
session.setLastLoginTime(new Date(System.currentTimeMillis() - Constants.SESSION_TIME_OUT * 1000 - 1));
sessionDao.updateById(session);

mockMvc.perform(post("/signOut")
.header("sessionId", sessionId))
.andExpect(status().is(HttpStatus.SC_UNAUTHORIZED))
.andReturn();
}

@Test
void testClearCookie() throws Exception {
MvcResult mvcResult = mockMvc.perform(delete("/cookies")
Expand Down
Loading