12 changed files with 254 additions and 17 deletions
@ -0,0 +1,55 @@ |
|||||
|
using LINGYUN.Abp.MessageService.Permissions; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
|
||||
|
namespace Hangfire |
||||
|
{ |
||||
|
public class HangfireDashboardRouteOptions |
||||
|
{ |
||||
|
public IList<string> AllowFrameOrigins { get; } |
||||
|
public IDictionary<string, string> RoutePermissions { get; } |
||||
|
public HangfireDashboardRouteOptions() |
||||
|
{ |
||||
|
AllowFrameOrigins = new List<string>(); |
||||
|
RoutePermissions = new Dictionary<string, string>(); |
||||
|
InitDefaultRoutes(); |
||||
|
} |
||||
|
|
||||
|
public void WithOrigins(params string[] origins) |
||||
|
{ |
||||
|
AllowFrameOrigins.AddIfNotContains(origins); |
||||
|
} |
||||
|
|
||||
|
public void WithPermission(string route, string permission) |
||||
|
{ |
||||
|
RoutePermissions.Add(route, permission); |
||||
|
} |
||||
|
|
||||
|
public string GetPermission(string route) |
||||
|
{ |
||||
|
var permission = RoutePermissions |
||||
|
.Where(x => x.Key.StartsWith(route)) |
||||
|
.Select(x => x.Value) |
||||
|
.FirstOrDefault(); |
||||
|
|
||||
|
return permission; |
||||
|
} |
||||
|
|
||||
|
private void InitDefaultRoutes() |
||||
|
{ |
||||
|
WithPermission("/hangfire", AbpMessageServicePermissions.Hangfire.Default); |
||||
|
WithPermission("/stats", AbpMessageServicePermissions.Hangfire.Default); |
||||
|
WithPermission("/servers", AbpMessageServicePermissions.Hangfire.Default); |
||||
|
WithPermission("/retries", AbpMessageServicePermissions.Hangfire.Default); |
||||
|
WithPermission("/recurring", AbpMessageServicePermissions.Hangfire.Default); |
||||
|
WithPermission("/jobs/enqueued", AbpMessageServicePermissions.Hangfire.ManageQueue); |
||||
|
WithPermission("/jobs/processing", AbpMessageServicePermissions.Hangfire.ManageQueue); |
||||
|
WithPermission("/jobs/scheduled", AbpMessageServicePermissions.Hangfire.ManageQueue); |
||||
|
WithPermission("/jobs/failed", AbpMessageServicePermissions.Hangfire.ManageQueue); |
||||
|
WithPermission("/jobs/deleted", AbpMessageServicePermissions.Hangfire.ManageQueue); |
||||
|
WithPermission("/jobs/awaiting", AbpMessageServicePermissions.Hangfire.ManageQueue); |
||||
|
WithPermission("/jobs/actions", AbpMessageServicePermissions.Hangfire.ManageQueue); |
||||
|
WithPermission("/jobs/details", AbpMessageServicePermissions.Hangfire.ManageQueue); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
using Microsoft.AspNetCore.Http; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Hangfire |
||||
|
{ |
||||
|
public class HangfireJwtTokenMiddleware : IMiddleware, ITransientDependency |
||||
|
{ |
||||
|
public async Task InvokeAsync(HttpContext context, RequestDelegate next) |
||||
|
{ |
||||
|
// 通过 iframe 加载页面的话,需要手动传递 access_token 到参数列表
|
||||
|
if (context.Request.Path.StartsWithSegments("/hangfire") && context.User.Identity?.IsAuthenticated != true) |
||||
|
{ |
||||
|
if (context.Request.Query.TryGetValue("access_token", out var accessTokens)) |
||||
|
{ |
||||
|
context.Request.Headers.Add("Authorization", accessTokens); |
||||
|
} |
||||
|
var options = context.RequestServices.GetService<IOptions<HangfireDashboardRouteOptions>>()?.Value; |
||||
|
if (options != null && options.AllowFrameOrigins.Count > 0) |
||||
|
{ |
||||
|
// 跨域 iframe
|
||||
|
context.Response.Headers.TryAdd("X-Frame-Options", $"\"ALLOW-FROM {options.AllowFrameOrigins.JoinAsString(",")}\""); |
||||
|
} |
||||
|
} |
||||
|
await next(context); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,49 @@ |
|||||
|
<template> |
||||
|
<div class="dashboard-container"> |
||||
|
<iframe |
||||
|
id="iframe-hangifre" |
||||
|
ref="hangfireIframe" |
||||
|
:src="hangfireUrl" |
||||
|
scrolling="auto" |
||||
|
frameborder="0" |
||||
|
class="iframe-hangifre" |
||||
|
/> |
||||
|
</div> |
||||
|
</template> |
||||
|
|
||||
|
<script lang="ts"> |
||||
|
import { Component, Prop, Vue } from 'vue-property-decorator' |
||||
|
|
||||
|
@Component({ |
||||
|
name: 'HangfireDashboard' |
||||
|
}) |
||||
|
export default class extends Vue { |
||||
|
@Prop({ default: '' }) |
||||
|
private hangfireUrl!: string |
||||
|
|
||||
|
get showIframe() { |
||||
|
if (this.hangfireUrl) { |
||||
|
return true |
||||
|
} |
||||
|
return false |
||||
|
} |
||||
|
|
||||
|
mounted() { |
||||
|
const hangfireFrame = this.$refs.hangfireIframe as any |
||||
|
if (hangfireFrame) { |
||||
|
const deviceWidth = document.documentElement.clientWidth |
||||
|
const deviceHeight = document.documentElement.clientHeight |
||||
|
hangfireFrame.style.width = deviceWidth + 'px' |
||||
|
hangfireFrame.style.height = deviceHeight + 'px' |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
</script> |
||||
|
|
||||
|
<style lang="scss" scoped> |
||||
|
.iframe-hangifre { |
||||
|
width: 100%; |
||||
|
height: 100%; |
||||
|
overflow: hidden; |
||||
|
} |
||||
|
</style> |
||||
Loading…
Reference in new issue