|
@ -16,14 +16,29 @@ import javax.servlet.http.HttpSession;
|
|
|
*/
|
|
|
public class WebMvcInterceptor extends HandlerInterceptorAdapter {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
|
|
throws Exception {
|
|
|
HttpSession session = request.getSession();
|
|
|
String servletPath = request.getServletPath();
|
|
|
TenantSession tenantSession = (TenantSession) session.getAttribute(ContextAttributes.TENANT_SESSION);
|
|
|
if (tenantSession!=null) {
|
|
|
LocalContext.getContext().setAttachment(ContextAttributes.SCHEMA, tenantSession.getSchema());
|
|
|
LocalContext.getContext().setAttachment(ContextAttributes.TENANT_NAME, tenantSession.getTenant());
|
|
|
if (servletPath.contains("/tenant/")){
|
|
|
LocalContext.getContext().setAttachment(ContextAttributes.SCHEMA, ContextAttributes.GLOBAL_DB);
|
|
|
}else {
|
|
|
if (ContextAttributes.GLOBAL_DB.equals(LocalContext.getContext().getAttachment(ContextAttributes.SCHEMA))){
|
|
|
//切换到租户对应数据库
|
|
|
String userSchema = LocalContext.getContext().getAttachment(ContextAttributes.SCHEMA_TEMP);
|
|
|
LocalContext.getContext().setAttachment(ContextAttributes.SCHEMA, userSchema);
|
|
|
}else {
|
|
|
LocalContext.getContext().setAttachment(ContextAttributes.SCHEMA, tenantSession.getSchema());
|
|
|
LocalContext.getContext().setAttachment(ContextAttributes.TENANT_NAME, tenantSession.getTenant());
|
|
|
LocalContext.getContext().setAttachment(ContextAttributes.SCHEMA_TEMP, tenantSession.getSchemaTemp());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
return true;
|
|
|
}
|