-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace WWB.UnifyApi.Security | ||
{ | ||
public interface IPermissionValidator | ||
{ | ||
PermissionValidResult Valid(string permission); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace WWB.UnifyApi.Security | ||
{ | ||
public class PermissionAuthorizeAttribute : TypeFilterAttribute | ||
{ | ||
public PermissionAuthorizeAttribute(string permission) : base(typeof(RequirementPermissionFilter)) | ||
{ | ||
Arguments = new object[] { permission }; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace WWB.UnifyApi.Security | ||
{ | ||
public class PermissionValidResult | ||
{ | ||
public bool IsSuccess { get; set; } | ||
public string Error { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
|
||
namespace WWB.UnifyApi.Security | ||
{ | ||
public class RequirementPermissionFilter : IAuthorizationFilter | ||
{ | ||
private readonly string _permission; | ||
|
||
public RequirementPermissionFilter(string permission) | ||
{ | ||
_permission = permission; | ||
} | ||
|
||
public void OnAuthorization(AuthorizationFilterContext context) | ||
{ | ||
var endpoint = context.HttpContext.Features.Get<IEndpointFeature>()?.Endpoint; | ||
if (endpoint != null && endpoint.Metadata.GetMetadata<AllowAnonymousAttribute>() != null) | ||
{ | ||
return; | ||
} | ||
if (!context.HttpContext.User.Identity.IsAuthenticated) | ||
{ | ||
context.Result = new StatusCodeResult(401); | ||
return; | ||
} | ||
|
||
var validator = context.HttpContext.RequestServices.GetService<IPermissionValidator>(); | ||
|
||
if (validator == null) | ||
{ | ||
throw new Exception("权限验证失败:未找到验证接口"); | ||
} | ||
|
||
var validResult = validator.Valid(_permission); | ||
if (!validResult.IsSuccess) | ||
{ | ||
throw new Exception($"权限验证失败:{validResult.Error}"); | ||
} | ||
} | ||
} | ||
} |