微服務-Ocelot基礎配置
前言:如果還不了解網關的,可以去看下我之前的介紹,本文將介紹一下如何配置網關里面對應的swagger以及網關對應的跨域問題,以及Jwt配置,下面直接進入主題:
一、配置Jwt,實現步驟如下:首先添加JetBearer包
1. 添加JWT配置文件,下面有用到
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "JWTTokenOptions": { //添加jwt配置文件 "Audience": "http://localhost:5726", "Issuer": "http://localhost:5726", "SecurityKey": "MIGfMA0GCSqGSIb3DQ" } }
2. 修改Program.cs類里面的方法:
備注:以往我們需要添加鑒權中間件,但是此處不能添加,會報錯:中間件(app.UseAuthentication)
修改如下:
#region jwt校驗 HS JWTTokenOptions tokenOptions = new JWTTokenOptions(); builder.Configuration.Bind("JWTTokenOptions", tokenOptions); string authenticationProviderKey = "UserGatewayKey"; builder.Services .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)//Bearer Scheme .AddJwtBearer(authenticationProviderKey, options => { options.TokenValidationParameters = new TokenValidationParameters { //JWT有一些默認的屬性,就是給鑒權時就可以篩選了 ValidateIssuer = true,//是否驗證Issuer ValidateAudience = true,//是否驗證Audience ValidateLifetime = true,//是否驗證失效時間---默認還添加了300s后才過期 ClockSkew = TimeSpan.FromSeconds(0),//token過期后立馬過期 ValidateIssuerSigningKey = true,//是否驗證SecurityKey ValidAudience = tokenOptions.Audience,//Audience,需要跟前面簽發jwt的設置一致 ValidIssuer = tokenOptions.Issuer,//Issuer,這兩項和前面簽發jwt的設置一致 IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenOptions.SecurityKey)),//拿到SecurityKey }; }); #endregion
3.
//*****************************超時+限流+熔斷+降級+Consul+Polly******************************** { "Routes": [ { "DownstreamPathTemplate": "/api/{url}", //服務地址--url變量 "DownstreamScheme": "http", "UpstreamPathTemplate": "/T/{url}", //網關地址--url變量 "UpstreamHttpMethod": [ "Get", "Post" ], "UseServiceDiscovery": true, "ServiceName": "UserWebAPIService", //consul服務名稱 "LoadBalancerOptions": { "Type": "RoundRobin" //輪詢 LeastConnection-最少連接數的服務器 NoLoadBalance不負載均衡 }, "RateLimitOptions": { "ClientWhitelist": [ "eleven", "seven" ], //白名單 ClientId 區分大小寫 "EnableRateLimiting": true, "Period": "5m", //1s, 5m, 1h, 1d "PeriodTimespan": 30, //多少秒之后客戶端可以重試 "Limit": 5 //統計時間段內允許的最大請求數量 }, "AuthenticationOptions": { "AuthenticationProviderKey": "UserGatewayKey", //UserGatewayKey 這個是我上面有用到的 "AllowedScopes": [ "UserWebAPIService", "UserMinimalAPIService" ] }, "RouteClaimsRequirement": { "Role": "Assistant" }, "QoSOptions": { "ExceptionsAllowedBeforeBreaking": 3, //允許多少個異常請求 "DurationOfBreak": 10000, // 熔斷的時間,單位為ms "TimeoutValue": 2000 //單位ms 如果下游請求的處理時間超過多少則自如將請求設置為超時 默認90秒 }, "FileCacheOptions": { "TtlSeconds": 15, "Region": "UserCache" //可以調用Api清理 } } ], "GlobalConfiguration": { "BaseUrl": "http://127.0.0.1:6299", //網關對外地址 "ServiceDiscoveryProvider": { "Host": "127.0.0.1", "Port": 8500, "Type": "Consul" //由Consul提供服務發現 }, "RateLimitOptions": { "QuotaExceededMessage": "Too many requests, maybe later? 11", // 當請求過載被截斷時返回的消息 "HttpStatusCode": 666, // 當請求過載被截斷時返回的http status "ClientIdHeader": "client_id" // 用來識別客戶端的請求頭,默認是 ClientId } } }
一、配置網關里面對應的swagger
//*****************************服務器配置swagger******************************** { "Routes": [ { "DownstreamPathTemplate": "/api/{url}", //服務地址--url變量 "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "192.168.3.230", "Port": 5030 //服務端口 } ], "UpstreamPathTemplate": "/T5030/{url}", //網關地址--url變量 "UpstreamHttpMethod": [ "Get", "Post" ] }, { "UpstreamPathTemplate": "/webapi/swagger/v1/swagger.json", "UpstreamHttpMethod": [ "Get" ], "DownstreamHostAndPorts": [ { "Host": "192.168.3.230", "Port": 5030 //服務端口 } ], "DownstreamPathTemplate": "/swagger/v1/swagger.json", "DownstreamScheme": "http" }, { "UpstreamPathTemplate": "/webapiV2/swagger/v2/swagger.json", "UpstreamHttpMethod": [ "Get" ], "DownstreamHostAndPorts": [ { "Host": "192.168.3.230", "Port": 5030 //服務端口 } ], "DownstreamPathTemplate": "/swagger/v1/swagger.json", "DownstreamScheme": "http" } ] }
二、網關對應的跨域
////*****************************單地址+跨域******************************** //{ // "Routes": [ // { // "DownstreamPathTemplate": "/api/{url}", //服務地址--url變量 // "DownstreamScheme": "http", // "DownstreamHeaderTransform": { // "Access-Control-Allow-Origin": "http://localhost:8070", //不存在就添加 // "Access-Control-Allow-Methods": "*", // "Access-Control-Allow-Headers": "*" // }, // "DownstreamHostAndPorts": [ // { // "Host": "192.168.3.230", // "Port": 5030 //服務端口 // } // ], // "UpstreamPathTemplate": "/T5030/{url}", //網關地址--url變量 // "UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ] // } // ] //}
謝謝學習!!!共同進步,如有疑問,請留言~