diff --git a/FastTunnel.Server/Controllers/AccountController.cs b/FastTunnel.Server/Controllers/AccountController.cs index a86cdad..d689a60 100644 --- a/FastTunnel.Server/Controllers/AccountController.cs +++ b/FastTunnel.Server/Controllers/AccountController.cs @@ -39,20 +39,20 @@ namespace FastTunnel.Api.Controllers { if ((serverOptionsMonitor.CurrentValue?.Api?.Accounts?.Length ?? 0) == 0) { - ApiResponse.errorCode = ErrorCodeEnum.NoAccount; - ApiResponse.errorMessage = "账号或密码错误"; + ApiResponse.code = ErrorCodeEnum.NoAccount; + ApiResponse.message = "账号或密码错误"; return ApiResponse; } var account = serverOptionsMonitor.CurrentValue.Api.Accounts.FirstOrDefault((x) => { - return x.Name.Equals(request.name) && x.Password.Equals(request.password); + return x.Name.Equals(request.account) && x.Password.Equals(request.password); }); if (account == null) { - ApiResponse.errorCode = ErrorCodeEnum.NoAccount; - ApiResponse.errorMessage = "账号或密码错误"; + ApiResponse.code = ErrorCodeEnum.NoAccount; + ApiResponse.message = "账号或密码错误"; return ApiResponse; } @@ -61,7 +61,7 @@ namespace FastTunnel.Api.Controllers new Claim("Name", account.Name) }; - ApiResponse.data = GenerateToken( + ApiResponse.data = "Bearer " + GenerateToken( claims, serverOptionsMonitor.CurrentValue.Api.JWT.IssuerSigningKey, serverOptionsMonitor.CurrentValue.Api.JWT.Expires, diff --git a/FastTunnel.Server/Filters/CustomExceptionFilterAttribute.cs b/FastTunnel.Server/Filters/CustomExceptionFilterAttribute.cs index 14262d8..b3fdb35 100644 --- a/FastTunnel.Server/Filters/CustomExceptionFilterAttribute.cs +++ b/FastTunnel.Server/Filters/CustomExceptionFilterAttribute.cs @@ -25,9 +25,9 @@ namespace FastTunnel.Api.Filters _logger.LogError(context.Exception, "【全局异常捕获】"); var res = new ApiResponse() { - errorCode = ErrorCodeEnum.Exception, + code = ErrorCodeEnum.Exception, data = null, - errorMessage = context.Exception.Message, + message = context.Exception.Message, }; var result = new JsonResult(res) { StatusCode = 200 }; diff --git a/FastTunnel.Server/Models/ApiResponse.cs b/FastTunnel.Server/Models/ApiResponse.cs index 8062309..6f63034 100644 --- a/FastTunnel.Server/Models/ApiResponse.cs +++ b/FastTunnel.Server/Models/ApiResponse.cs @@ -12,9 +12,9 @@ namespace FastTunnel.Server.Models /// 错误码 /// 0 成功,其他为失败 /// - public ErrorCodeEnum errorCode { get; set; } + public ErrorCodeEnum code { get; set; } - public string errorMessage { get; set; } + public string message { get; set; } public object data { get; set; } } diff --git a/FastTunnel.Server/Models/GetTokenRequest.cs b/FastTunnel.Server/Models/GetTokenRequest.cs index 8617030..3fe2f02 100644 --- a/FastTunnel.Server/Models/GetTokenRequest.cs +++ b/FastTunnel.Server/Models/GetTokenRequest.cs @@ -11,7 +11,7 @@ namespace FastTunnel.Api.Models public class GetTokenRequest { [Required] - public string name { get; set; } + public string account { get; set; } [Required] public string password { get; set; } diff --git a/FastTunnel.Server/Program.cs b/FastTunnel.Server/Program.cs index e380476..7901eee 100644 --- a/FastTunnel.Server/Program.cs +++ b/FastTunnel.Server/Program.cs @@ -6,6 +6,7 @@ using System.Net.Http; using System.Net.Sockets; +using FastTunnel.Api.Filters; using FastTunnel.Core.Extensions; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -36,11 +37,19 @@ public class Program }); // Add services to the container. - + builder.Services.AddSingleton(); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); + builder.Services.AddCors(options => + { + options.AddPolicy("corsPolicy", policy => + { + policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin() + .WithExposedHeaders("Set-Token"); + }); + }); builder.Host.UseSerilog((context, services, configuration) => configuration .ReadFrom.Configuration(context.Configuration) @@ -66,6 +75,7 @@ public class Program app.UseSwaggerUI(); } + app.UseCors("corsPolicy"); app.UseHttpsRedirection(); app.UseStaticFiles(); diff --git a/FastTunnel.Server/Startup.cs b/FastTunnel.Server/Startup.cs deleted file mode 100644 index 63e9d39..0000000 --- a/FastTunnel.Server/Startup.cs +++ /dev/null @@ -1,84 +0,0 @@ -// Licensed under the Apache License, Version 2.0 (the "License"). -// You may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE -// Copyright (c) 2019 Gui.H - -using FastTunnel.Core.Extensions; -using Microsoft.AspNetCore.Authentication.JwtBearer; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.IdentityModel.Tokens; -using System; -using FastTunnel.Core.Config; -using System.Text; -using FastTunnel.Api.Filters; - -#if DEBUG -using Microsoft.OpenApi.Models; -#endif - -namespace FastTunnel.Server; - -public class Startup -{ - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - public IConfiguration Configuration { get; } - - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) - { - services.AddAuthorization(); - - services.AddControllers(); - -#if DEBUG - services.AddSwaggerGen(c => - { - c.SwaggerDoc("v2", new OpenApiInfo { Title = "FastTunel.Api", Version = "v2" }); - }); -#endif - // -------------------FastTunnel STEP1 OF 3------------------ - services.AddFastTunnelServer(Configuration.GetSection("FastTunnel")); - // -------------------FastTunnel STEP1 END------------------- - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); -#if DEBUG - app.UseSwagger(); - app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v2/swagger.json", "FastTunel.WebApi v2")); -#endif - } - - app.UseRouting(); - - // -------------------FastTunnel STEP2 OF 3------------------ - app.UseFastTunnelServer(); - // -------------------FastTunnel STEP2 END------------------- - - // app.UseStaticFiles(); - app.UseAuthentication(); - app.UseAuthorization(); - - app.UseEndpoints(endpoints => - { - endpoints.MapControllers(); - // -------------------FastTunnel STEP3 OF 3------------------ - endpoints.MapFastTunnelServer(); - // -------------------FastTunnel STEP3 END------------------- - }); - } -} diff --git a/FastTunnel.Server/config/appsettings.json b/FastTunnel.Server/config/appsettings.json index 8ab6754..0c9fd29 100644 --- a/FastTunnel.Server/config/appsettings.json +++ b/FastTunnel.Server/config/appsettings.json @@ -41,20 +41,20 @@ /** * 访问api接口的JWT配置 */ - //"Api": { - // "JWT": { - // "ClockSkew": 10, - // "ValidAudience": "https://suidao.io", - // "ValidIssuer": "FastTunnel", - // "IssuerSigningKey": "This is IssuerSigningKey", - // "Expires": 120 - // }, - // "Accounts": [ - // { - // "Name": "admin", - // "Password": "admin123" - // } - // ] - //} + "Api": { + "JWT": { + "ClockSkew": 10, + "ValidAudience": "https://suidao.io", + "ValidIssuer": "FastTunnel", + "IssuerSigningKey": "This is IssuerSigningKey", + "Expires": 120 + }, + "Accounts": [ + { + "Name": "admin", + "Password": "admin123" + } + ] + } } }