forked from AcaMate/AcaMate_API
Compare commits
No commits in common. "1f8ac2cff7946de57d22790a3e390f4ac5ac623e" and "cb159397af40878813bd5fec7659593e1218050e" have entirely different histories.
1f8ac2cff7
...
cb159397af
|
@ -18,5 +18,9 @@
|
|||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="7.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Program\Common\Interfaces\" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
</Project>
|
||||
|
|
15
Program.cs
15
Program.cs
|
@ -14,7 +14,6 @@ using System.Text.Json;
|
|||
using AcaMate.Common.Models;
|
||||
using AcaMate.V1.Services;
|
||||
using AcaMate.Common.Data;
|
||||
using AcaMate.Common.Token;
|
||||
using AcaMate.V1.Controllers;
|
||||
using AcaMate.V1.Models;
|
||||
|
||||
|
@ -150,13 +149,10 @@ else
|
|||
builder.Logging.SetMinimumLevel(LogLevel.Warning);
|
||||
}
|
||||
|
||||
//헤더 부분
|
||||
builder.Services.AddScoped<IHeaderConfig, HeaderConfigRepository>();
|
||||
|
||||
|
||||
|
||||
// 로컬 테스트 위한 부분 (올릴때는 꺼두기)
|
||||
builder.WebHost.UseUrls("http://0.0.0.0:5144");
|
||||
// builder.WebHost.UseUrls("http://0.0.0.0:5144");
|
||||
|
||||
///// ===== builder 설정 부 ===== /////
|
||||
|
||||
|
@ -176,14 +172,7 @@ else
|
|||
}
|
||||
|
||||
// 로컬 테스트 위한 부분 (올릴떄는 켜두기)
|
||||
// app.UseHttpsRedirection();
|
||||
|
||||
// 헤더 미들웨어 부분
|
||||
app.UseMiddleware<APIHeaderMiddle>("HEAD-CHECK");
|
||||
|
||||
// 이부분 봐야 합니다.
|
||||
// app.UseMiddleware<CustomHeaderMiddleware>("X-MyHeader");
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseRouting();
|
||||
// app.MapControllers();
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
|
||||
|
||||
namespace AcaMate.Common.Token;
|
||||
|
||||
public class APIHeaderFilter : ActionFilterAttribute
|
||||
{
|
||||
private readonly string _headerName;
|
||||
|
||||
public APIHeaderFilter(string headerName)
|
||||
{
|
||||
_headerName = headerName;
|
||||
}
|
||||
|
||||
public override void OnActionExecuted(ActionExecutedContext context)
|
||||
{
|
||||
if (!context.HttpContext.Request.Headers.TryGetValue(_headerName, out var headerValues) ||
|
||||
string.IsNullOrWhiteSpace(headerValues))
|
||||
{
|
||||
context.Result = new BadRequestObjectResult($"Missing or empty header: {_headerName}");
|
||||
}
|
||||
|
||||
base.OnActionExecuted(context);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,65 +0,0 @@
|
|||
using System.Threading.Tasks;
|
||||
using AcaMate.Common.Data;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace AcaMate.Common.Token;
|
||||
public interface IHeaderConfig
|
||||
{
|
||||
Task<string> GetExpectedHeaderValueAsync(string headerName);
|
||||
}
|
||||
|
||||
public class HeaderConfigRepository : IHeaderConfig
|
||||
{
|
||||
private readonly AppDbContext _dbContext;
|
||||
|
||||
public HeaderConfigRepository(AppDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task<string> GetExpectedHeaderValueAsync(string headerName)
|
||||
{
|
||||
// 예를 들어, HeaderConfig 테이블에 헤더 이름과 기대 값이 저장되어 있다고 가정합니다.
|
||||
var config = await _dbContext.APIHeader.
|
||||
FirstOrDefaultAsync(h => h.h_key == headerName);
|
||||
return config?.h_value ?? string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class APIHeaderMiddle
|
||||
{
|
||||
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly string _headerName;
|
||||
private readonly IHeaderConfig _headerConfig;
|
||||
|
||||
public APIHeaderMiddle(RequestDelegate next, string headerName, IHeaderConfig headerConfig)
|
||||
{
|
||||
_next = next;
|
||||
_headerName = headerName;
|
||||
_headerConfig = headerConfig;
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
var expectedValue = await _headerConfig.GetExpectedHeaderValueAsync(_headerName);
|
||||
|
||||
if (!context.Request.Headers.TryGetValue(_headerName,out var headerValue) || string.IsNullOrWhiteSpace(headerValue))
|
||||
// if (!context.Request.Headers.ContainsKey(_headerName) || string.IsNullOrWhiteSpace(context.Request.Headers[_headerName]))
|
||||
{
|
||||
context.Response.StatusCode = StatusCodes.Status400BadRequest;
|
||||
await context.Response.WriteAsync($"Missing or empty header: {_headerName}");
|
||||
return;
|
||||
}
|
||||
|
||||
if (headerValue != expectedValue)
|
||||
{
|
||||
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
||||
await context.Response.WriteAsync($"Invalid header value");
|
||||
return;
|
||||
}
|
||||
await _next(context);
|
||||
}
|
||||
}
|
|
@ -11,9 +11,6 @@ public class AppDbContext: DbContext
|
|||
{
|
||||
}
|
||||
|
||||
//MARK: API
|
||||
public DbSet<APIHeader> APIHeader { get; set; }
|
||||
|
||||
//MARK: Program
|
||||
public DbSet<Version> Version { get; set; }
|
||||
public DbSet<Academy> Academy { get; set; }
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace AcaMate.Common.Models;
|
||||
|
||||
[Table("api_header")]
|
||||
public class APIHeader
|
||||
{
|
||||
[Key]
|
||||
public string h_key { get; set; }
|
||||
public string h_value { get; set; }
|
||||
}
|
||||
|
||||
/*
|
||||
h_key : h_value
|
||||
iOS_AM_Connect_Key
|
||||
And_AM_Connect_Key
|
||||
Web_AM_Connect_Key
|
||||
*/
|
|
@ -268,7 +268,7 @@ public class UserController : ControllerBase
|
|||
logUser.log = $"[{summary}] : 정상";
|
||||
|
||||
if (await _repositoryService.SaveData<LogUser>(logUser))
|
||||
_logger.LogInformation($"[{summary}] : 로그 저장 성공");
|
||||
_logger.LogError($"[{summary}] : 로그 저장 성공");
|
||||
|
||||
return Ok(APIResponse.Send("000", $"[{summary}], 정상", new
|
||||
{
|
||||
|
@ -301,7 +301,6 @@ public class UserController : ControllerBase
|
|||
{
|
||||
if (string.IsNullOrEmpty(token)) return BadRequest(APIResponse.InvalidInputError());
|
||||
if (!ModelState.IsValid) return BadRequest(APIResponse.InvalidInputError());
|
||||
|
||||
string summary = String.Empty;
|
||||
try
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue
Block a user