Skip to content

Commit

Permalink
更新默认租户解析器
Browse files Browse the repository at this point in the history
  • Loading branch information
UtilCore committed Sep 18, 2023
1 parent e89dc67 commit ceccb8c
Show file tree
Hide file tree
Showing 83 changed files with 657 additions and 356 deletions.
34 changes: 17 additions & 17 deletions build/Build.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(NetTargetFramework)</TargetFramework>
</PropertyGroup>

<ItemGroup>
<None Remove="common.props" />
<None Remove="icon.jpg" />
<None Remove="version.props" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FlubuCore" Version="7.0.0" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(NetTargetFramework)</TargetFramework>
</PropertyGroup>

<ItemGroup>
<None Remove="common.props" />
<None Remove="icon.jpg" />
<None Remove="version.props" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FlubuCore" Version="7.0.1" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion build/version.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<VersionMajor>7</VersionMajor>
<VersionMinor>1</VersionMinor>
<VersionPatch>22</VersionPatch>
<VersionPatch>27</VersionPatch>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Util.AspNetCore/07-Util.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.11" />
</ItemGroup>

<ItemGroup>
Expand Down
24 changes: 23 additions & 1 deletion src/Util.AspNetCore/Helpers/Web.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Security.Claims;
using System.Collections.Specialized;
using System.Security.Claims;
using Util.Http;
using Util.Security.Authentication;

Expand Down Expand Up @@ -274,6 +275,27 @@ public static string UrlDecode( string url, Encoding encoding ) {

#endregion

#region ParseQueryString(将查询字符串转换为名值对集合)

/// <summary>
/// 将查询字符串转换为名值对集合
/// </summary>
/// <param name="query">查询字符串</param>
public static NameValueCollection ParseQueryString( string query ) {
return HttpUtility.ParseQueryString( query );
}

/// <summary>
/// 将查询字符串转换为名值对集合
/// </summary>
/// <param name="query">查询字符串</param>
/// <param name="encoding">字符编码</param>
public static NameValueCollection ParseQueryString( string query, Encoding encoding ) {
return HttpUtility.ParseQueryString( query, encoding );
}

#endregion

#region DownloadAsync(下载)

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Npgsql" Version="7.0.4" />
<PackageReference Include="Npgsql" Version="7.0.6" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.11" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.11" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.11" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Util.Domain/Auditing/IAudited.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public interface IAudited : ICreationAudited, IModificationAudited {
/// <summary>
/// 操作审计
/// </summary>
/// <typeparam name="TKey">操作人编号类型</typeparam>
/// <typeparam name="TKey">操作人标识类型</typeparam>
public interface IAudited<TKey> : ICreationAudited<TKey>, IModificationAudited<TKey> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.2" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.0.0" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Util.Localization/01-Util.Localization.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Localization" Version="7.0.10" />
<PackageReference Include="Microsoft.Extensions.Localization" Version="7.0.11" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="7.1.0" />
<PackageReference Include="AspNetCore.HealthChecks.Uris" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="7.0.10" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="7.0.11" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Util.Templates.Razor/02-Util.Templates.Razor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="6.0.21" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="6.0.22" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using Util.Dependency;

namespace Util.Tenants.Resolvers;
namespace Util.Tenants;

/// <summary>
/// 基于域名的租户解析器
/// 域名租户解析器
/// </summary>
public interface IDomainTenantResolver : ITransientDependency {
/// <summary>
/// 解析租户标识
/// </summary>
/// <param name="host">域名,范例: a.test.com</param>
Task<string> ResolveTenantIdAsync( string host );
}
}
5 changes: 2 additions & 3 deletions src/Util.Tenants/AppBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ public static IAppBuilder AddTenant( this IAppBuilder builder ) {
/// <param name="setupAction">租户配置操作</param>
public static IAppBuilder AddTenant( this IAppBuilder builder, Action<TenantOptions> setupAction ) {
builder.CheckNull( nameof( builder ) );
setupAction ??= tenantOptions => tenantOptions.IsEnabled = true;
var options = new TenantOptions {
IsEnabled = true
};
setupAction.Invoke( options );
setupAction?.Invoke( options );
builder.Host.ConfigureServices( ( context, services ) => {
services.TryAddSingleton<ITenantResolver, DefaultTenantResolver>();
services.Configure( setupAction );
services.TryAddSingleton<IOptions<TenantOptions>>( new OptionsWrapper<TenantOptions>( options ) );
} );
return builder;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Util.Tenants/Resolvers/DefaultTenantResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ public DefaultTenantResolver( IOptions<TenantOptions> options ) {

/// <inheritdoc />
public async Task<string> ResolveAsync( HttpContext context ) {
if ( context == null )
return null;
if ( _options.IsEnabled == false )
return null;
var session = context.RequestServices.GetService<Util.Sessions.ISession>();
if ( session is { IsAuthenticated: true } )
return session.TenantId;
if ( _options.Resolvers == null )
return null;
foreach ( var resolver in _options.Resolvers.OrderByDescending( t => t.Priority ) ) {
var result = await resolver.ResolveAsync( context );
if ( result.IsEmpty() == false )
Expand Down
90 changes: 17 additions & 73 deletions src/Util.Tenants/Resolvers/DomainTenantResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@
/// 基于域名的租户解析器
/// </summary>
public class DomainTenantResolver : TenantResolverBase {
/// <summary>
/// 域名格式字符串
/// </summary>
private readonly string _domainFormat;
/// <summary>
/// 域名映射配置
/// </summary>
private readonly IDictionary<string, string> _domainMap;

/// <summary>
/// 初始化基于域名的租户解析器
/// </summary>
Expand All @@ -24,92 +15,45 @@ public DomainTenantResolver() {
/// </summary>
/// <param name="domainFormat">域名格式字符串,范例:{0}.a.com</param>
public DomainTenantResolver( string domainFormat ) {
_domainFormat = domainFormat;
DomainFormat = domainFormat;
}

/// <summary>
/// 初始化基于域名的租户解析器
/// </summary>
/// <param name="domainMap">域名映射配置,键为域名,值为租户标识</param>
public DomainTenantResolver( IDictionary<string, string> domainMap ) {
_domainMap = domainMap;
DomainMap = domainMap;
}

/// <summary>
/// 域名格式字符串
/// </summary>
public string DomainFormat { get; }

/// <summary>
/// 域名映射配置
/// </summary>
public IDictionary<string, string> DomainMap { get; }

/// <summary>
/// 解析租户标识
/// </summary>
protected override async Task<string> Resolve( HttpContext context ) {
var host = GetDomain( context.Request.Host.Value );
var host = DomainTenantResolverHelper.RemoveDomainPrefix( context.Request.Host.Value );
if ( host.IsEmpty() )
return null;
var result = await Resolve( context,host );
var result = await Resolve( context, host );
GetLog( context ).LogTrace( $"执行域名租户解析器,host={host},tenantId={result}" );
return result;
}

/// <summary>
/// 获取域名
/// </summary>
private string GetDomain( string domain ) {
return domain.RemoveStart( "http://" ).RemoveStart( "https://" );
}

/// <summary>
/// 解析租户标识
/// </summary>
private async Task<string> Resolve( HttpContext context, string host ) {
if ( _domainMap != null )
return ResolveByMap( host );
if ( _domainFormat.IsEmpty() == false )
return ResolveByFormat( host );
var resolver = context.RequestServices.GetService<IDomainTenantResolver>();
return resolver == null ? ResolveBySplit( host ) : await resolver.ResolveTenantIdAsync( host );
}

/// <summary>
/// 通过域名映射配置解析
/// </summary>
private string ResolveByMap( string host ) {
foreach ( var item in _domainMap ) {
if ( item.Key == host )
return item.Value;
}
return null;
}

/// <summary>
/// 通过域名格式字符串解析
/// </summary>
private string ResolveByFormat( string host ) {
var formats = GetDomainFormats();
foreach ( var format in formats ) {
if ( format.IsEmpty() )
continue;
var result = Util.Helpers.String.Extract( host, GetDomain( format ) ).FirstOrDefault().Value;
if( result.IsEmpty() == false )
return result;
}
return null;
}

/// <summary>
/// 获取域名格式列表
/// </summary>
private IEnumerable<string> GetDomainFormats() {
if ( _domainFormat.IndexOf( ",", StringComparison.Ordinal ) >= 0 )
return _domainFormat.Split( ',' );
if ( _domainFormat.IndexOf( ";", StringComparison.Ordinal ) >= 0 )
return _domainFormat.Split( ';' );
return new[] { _domainFormat };
}

/// <summary>
/// 通过拆分解析
/// </summary>
private string ResolveBySplit( string host ) {
var items = host.Split( '.' );
if ( items.Length > 2 )
return items[0];
return null;
var store = context.RequestServices.GetService<ITenantDomainStore>();
var map = await DomainTenantResolverHelper.CombineMapAsync( DomainMap,store );
return DomainTenantResolverHelper.ResolveTenantId( host, map, DomainFormat );
}
}
Loading

0 comments on commit ceccb8c

Please sign in to comment.