This commit is contained in:
ky_sunl
2021-04-01 06:47:58 +00:00
parent 687b79910e
commit cb7e07922f
41 changed files with 881 additions and 88 deletions

View File

@@ -32,8 +32,16 @@ namespace Ewide.Core.WebApi
config.Services.Replace(typeof(IHttpActionInvoker), new HttpWebApiControllerActionInvoker(config));
#endregion
// 接口权限
config.Filters.Add(new ApiAuthorizeAttribute());
// 接口参数验证
config.Filters.Add(new ValidateArgumentsFilter());
#if DEBUG
// 允许跨域
config.EnableCors(new System.Web.Http.Cors.EnableCorsAttribute("*", "*", "*"));
#endif
}
}
}

View File

@@ -1,38 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace Ewide.Core.WebApi.Areas.Base.Controllers
{
[ValidateArgumentsFilter]
public class BetestController : BaseController
{
/// <summary>
/// 获取所有接口及其注释
/// </summary>
/// <returns></returns>
[Route("test22")]
[HttpPost]
public IHttpActionResult Test1()
{
var apis = Configuration.Services.GetApiExplorer().ApiDescriptions;
var result = apis
.Select(p => new
{
p.RelativePath,
p.Documentation
});
return DisplayJSON(result);
}
[HttpPost]
public IHttpActionResult Test2()
{
return DisplayJSON("");
}
}
}

View File

@@ -1,4 +1,6 @@
using Ewide.Core.DTO;
using Ewide.Core.Common;
using Ewide.Core.DTO;
using Ewide.Core.Service;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -15,11 +17,24 @@ namespace Ewide.Core.WebApi.Areas.Base.Controllers
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
[ApiAuthorize(VerifyAuthorization = false)]
[AllowAnonymous]
[HttpPost]
public IHttpActionResult Login(LoginDTO dto)
{
return DisplayJSON(dto);
var info = new LoginHelper().Login(dto.Account, dto.Password, out string token, out string message);
if(info == null)
{
return DisplayErrorJSON(message);
}
else
{
return DisplaySuccessJSON(new
{
Token = token,
Message = message,
Info = info
});
}
}
[ValidateArgumentsFilter(AllowNull = true)]
@@ -28,5 +43,18 @@ namespace Ewide.Core.WebApi.Areas.Base.Controllers
{
return DisplayJSON(dto);
}
/// <summary>
/// 测试创建帐号
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
[HttpPost]
public IHttpActionResult CreateAccount(CreateUserDTO dto)
{
var service = new GateService();
var result = service.CreateAccount(dto.Account, dto.Password, dto.Name);
return DisplayJSON(String.IsNullOrEmpty(service.ErrorMessage) ? result : service.ErrorMessage);
}
}
}

View File

@@ -0,0 +1,19 @@
using Ewide.Core.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace Ewide.Core.WebApi.Areas.Base.Controllers
{
public class MenuController : BaseController
{
[HttpPost]
public IHttpActionResult Get()
{
return DisplaySuccessJSON(new MenuHelper().GetMenu());
}
}
}

View File

@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace Ewide.Core.WebApi.Areas.Base.Controllers
{
public class SpareController : BaseController
{
/// <summary>
/// 获取所有接口及其注释
/// </summary>
/// <returns></returns>
[HttpPost]
public IHttpActionResult GetApis()
{
var apis = Configuration.Services.GetApiExplorer().ApiDescriptions;
var result = apis
.ToLookup(p => p.ActionDescriptor.ControllerDescriptor)
.Select(p => new
{
Group = p.Key.ControllerName.ToLower(),
Apis = p.Select(q => new
{
RelativePath = "/" + q.RelativePath.ToLower(),
q.Documentation
})
});
return DisplayJSON(result);
}
}
}

View File

@@ -13,26 +13,16 @@ namespace Ewide.Core.WebApi
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class ApiAuthorizeAttribute : AuthorizeAttribute
{
/// <summary>
/// 是否验证权限
/// </summary>
public bool VerifyAuthorization { get; set; } = true;
public override void OnAuthorization(HttpActionContext actionContext)
{
if (!VerifyAuthorization)
{
base.IsAuthorized(actionContext);
return;
}
var path = actionContext.Request.RequestUri.AbsolutePath;
// 验证token
var authorization = actionContext.Request.Headers.Authorization;
if (authorization != null && !String.IsNullOrEmpty(authorization.Parameter))
{
var token = authorization.Parameter;
var userID = AuthorizedHelper.GetUserID(token);
if (!String.IsNullOrEmpty(userID))
if (ApiAuthorized.Authorized(path, userID))
{
base.IsAuthorized(actionContext);
return;
@@ -42,9 +32,10 @@ namespace Ewide.Core.WebApi
{
var attributes = actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().OfType<AllowAnonymousAttribute>();
bool isAnonymous = attributes.Any(a => a is AllowAnonymousAttribute);
if (isAnonymous)
if (isAnonymous || ApiAuthorized.Authorized(path))
{
base.OnAuthorization(actionContext);
base.IsAuthorized(actionContext);
return;
}
else
{

View File

@@ -58,13 +58,49 @@ namespace Ewide.Core.WebApi
return _DisplayJSON(BaseDisplayJSON.Ok(message));
}
protected IHttpActionResult DisplaySuccessJSON(object result)
{
return DisplayJSON(new
{
Success = true,
Data = result
});
}
protected IHttpActionResult DisplaySuccessJSON(string message)
{
return DisplayJSON(new
{
Success = true,
Message = message
});
}
protected IHttpActionResult DisplayErrorJSON(object result)
{
return DisplayJSON(new
{
Success = false,
Data = result
});
}
protected IHttpActionResult DisplayErrorJSON(string message)
{
return DisplayJSON(new
{
Success = false,
Message = message
});
}
protected IHttpActionResult DisplayDataJSON(object data, int total)
{
return _DisplayJSON(BaseDisplayJSON.Ok(new
return DisplayJSON(new
{
Data = data,
Total = total
}));
});
}
}
}

View File

@@ -52,6 +52,12 @@
<Reference Include="System.Data" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Drawing" />
<Reference Include="System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.7\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
<Reference Include="System.Web.Cors, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.Cors.5.2.4\lib\net45\System.Web.Cors.dll</HintPath>
</Reference>
<Reference Include="System.Web.DynamicData" />
<Reference Include="System.Web.Entity" />
<Reference Include="System.Web.ApplicationServices" />
@@ -59,6 +65,12 @@
<Reference Include="System.Web" />
<Reference Include="System.Web.Abstractions" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Web.Http, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.4\lib\net45\System.Web.Http.dll</HintPath>
</Reference>
<Reference Include="System.Web.Http.Cors, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Cors.5.2.4\lib\net45\System.Web.Http.Cors.dll</HintPath>
</Reference>
<Reference Include="System.Web.Routing" />
<Reference Include="System.Xml" />
<Reference Include="System.Configuration" />
@@ -72,18 +84,12 @@
</Reference>
<Reference Include="System.Net.Http">
</Reference>
<Reference Include="System.Net.Http.Formatting, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.4\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.WebRequest">
</Reference>
<Reference Include="System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.4\lib\net45\System.Web.Helpers.dll</HintPath>
</Reference>
<Reference Include="System.Web.Http, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.4\lib\net45\System.Web.Http.dll</HintPath>
</Reference>
<Reference Include="System.Web.Http.WebHost, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.4\lib\net45\System.Web.Http.WebHost.dll</HintPath>
</Reference>
@@ -134,7 +140,8 @@
<Compile Include="App_Start\RouteConfig.cs" />
<Compile Include="App_Start\WebApiConfig.cs" />
<Compile Include="Areas\Base\BaseAreaRegistration.cs" />
<Compile Include="Areas\Base\Controllers\BetestController.cs" />
<Compile Include="Areas\Base\Controllers\MenuController.cs" />
<Compile Include="Areas\Base\Controllers\SpareController.cs" />
<Compile Include="Areas\Base\Controllers\GateController.cs" />
<Compile Include="Areas\HelpPage\ApiDescriptionExtensions.cs" />
<Compile Include="Areas\HelpPage\App_Start\HelpPageConfig.cs" />
@@ -206,7 +213,9 @@
<Content Include="Areas\HelpPage\Views\Help\Api.cshtml" />
<Content Include="Scripts\jquery-3.3.1.js" />
<Content Include="Scripts\modernizr-2.8.3.js" />
<Content Include="Web.config" />
<Content Include="Web.config">
<SubType>Designer</SubType>
</Content>
<Content Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
</Content>

View File

@@ -5,13 +5,17 @@
-->
<configuration>
<connectionStrings>
<add name="MySqlConnection" connectionString="server=localhost;user id=root;password=a45683926;database=ewide.core;persistsecurityinfo=True" />
<add name="MySqlConnection" connectionString="server=localhost;user id=root;password=a45683926;database=ewide_core;persistsecurityinfo=True" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="Token_JwtSecret" value="H@I9R^@vm!7loYnkG5xWC7frA6@IbBIPA!6NR1$hqBep2e4AC1OtdS^z!X0qT3ik" />
<add key="Token_JwtUser" value="ewide.core" />
<add key="Token_WhiteList" value="ewide.core.whitelist" />
</appSettings>
<!--
有关 web.config 更改的说明,请参见 http://go.microsoft.com/fwlink/?LinkId=235367。
@@ -75,6 +79,10 @@
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.7.0" newVersion="5.2.7.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.codedom>

View File

@@ -3,6 +3,7 @@
<package id="Antlr" version="3.5.0.2" targetFramework="net45" />
<package id="bootstrap" version="3.3.7" targetFramework="net45" />
<package id="jQuery" version="3.3.1" targetFramework="net45" />
<package id="Microsoft.AspNet.Cors" version="5.2.4" targetFramework="net452" />
<package id="Microsoft.AspNet.Mvc" version="5.2.4" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc.zh-Hans" version="5.2.4" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="3.2.4" targetFramework="net45" />
@@ -10,10 +11,11 @@
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net45" />
<package id="Microsoft.AspNet.Web.Optimization.zh-Hans" version="1.1.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi" version="5.2.4" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.4" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client.zh-Hans" version="5.2.4" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.4" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core.zh-Hans" version="5.2.4" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.7" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Client.zh-Hans" version="5.2.7" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.4" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Core.zh-Hans" version="5.2.4" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Cors" version="5.2.4" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.HelpPage" version="5.2.4" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.4" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost.zh-Hans" version="5.2.4" targetFramework="net45" />