LoginHandlerInterceptor.java 3.9 KB
Newer Older
L
ligang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Q
qiaozhanwei 已提交
17
package org.apache.dolphinscheduler.api.interceptor;
L
ligang 已提交
18

R
Rubik-W 已提交
19
import org.apache.dolphinscheduler.api.enums.Status;
20
import org.apache.dolphinscheduler.api.security.Authenticator;
Q
qiaozhanwei 已提交
21
import org.apache.dolphinscheduler.api.service.SessionService;
22
import org.apache.dolphinscheduler.common.Constants;
R
Rubik-W 已提交
23
import org.apache.dolphinscheduler.common.enums.Flag;
Q
qiaozhanwei 已提交
24 25
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.mapper.UserMapper;
L
ligang 已提交
26
import org.apache.commons.httpclient.HttpStatus;
Q
qiaozhanwei 已提交
27
import org.apache.commons.lang.StringUtils;
L
ligang 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * login interceptor, must login first
 */
public class LoginHandlerInterceptor implements HandlerInterceptor {
  private static final Logger logger = LoggerFactory.getLogger(LoginHandlerInterceptor.class);

  @Autowired
  private SessionService sessionService;

  @Autowired
  private UserMapper userMapper;

48 49 50
  @Autowired
  private Authenticator authenticator;

L
ligang 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  /**
   * Intercept the execution of a handler. Called after HandlerMapping determined
   * an appropriate handler object, but before HandlerAdapter invokes the handler.
   * <p>DispatcherServlet processes a handler in an execution chain, consisting
   * of any number of interceptors, with the handler itself at the end.
   * With this method, each interceptor can decide to abort the execution chain,
   * typically sending a HTTP error or writing a custom response.
   * <p><strong>Note:</strong> special considerations apply for asynchronous
   * request processing. For more details see
   * {@link org.springframework.web.servlet.AsyncHandlerInterceptor}.
   * @param request current HTTP request
   * @param response current HTTP response
   * @param handler chosen handler to execute, for type and/or instance evaluation
   * @return {@code true} if the execution chain should proceed with the
   * next interceptor or the handler itself. Else, DispatcherServlet assumes
   * that this interceptor has already dealt with the response itself.
   */
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

Q
qiaozhanwei 已提交
71 72 73 74
    // get token
    String token = request.getHeader("token");
    User user = null;
    if (StringUtils.isEmpty(token)){
75
      user = authenticator.getAuthUser(request);
Q
qiaozhanwei 已提交
76 77 78 79 80 81
      // if user is null
      if (user == null) {
        response.setStatus(HttpStatus.SC_UNAUTHORIZED);
        logger.info("user does not exist");
        return false;
      }
Q
qiaozhanwei 已提交
82 83
    }else {
       user = userMapper.queryUserByToken(token);
Q
qiaozhanwei 已提交
84 85 86 87 88
      if (user == null) {
        response.setStatus(HttpStatus.SC_UNAUTHORIZED);
        logger.info("user token has expired");
        return false;
      }
L
ligang 已提交
89
    }
R
Rubik-W 已提交
90 91 92 93 94 95 96 97

    // check user state
    if (user.getState() == Flag.NO.ordinal()) {
      response.setStatus(HttpStatus.SC_UNAUTHORIZED);
      logger.info(Status.USER_DISABLED.getMsg());
      return false;
    }

L
ligang 已提交
98 99 100 101 102
    request.setAttribute(Constants.SESSION_USER, user);
    return true;
  }

}