From 5cfe7dad7899c238149d97bb9e63f40d37d98230 Mon Sep 17 00:00:00 2001 From: "Gui.H" Date: Thu, 29 Dec 2022 18:46:00 +0800 Subject: [PATCH] 1 --- FastTunnel.Client/appsettings.json | 2 +- FastTunnel.Server/Program.cs | 38 +++++++++++++++++++++++ FastTunnel.Server/config/appsettings.json | 1 - 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/FastTunnel.Client/appsettings.json b/FastTunnel.Client/appsettings.json index 9f0c826..4a8ca03 100644 --- a/FastTunnel.Client/appsettings.json +++ b/FastTunnel.Client/appsettings.json @@ -21,7 +21,7 @@ "FastTunnel": { "Server": { // [必选] 服务端ip/域名(来自服务端配置文件的urls参数) - "ServerAddr": "suidao.bx.com.cn", + "ServerAddr": "127.0.0.1", // [必选] 服务端监听的通信端口(来自服务端配置文件的urls参数) "ServerPort": 1270 }, diff --git a/FastTunnel.Server/Program.cs b/FastTunnel.Server/Program.cs index 7901eee..d45a399 100644 --- a/FastTunnel.Server/Program.cs +++ b/FastTunnel.Server/Program.cs @@ -4,16 +4,23 @@ // https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE // Copyright (c) 2019 Gui.H +using System; using System.Net.Http; using System.Net.Sockets; +using System.Text; using FastTunnel.Api.Filters; +using FastTunnel.Core.Config; using FastTunnel.Core.Extensions; +using FastTunnel.Server.Models; +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.Extensions.Logging; +using Microsoft.IdentityModel.Tokens; using Serilog; using Serilog.Events; @@ -64,6 +71,37 @@ public class Program builder.Services.AddFastTunnelServer(builder.Configuration.GetSection("FastTunnel")); // -------------------FastTunnel STEP1 END------------------- + var Configuration = builder.Configuration; + var apioptions = Configuration.GetSection("FastTunnel").Get(); + + builder.Services.AddAuthentication("Bearer").AddJwtBearer(delegate (JwtBearerOptions options) + { + options.TokenValidationParameters = new TokenValidationParameters + { + ValidateIssuer = false, + ValidateAudience = false, + ValidateLifetime = true, + ClockSkew = TimeSpan.FromSeconds(apioptions.Api.JWT.ClockSkew), + ValidateIssuerSigningKey = true, + ValidAudience = apioptions.Api.JWT.ValidAudience, + ValidIssuer = apioptions.Api.JWT.ValidIssuer, + IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(apioptions.Api.JWT.IssuerSigningKey)) + }; + options.Events = new JwtBearerEvents + { + OnChallenge = async delegate (JwtBearerChallengeContext context) + { + context.HandleResponse(); + context.Response.ContentType = "application/json;charset=utf-8"; + await context.Response.WriteAsJsonAsync(new + { + code = -1, + message = context.Error ?? "未登录" + }); + } + }; + }); + builder.Host.UseWindowsService(); var app = builder.Build(); diff --git a/FastTunnel.Server/config/appsettings.json b/FastTunnel.Server/config/appsettings.json index 0c9fd29..13eab16 100644 --- a/FastTunnel.Server/config/appsettings.json +++ b/FastTunnel.Server/config/appsettings.json @@ -37,7 +37,6 @@ // 可选,当不为空时,客户端也必须携带Tokens中的任意一个token,否则拒绝登录。 "Tokens": [ "TOKEN_FOR_CLIENT_AUTHENTICATION" ], - /** * 访问api接口的JWT配置 */