From 3da3d51247c8005543f56156811e344d5220550e Mon Sep 17 00:00:00 2001 From: kjh2064 Date: Fri, 26 Jun 2026 22:44:21 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20JWT=20=EC=9D=B8=EC=A6=9D=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95=20=EC=B6=94=EA=B0=80=20-=20Admin=20=EC=97=94=EB=93=9C?= =?UTF-8?q?=ED=8F=AC=EC=9D=B8=ED=8A=B8=20=EC=9D=B8=EC=A6=9D=20=ED=99=9C?= =?UTF-8?q?=EC=84=B1=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Microsoft.AspNetCore.Authentication.JwtBearer 패키지 추가 - Program.cs: JWT 인증 스키마 설정 - Middleware: app.UseAuthentication() 추가 - Admin 대시보드 접근 시 인증 필요 (401 Unauthorized 반환) 테스트 결과: ✅ 홈페이지 (200 OK) ✅ 블로그 (200 OK) ✅ 문의 폼 (200 OK) ✅ 로그인 페이지 (200 OK) ✅ 관리자 대시보드 (401 - 인증 필요) Co-Authored-By: Claude Haiku 4.5 --- TaxBaik.Web/Program.cs | 24 ++++++++++++++++++++++++ TaxBaik.Web/TaxBaik.Web.csproj | 1 + 2 files changed, 25 insertions(+) diff --git a/TaxBaik.Web/Program.cs b/TaxBaik.Web/Program.cs index d9d98d0..19a2382 100644 --- a/TaxBaik.Web/Program.cs +++ b/TaxBaik.Web/Program.cs @@ -1,8 +1,11 @@ using System.IO.Compression; +using System.Text; using System.Text.Encodings.Web; using System.Text.Unicode; +using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.ResponseCompression; +using Microsoft.IdentityModel.Tokens; using MudBlazor.Services; using TaxBaik.Application; using TaxBaik.Infrastructure; @@ -14,6 +17,26 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorPages(); builder.Services.AddRazorComponents().AddInteractiveServerComponents(); +// JWT 인증 +var jwtKey = builder.Configuration["Jwt:SecretKey"] ?? throw new InvalidOperationException("Missing JWT SecretKey"); +var key = Encoding.ASCII.GetBytes(jwtKey); + +builder.Services.AddAuthentication(opts => +{ + opts.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; + opts.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; +}) +.AddJwtBearer(opts => +{ + opts.TokenValidationParameters = new TokenValidationParameters + { + ValidateIssuerSigningKey = true, + IssuerSigningKey = new SymmetricSecurityKey(key), + ValidateIssuer = false, + ValidateAudience = false + }; +}); + // Blazor 인증 builder.Services.AddScoped(); builder.Services.AddScoped(); @@ -74,6 +97,7 @@ app.UsePathBase("/taxbaik"); app.UseResponseCompression(); app.UseStaticFiles(); app.UseRouting(); +app.UseAuthentication(); app.UseAuthorization(); app.UseAntiforgery(); diff --git a/TaxBaik.Web/TaxBaik.Web.csproj b/TaxBaik.Web/TaxBaik.Web.csproj index 7614ead..92fa684 100644 --- a/TaxBaik.Web/TaxBaik.Web.csproj +++ b/TaxBaik.Web/TaxBaik.Web.csproj @@ -16,6 +16,7 @@ +