一、Abp后台配置
1、在Admin.Application.Contracts配制权限常量
public static class DemoPermissions
{
public const string BaseService = "Demo";
public static class Customer
{
public const string Default = BaseService + ".Customer";
public const string Delete = Default + ".Delete";
public const string Update = Default + ".Update";
public const string Create = Default + ".Create";
}
}
2、继承abp的PermissionDefinitionProvider类添加权限
public class DemoPermissionDefinitionProvider : PermissionDefinitionProvider
{
public override void Define(IPermissionDefinitionContext context)
{
var business = context.AddGroup(DemoPermissions.BaseService, L(DemoPermissions.BaseService), MultiTenancySides.Tenant);
var Customer = business.AddPermission(DemoPermissions.Customer.Default, L("Customer"));
Customer.AddChild(DemoPermissions.Customer.Update, L("Edit"));
Customer.AddChild(DemoPermissions.Customer.Delete, L("Delete"));
Customer.AddChild(DemoPermissions.Customer.Create, L("Create"));
}
private static LocalizableString L(string name)
{
return LocalizableString.Create<DemoResource>(name);
}
}
3.在Admin.Application中的业务类中添加权限特性
[Authorize(DemoPermissions.Customer.Default)]
public class CustomerAppService : ApplicationService, ICustomerAppService
{
[Authorize(DemoPermissions.Customer.Create)]
public async Task<CustomerDto> CreateAsync(CreateUpdateCustomerDto input)
{
//...
}
//忽略权限验证,匿名访问
[AllowAnonymous]
public async Task<CustomerDto> GetAsync(long? id)
{
//...
}
}
二、Vben前端,在Routes中配置页面权限
const setup: AppRouteModule = {
path: '',
name: '',
component: LAYOUT,
redirect: '',
meta: {
orderNo: 90000,
hideChildrenInMenu: true,
icon: 'ant-design:user-outlined',
title: '',
},
children: [
{
path: '',
name: '',
component: () => import(''),
meta: {
title: t('会员信息'),
icon: 'ant-design:user-outlined',
hideMenu: true,
ignoreKeepAlive: true,
roles: ['Demo.Customer.MemberInfo'], //设置页面对应的权限
},
},
],
};
三、在后台给角色配置会员权限,然后强制刷新浏览器,权限就生效了