调整api响应

This commit is contained in:
Gui.H 2022-12-29 17:01:47 +08:00
parent 507abebf2c
commit 3488bd6ae7
7 changed files with 37 additions and 111 deletions

View File

@ -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,

View File

@ -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 };

View File

@ -12,9 +12,9 @@ namespace FastTunnel.Server.Models
/// 错误码
/// 0 成功,其他为失败
/// </summary>
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; }
}

View File

@ -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; }

View File

@ -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<CustomExceptionFilterAttribute>();
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();

View File

@ -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-------------------
});
}
}

View File

@ -41,20 +41,20 @@
/**
* 访apiJWT
*/
//"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"
}
]
}
}
}