Browse Source

add hangfire dashboard white ip address options

pull/69/head
cKey 5 years ago
parent
commit
cf827f8e1c
  1. 15
      aspnet-core/services/messages/LINGYUN.Abp.MessageService.HttpApi.Host/AbpMessageServiceHttpApiHostModule.cs
  2. 32
      aspnet-core/services/messages/LINGYUN.Abp.MessageService.HttpApi.Host/Authorization/HangfireDashboardAuthorizationFilter.cs
  3. 19
      aspnet-core/services/messages/LINGYUN.Abp.MessageService.HttpApi.Host/Hangfire/HangfireDashboardRouteOptions.cs

15
aspnet-core/services/messages/LINGYUN.Abp.MessageService.HttpApi.Host/AbpMessageServiceHttpApiHostModule.cs

@ -216,11 +216,20 @@ namespace LINGYUN.Abp.MessageService
Configure<HangfireDashboardRouteOptions>(options => Configure<HangfireDashboardRouteOptions>(options =>
{ {
if (configuration.GetSection("Hangfire:Dashboard:WhiteList").Exists())
{
options.WithWhite(
configuration["Hangfire:Dashboard:WhiteList"]
.Split(",", StringSplitOptions.RemoveEmptyEntries)
.Select(o => o.RemovePostFix("/"))
.ToArray());
}
options.WithOrigins( options.WithOrigins(
configuration["App:CorsOrigins"] configuration["App:CorsOrigins"]
.Split(",", StringSplitOptions.RemoveEmptyEntries) .Split(",", StringSplitOptions.RemoveEmptyEntries)
.Select(o => o.RemovePostFix("/")) .Select(o => o.RemovePostFix("/"))
.ToArray() .ToArray()
); );
}); });

32
aspnet-core/services/messages/LINGYUN.Abp.MessageService.HttpApi.Host/Authorization/HangfireDashboardAuthorizationFilter.cs

@ -20,12 +20,6 @@ namespace LINGYUN.Abp.MessageService.Authorization
public bool Authorize([NotNull] DashboardContext context) public bool Authorize([NotNull] DashboardContext context)
{ {
// 本地请求
if (LocalRequestOnlyAuthorize(context))
{
return true;
}
// 放行路径 // 放行路径
if (AllowGrantPath.Contains(context.Request.Path)) if (AllowGrantPath.Contains(context.Request.Path))
{ {
@ -37,6 +31,12 @@ namespace LINGYUN.Abp.MessageService.Authorization
if (options != null) if (options != null)
{ {
// 白名单检查
if (!context.Request.RemoteIpAddress.IsNullOrWhiteSpace()
&& options.IpAllow(context.Request.RemoteIpAddress))
{
return true;
}
// 请求路径对应的权限检查 // 请求路径对应的权限检查
// TODO: 怎么来传递用户身份令牌? // TODO: 怎么来传递用户身份令牌?
var permission = options.GetPermission(context.Request.Path); var permission = options.GetPermission(context.Request.Path);
@ -69,25 +69,5 @@ namespace LINGYUN.Abp.MessageService.Authorization
} }
return base.Equals(obj); return base.Equals(obj);
} }
protected virtual bool LocalRequestOnlyAuthorize(DashboardContext context)
{
if (string.IsNullOrEmpty(context.Request.RemoteIpAddress))
{
return false;
}
if (context.Request.RemoteIpAddress == "127.0.0.1" || context.Request.RemoteIpAddress == "::1")
{
return true;
}
if (context.Request.RemoteIpAddress == context.Request.LocalIpAddress)
{
return true;
}
return false;
}
} }
} }

19
aspnet-core/services/messages/LINGYUN.Abp.MessageService.HttpApi.Host/Hangfire/HangfireDashboardRouteOptions.cs

@ -1,4 +1,5 @@
using LINGYUN.Abp.MessageService.Permissions; using LINGYUN.Abp.MessageService.Permissions;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -7,12 +8,30 @@ namespace Hangfire
public class HangfireDashboardRouteOptions public class HangfireDashboardRouteOptions
{ {
public IList<string> AllowFrameOrigins { get; } public IList<string> AllowFrameOrigins { get; }
/// <summary>
/// 白名单
/// 添加网关地址
/// </summary>
public IList<string> WhiteList { get; }
public IDictionary<string, string> RoutePermissions { get; } public IDictionary<string, string> RoutePermissions { get; }
public HangfireDashboardRouteOptions() public HangfireDashboardRouteOptions()
{ {
WhiteList = new List<string>();
AllowFrameOrigins = new List<string>(); AllowFrameOrigins = new List<string>();
RoutePermissions = new Dictionary<string, string>(); RoutePermissions = new Dictionary<string, string>();
InitDefaultRoutes(); InitDefaultRoutes();
WithWhite("127.0.0.1");
WithWhite("::1");
}
public bool IpAllow(string ipaddress)
{
return WhiteList.Any(ip => ip == ipaddress);
}
public void WithWhite(params string[] wgites)
{
WhiteList.AddIfNotContains(wgites);
} }
public void WithOrigins(params string[] origins) public void WithOrigins(params string[] origins)

Loading…
Cancel
Save