-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
21 changed files
with
760 additions
and
18 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
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,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<Controller_SelectedScaffolderID>ApiControllerEmptyScaffolder</Controller_SelectedScaffolderID> | ||
<Controller_SelectedScaffolderCategoryPath>root/Controller</Controller_SelectedScaffolderCategoryPath> | ||
<WebStackScaffolding_ControllerDialogWidth>600</WebStackScaffolding_ControllerDialogWidth> | ||
<WebStackScaffolding_IsLayoutPageSelected>True</WebStackScaffolding_IsLayoutPageSelected> | ||
<WebStackScaffolding_IsPartialViewSelected>False</WebStackScaffolding_IsPartialViewSelected> | ||
<WebStackScaffolding_IsReferencingScriptLibrariesSelected>True</WebStackScaffolding_IsReferencingScriptLibrariesSelected> | ||
<WebStackScaffolding_LayoutPageFile /> | ||
<WebStackScaffolding_IsAsyncSelected>False</WebStackScaffolding_IsAsyncSelected> | ||
</PropertyGroup> | ||
</Project> |
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,68 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using April.Util; | ||
using April.Util.Config; | ||
using April.Util.Entities.Admin; | ||
using April.Util.Entities.Request; | ||
using April.Util.Entities.Response; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace April.Simple.WebApi.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class LoginController : ControllerBase | ||
{ | ||
[HttpPost] | ||
public async Task<ResponseDataEntity> Login(LoginFormEntity formEntity) | ||
{ | ||
if (string.IsNullOrEmpty(formEntity.LoginName) || string.IsNullOrEmpty(formEntity.Password)) | ||
{ | ||
return ResponseUtil.Fail("请输入账号密码"); | ||
} | ||
if (formEntity.LoginName == "admin") | ||
{ | ||
//这里实际应该通过db获取管理员 | ||
string password = EncryptUtil.MD5Encrypt(formEntity.Password, AprilConfig.SecurityKey); | ||
if (password == "B092956160CB0018") | ||
{ | ||
//获取管理员相关权限,同样是db获取,这里只做展示 | ||
AdminEntity admin = new AdminEntity | ||
{ | ||
UserName = "超级管理员", | ||
Avator = "", | ||
IsSuperManager = true, | ||
TokenType = (int)AprilEnums.TokenType.Web | ||
}; | ||
string token = TokenUtil.GetToken(admin, out string expiretimestamp); | ||
int expiretime = 0; | ||
int.TryParse(expiretimestamp, out expiretime); | ||
//可以考虑记录登录日志等其他信息 | ||
return ResponseUtil.Success("", new { username = admin.UserName, avator = admin.Avator, token = token, expire = expiretime }); | ||
} | ||
} | ||
else if (formEntity.LoginName == "test") | ||
{ | ||
//这里做权限演示 | ||
AdminEntity admin = new AdminEntity | ||
{ | ||
UserName = "测试", | ||
Avator = "", | ||
TokenType = (int)AprilEnums.TokenType.Web | ||
}; | ||
admin.Controllers.Add("weatherforecast"); | ||
admin.Permissions.Add("weatherforecast_log");//控制器_事件(Add,Update...) | ||
string token = TokenUtil.GetToken(admin, out string expiretimestamp); | ||
int expiretime = 0; | ||
int.TryParse(expiretimestamp, out expiretime); | ||
//可以考虑记录登录日志等其他信息 | ||
return ResponseUtil.Success("", new { username = admin.UserName, avator = admin.Avator, token = token, expire = expiretime }); | ||
} | ||
//这里其实已经可以考虑验证码相关了,但是这是示例工程,后续可持续关注我,会有基础工程(带权限)的实例公开 | ||
return ResponseUtil.Fail("账号密码错误"); | ||
} | ||
} | ||
} |
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
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
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
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,45 @@ | ||
using April.Util.Entities.Admin; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using System; | ||
|
||
namespace April.Util.Aop | ||
{ | ||
public class AprilPermissionAttribute : Attribute, IActionFilter | ||
{ | ||
|
||
public string Permission; | ||
public string Controller; | ||
/// <summary> | ||
/// 构造函数 | ||
/// </summary> | ||
/// <param name="_controller">控制器</param> | ||
/// <param name="_permission">接口事件</param> | ||
public AprilPermissionAttribute(string _controller, string _permission) | ||
{ | ||
Permission = _permission; | ||
Controller = _controller; | ||
} | ||
|
||
public void OnActionExecuted(ActionExecutedContext context) | ||
{ | ||
LogUtil.Debug("AprilPermission OnActionExecuted"); | ||
} | ||
public void OnActionExecuting(ActionExecutingContext context) | ||
{ | ||
AdminEntity admin = TokenUtil.GetUserByToken(); | ||
if (admin == null || admin.ExpireTime <= DateTime.Now) | ||
{ | ||
context.Result = new ObjectResult(new { msg = "未登录", code = -2 }); | ||
} | ||
if (!admin.IsSuperManager) | ||
{ | ||
string controller_permission = $"{Controller}_{Permission}"; | ||
if (!admin.Controllers.Contains(Controller) || !admin.Permissions.Contains(controller_permission)) | ||
{ | ||
context.Result = new ObjectResult(new { msg = "无权访问", code = 401 }); | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.