cn.herodotus.engine.rest.protect.tenant.MultiTenantInterceptor Maven / Gradle / Ivy
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2020-2030 郑庚伟 ZHENGGENGWEI (码匠君), Licensed under the AGPL License
*
* This file is part of Herodotus Engine.
*
* Herodotus Engine is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Herodotus Engine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package cn.herodotus.engine.rest.protect.tenant;
import cn.herodotus.engine.assistant.definition.constants.DefaultConstants;
import cn.herodotus.engine.assistant.core.context.TenantContextHolder;
import cn.herodotus.engine.assistant.core.utils.http.HeaderUtils;
import cn.herodotus.engine.assistant.core.utils.http.SessionUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
/**
* Description: 多租户拦截器
*
* @author : gengwei.zheng
* @date : 2022/9/6 11:16
*/
public class MultiTenantInterceptor implements HandlerInterceptor {
private static final Logger log = LoggerFactory.getLogger(MultiTenantInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String tenantId = HeaderUtils.getHerodotusTenantId(request);
if (StringUtils.isBlank(tenantId)) {
tenantId = DefaultConstants.TENANT_ID;
}
TenantContextHolder.setTenantId(tenantId);
log.debug("[Herodotus] |- TENANT ID is : [{}].", tenantId);
String path = request.getRequestURI();
String sessionId = SessionUtils.getSessionId(request);
String herodotusSessionId = HeaderUtils.getHerodotusSessionId(request);
log.debug("[Herodotus] |- SESSION ID for [{}] is : [{}].", path, sessionId);
log.debug("[Herodotus] |- SESSION ID of HERODOTUS for [{}] is : [{}].", path, herodotusSessionId);
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
String path = request.getRequestURI();
TenantContextHolder.clear();
log.debug("[Herodotus] |- Tenant Interceptor CLEAR tenantId for request [{}].", path);
}
}