@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        ApiServerMetrics.incApiRequestCount();

        // get token
        String token = request.getHeader("token");
        User user;
        if (StringUtils.isEmpty(token)) {
            user = authenticator.getAuthUser(request);
            // if user is null
            if (user == null) {
                response.setStatus(HttpStatus.SC_UNAUTHORIZED);
                log.info("user does not exist");
                return false;
            }
        } else {
            user = userMapper.queryUserByToken(token, new Date());
            if (user == null) {
                response.setStatus(HttpStatus.SC_UNAUTHORIZED);
                log.info("user token has expired");
                return false;
            }
        }

        // check user state
        if (user.getState() == Flag.NO.ordinal()) {
            response.setStatus(HttpStatus.SC_UNAUTHORIZED);
            log.info(Status.USER_DISABLED.getMsg());
            return false;
        }
        request.setAttribute(Constants.SESSION_USER, user);
        ThreadLocalContext.getTimezoneThreadLocal().set(user.getTimeZone());
        return true;
    }
@Test
    public void testPreHandle() {
        HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
        HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
        // test no token and no cookie
        Assertions.assertFalse(interceptor.preHandle(request, response, null));

        User mockUser = new User();
        mockUser.setId(1);
        mockUser.setUserType(UserType.GENERAL_USER);
        mockUser.setState(1);

        // test no token
        when(authenticator.getAuthUser(request)).thenReturn(mockUser);
        Assertions.assertTrue(interceptor.preHandle(request, response, null));

        // test token
        String token = "123456";
        when(request.getHeader("token")).thenReturn(token);
        when(userMapper.queryUserByToken(eq(token), any(Date.class))).thenReturn(mockUser);
        Assertions.assertTrue(interceptor.preHandle(request, response, null));

        // test disable user
        mockUser.setState(0);
        when(authenticator.getAuthUser(request)).thenReturn(mockUser);
        Assertions.assertFalse(interceptor.preHandle(request, response, null));
    }