mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 03:18:15 +08:00
everything working..now for the docs
This commit is contained in:
@ -36,7 +36,7 @@ namespace Ocelot.Authentication.Middleware
|
||||
{
|
||||
_logger.LogDebug($"{context.Request.Path} is an authenticated route. {MiddlewareName} checking if client is authenticated");
|
||||
|
||||
var result = await context.AuthenticateAsync(DownstreamRoute.ReRoute.AuthenticationProviderKey);
|
||||
var result = await context.AuthenticateAsync(DownstreamRoute.ReRoute.AuthenticationOptions.AuthenticationProviderKey);
|
||||
|
||||
context.User = result.Principal;
|
||||
|
||||
|
@ -4,11 +4,13 @@ namespace Ocelot.Configuration
|
||||
{
|
||||
public class AuthenticationOptions
|
||||
{
|
||||
public AuthenticationOptions(List<string> allowedScopes)
|
||||
public AuthenticationOptions(List<string> allowedScopes, string authenticationProviderKey)
|
||||
{
|
||||
AllowedScopes = allowedScopes;
|
||||
AuthenticationProviderKey = authenticationProviderKey;
|
||||
}
|
||||
|
||||
public List<string> AllowedScopes { get; private set; }
|
||||
public string AuthenticationProviderKey { get; private set; }
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ namespace Ocelot.Configuration.Builder
|
||||
public class AuthenticationOptionsBuilder
|
||||
{
|
||||
private List<string> _allowedScopes = new List<string>();
|
||||
private string _authenticationProviderKey;
|
||||
|
||||
public AuthenticationOptionsBuilder WithAllowedScopes(List<string> allowedScopes)
|
||||
{
|
||||
@ -12,9 +13,15 @@ namespace Ocelot.Configuration.Builder
|
||||
return this;
|
||||
}
|
||||
|
||||
public AuthenticationOptionsBuilder WithAuthenticationProviderKey(string authenticationProviderKey)
|
||||
{
|
||||
_authenticationProviderKey = authenticationProviderKey;
|
||||
return this;
|
||||
}
|
||||
|
||||
public AuthenticationOptions Build()
|
||||
{
|
||||
return new AuthenticationOptions(_allowedScopes);
|
||||
return new AuthenticationOptions(_allowedScopes, _authenticationProviderKey);
|
||||
}
|
||||
}
|
||||
}
|
@ -29,6 +29,7 @@ namespace Ocelot.Configuration.Builder
|
||||
private ServiceProviderConfiguration _serviceProviderConfiguraion;
|
||||
private bool _useQos;
|
||||
private QoSOptions _qosOptions;
|
||||
private HttpHandlerOptions _httpHandlerOptions;
|
||||
public bool _enableRateLimiting;
|
||||
public RateLimitOptions _rateLimitOptions;
|
||||
private string _authenticationProviderKey;
|
||||
@ -183,6 +184,11 @@ namespace Ocelot.Configuration.Builder
|
||||
return this;
|
||||
}
|
||||
|
||||
public ReRouteBuilder WithHttpHandlerOptions(HttpHandlerOptions input)
|
||||
{
|
||||
_httpHandlerOptions = input;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ReRoute Build()
|
||||
{
|
||||
@ -211,7 +217,7 @@ namespace Ocelot.Configuration.Builder
|
||||
_qosOptions,
|
||||
_enableRateLimiting,
|
||||
_rateLimitOptions,
|
||||
_authenticationProviderKey);
|
||||
_httpHandlerOptions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ namespace Ocelot.Configuration.Creator
|
||||
{
|
||||
public AuthenticationOptions Create(FileReRoute reRoute)
|
||||
{
|
||||
return new AuthenticationOptions(reRoute.AuthenticationOptions.AllowedScopes);
|
||||
return new AuthenticationOptions(reRoute.AuthenticationOptions.AllowedScopes, reRoute.AuthenticationOptions.AuthenticationProviderKey);
|
||||
}
|
||||
}
|
||||
}
|
@ -38,6 +38,7 @@ namespace Ocelot.Configuration.Creator
|
||||
private readonly IReRouteOptionsCreator _fileReRouteOptionsCreator;
|
||||
private readonly IRateLimitOptionsCreator _rateLimitOptionsCreator;
|
||||
private readonly IRegionCreator _regionCreator;
|
||||
private readonly IHttpHandlerOptionsCreator _httpHandlerOptionsCreator;
|
||||
|
||||
public FileOcelotConfigurationCreator(
|
||||
IOptions<FileConfiguration> options,
|
||||
@ -55,7 +56,8 @@ namespace Ocelot.Configuration.Creator
|
||||
IQoSOptionsCreator qosOptionsCreator,
|
||||
IReRouteOptionsCreator fileReRouteOptionsCreator,
|
||||
IRateLimitOptionsCreator rateLimitOptionsCreator,
|
||||
IRegionCreator regionCreator
|
||||
IRegionCreator regionCreator,
|
||||
IHttpHandlerOptionsCreator httpHandlerOptionsCreator
|
||||
)
|
||||
{
|
||||
_regionCreator = regionCreator;
|
||||
@ -74,6 +76,7 @@ namespace Ocelot.Configuration.Creator
|
||||
_serviceProviderConfigCreator = serviceProviderConfigCreator;
|
||||
_qosOptionsCreator = qosOptionsCreator;
|
||||
_fileReRouteOptionsCreator = fileReRouteOptionsCreator;
|
||||
_httpHandlerOptionsCreator = httpHandlerOptionsCreator;
|
||||
}
|
||||
|
||||
public async Task<Response<IOcelotConfiguration>> Create()
|
||||
@ -143,6 +146,8 @@ namespace Ocelot.Configuration.Creator
|
||||
|
||||
var region = _regionCreator.Create(fileReRoute);
|
||||
|
||||
var httpHandlerOptions = _httpHandlerOptionsCreator.Create(fileReRoute);
|
||||
|
||||
var reRoute = new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate(fileReRoute.DownstreamPathTemplate)
|
||||
.WithUpstreamPathTemplate(fileReRoute.UpstreamPathTemplate)
|
||||
@ -168,7 +173,7 @@ namespace Ocelot.Configuration.Creator
|
||||
.WithQosOptions(qosOptions)
|
||||
.WithEnableRateLimiting(fileReRouteOptions.EnableRateLimiting)
|
||||
.WithRateLimitOptions(rateLimitOption)
|
||||
.WithAuthenticationProviderKey(fileReRoute.AuthenticationOptions.AuthenticationProviderKey)
|
||||
.WithHttpHandlerOptions(httpHandlerOptions)
|
||||
.Build();
|
||||
|
||||
await SetupLoadBalancer(reRoute);
|
||||
|
@ -0,0 +1,13 @@
|
||||
using Ocelot.Configuration.File;
|
||||
|
||||
namespace Ocelot.Configuration.Creator
|
||||
{
|
||||
public class HttpHandlerOptionsCreator : IHttpHandlerOptionsCreator
|
||||
{
|
||||
public HttpHandlerOptions Create(FileReRoute fileReRoute)
|
||||
{
|
||||
return new HttpHandlerOptions(fileReRoute.HttpHandlerOptions.AllowAutoRedirect,
|
||||
fileReRoute.HttpHandlerOptions.UseCookieContainer);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
using Ocelot.Configuration.File;
|
||||
|
||||
namespace Ocelot.Configuration.Creator
|
||||
{
|
||||
/// <summary>
|
||||
/// Describes creation of HttpHandlerOptions
|
||||
/// </summary>
|
||||
public interface IHttpHandlerOptionsCreator
|
||||
{
|
||||
HttpHandlerOptions Create(FileReRoute fileReRoute);
|
||||
}
|
||||
}
|
@ -41,8 +41,8 @@ namespace Ocelot.Configuration.Creator
|
||||
}
|
||||
|
||||
var route = reRoute.ReRouteIsCaseSensitive
|
||||
? $"{upstreamTemplate}{RegExMatchEndString}"
|
||||
: $"{RegExIgnoreCase}{upstreamTemplate}{RegExMatchEndString}";
|
||||
? $"^{upstreamTemplate}{RegExMatchEndString}"
|
||||
: $"^{RegExIgnoreCase}{upstreamTemplate}{RegExMatchEndString}";
|
||||
|
||||
return route;
|
||||
}
|
||||
|
15
src/Ocelot/Configuration/File/FileHttpHandlerOptions.cs
Normal file
15
src/Ocelot/Configuration/File/FileHttpHandlerOptions.cs
Normal file
@ -0,0 +1,15 @@
|
||||
namespace Ocelot.Configuration.File
|
||||
{
|
||||
public class FileHttpHandlerOptions
|
||||
{
|
||||
public FileHttpHandlerOptions()
|
||||
{
|
||||
AllowAutoRedirect = true;
|
||||
UseCookieContainer = true;
|
||||
}
|
||||
|
||||
public bool AllowAutoRedirect { get; set; }
|
||||
|
||||
public bool UseCookieContainer { get; set; }
|
||||
}
|
||||
}
|
@ -15,6 +15,7 @@ namespace Ocelot.Configuration.File
|
||||
QoSOptions = new FileQoSOptions();
|
||||
RateLimitOptions = new FileRateLimitRule();
|
||||
AuthenticationOptions = new FileAuthenticationOptions();
|
||||
HttpHandlerOptions = new FileHttpHandlerOptions();
|
||||
}
|
||||
|
||||
public string DownstreamPathTemplate { get; set; }
|
||||
@ -35,5 +36,6 @@ namespace Ocelot.Configuration.File
|
||||
public string LoadBalancer {get;set;}
|
||||
public FileRateLimitRule RateLimitOptions { get; set; }
|
||||
public FileAuthenticationOptions AuthenticationOptions { get; set; }
|
||||
public FileHttpHandlerOptions HttpHandlerOptions { get; set; }
|
||||
}
|
||||
}
|
25
src/Ocelot/Configuration/HttpHandlerOptions.cs
Normal file
25
src/Ocelot/Configuration/HttpHandlerOptions.cs
Normal file
@ -0,0 +1,25 @@
|
||||
namespace Ocelot.Configuration
|
||||
{
|
||||
/// <summary>
|
||||
/// Describes configuration parameters for http handler,
|
||||
/// that is created to handle a request to service
|
||||
/// </summary>
|
||||
public class HttpHandlerOptions
|
||||
{
|
||||
public HttpHandlerOptions(bool allowAutoRedirect, bool useCookieContainer)
|
||||
{
|
||||
AllowAutoRedirect = allowAutoRedirect;
|
||||
UseCookieContainer = useCookieContainer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specify if auto redirect is enabled
|
||||
/// </summary>
|
||||
public bool AllowAutoRedirect { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Specify is handler has to use a cookie container
|
||||
/// </summary>
|
||||
public bool UseCookieContainer { get; private set; }
|
||||
}
|
||||
}
|
@ -30,7 +30,7 @@ namespace Ocelot.Configuration
|
||||
QoSOptions qosOptions,
|
||||
bool enableEndpointRateLimiting,
|
||||
RateLimitOptions ratelimitOptions,
|
||||
string authenticationProviderKey)
|
||||
HttpHandlerOptions httpHandlerOptions)
|
||||
{
|
||||
ReRouteKey = reRouteKey;
|
||||
ServiceProviderConfiguraion = serviceProviderConfiguraion;
|
||||
@ -59,7 +59,7 @@ namespace Ocelot.Configuration
|
||||
QosOptionsOptions = qosOptions;
|
||||
EnableEndpointEndpointRateLimiting = enableEndpointRateLimiting;
|
||||
RateLimitOptions = ratelimitOptions;
|
||||
AuthenticationProviderKey = authenticationProviderKey;
|
||||
HttpHandlerOptions = httpHandlerOptions;
|
||||
}
|
||||
|
||||
public string ReRouteKey {get;private set;}
|
||||
@ -86,6 +86,6 @@ namespace Ocelot.Configuration
|
||||
public ServiceProviderConfiguration ServiceProviderConfiguraion { get; private set; }
|
||||
public bool EnableEndpointEndpointRateLimiting { get; private set; }
|
||||
public RateLimitOptions RateLimitOptions { get; private set; }
|
||||
public string AuthenticationProviderKey { get; private set; }
|
||||
public HttpHandlerOptions HttpHandlerOptions { get; private set; }
|
||||
}
|
||||
}
|
@ -124,6 +124,7 @@ namespace Ocelot.DependencyInjection
|
||||
services.TryAddSingleton<IRateLimitCounterHandler, MemoryCacheRateLimitCounterHandler>();
|
||||
services.TryAddSingleton<IHttpClientCache, MemoryHttpClientCache>();
|
||||
services.TryAddSingleton<IRequestMapper, RequestMapper>();
|
||||
services.TryAddSingleton<IHttpHandlerOptionsCreator, HttpHandlerOptionsCreator>();
|
||||
|
||||
// see this for why we register this as singleton http://stackoverflow.com/questions/37371264/invalidoperationexception-unable-to-resolve-service-for-type-microsoft-aspnetc
|
||||
// could maybe use a scoped data repository
|
||||
|
@ -10,9 +10,11 @@ namespace Ocelot.Request.Builder
|
||||
public async Task<Response<Request>> Build(
|
||||
HttpRequestMessage httpRequestMessage,
|
||||
bool isQos,
|
||||
IQoSProvider qosProvider)
|
||||
IQoSProvider qosProvider,
|
||||
bool useCookieContainer,
|
||||
bool allowAutoRedirect)
|
||||
{
|
||||
return new OkResponse<Request>(new Request(httpRequestMessage, isQos, qosProvider));
|
||||
return new OkResponse<Request>(new Request(httpRequestMessage, isQos, qosProvider, useCookieContainer, allowAutoRedirect));
|
||||
}
|
||||
}
|
||||
}
|
@ -11,6 +11,8 @@
|
||||
Task<Response<Request>> Build(
|
||||
HttpRequestMessage httpRequestMessage,
|
||||
bool isQos,
|
||||
IQoSProvider qosProvider);
|
||||
IQoSProvider qosProvider,
|
||||
bool useCookieContainer,
|
||||
bool allowAutoRedirect);
|
||||
}
|
||||
}
|
||||
|
@ -46,7 +46,9 @@ namespace Ocelot.Request.Middleware
|
||||
var buildResult = await _requestCreator.Build(
|
||||
DownstreamRequest,
|
||||
DownstreamRoute.ReRoute.IsQos,
|
||||
qosProvider.Data);
|
||||
qosProvider.Data,
|
||||
DownstreamRoute.ReRoute.HttpHandlerOptions.UseCookieContainer,
|
||||
DownstreamRoute.ReRoute.HttpHandlerOptions.AllowAutoRedirect);
|
||||
|
||||
if (buildResult.IsError)
|
||||
{
|
||||
|
@ -8,15 +8,21 @@ namespace Ocelot.Request
|
||||
public Request(
|
||||
HttpRequestMessage httpRequestMessage,
|
||||
bool isQos,
|
||||
IQoSProvider qosProvider)
|
||||
IQoSProvider qosProvider,
|
||||
bool allowAutoRedirect,
|
||||
bool useCookieContainer)
|
||||
{
|
||||
HttpRequestMessage = httpRequestMessage;
|
||||
IsQos = isQos;
|
||||
QosProvider = qosProvider;
|
||||
AllowAutoRedirect = allowAutoRedirect;
|
||||
UseCookieContainer = useCookieContainer;
|
||||
}
|
||||
|
||||
public HttpRequestMessage HttpRequestMessage { get; private set; }
|
||||
public bool IsQos { get; private set; }
|
||||
public IQoSProvider QosProvider { get; private set; }
|
||||
public bool AllowAutoRedirect { get; private set; }
|
||||
public bool UseCookieContainer { get; private set; }
|
||||
}
|
||||
}
|
||||
|
@ -20,9 +20,9 @@ namespace Ocelot.Requester
|
||||
return this;
|
||||
}
|
||||
|
||||
public IHttpClient Create()
|
||||
public IHttpClient Create(bool useCookies, bool allowAutoRedirect)
|
||||
{
|
||||
var httpclientHandler = new HttpClientHandler();
|
||||
var httpclientHandler = new HttpClientHandler { AllowAutoRedirect = allowAutoRedirect, UseCookies = useCookies};
|
||||
|
||||
var client = new HttpClient(CreateHttpMessageHandler(httpclientHandler));
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Responses;
|
||||
using Polly.CircuitBreaker;
|
||||
@ -14,7 +15,8 @@ namespace Ocelot.Requester
|
||||
private readonly IHttpClientCache _cacheHandlers;
|
||||
private readonly IOcelotLogger _logger;
|
||||
|
||||
public HttpClientHttpRequester(IOcelotLoggerFactory loggerFactory, IHttpClientCache cacheHandlers)
|
||||
public HttpClientHttpRequester(IOcelotLoggerFactory loggerFactory,
|
||||
IHttpClientCache cacheHandlers)
|
||||
{
|
||||
_logger = loggerFactory.CreateLogger<HttpClientHttpRequester>();
|
||||
_cacheHandlers = cacheHandlers;
|
||||
@ -25,8 +27,8 @@ namespace Ocelot.Requester
|
||||
var builder = new HttpClientBuilder();
|
||||
|
||||
var cacheKey = GetCacheKey(request, builder);
|
||||
|
||||
var httpClient = GetHttpClient(cacheKey, builder);
|
||||
|
||||
var httpClient = GetHttpClient(cacheKey, builder, request.UseCookieContainer, request.AllowAutoRedirect);
|
||||
|
||||
try
|
||||
{
|
||||
@ -54,13 +56,13 @@ namespace Ocelot.Requester
|
||||
|
||||
}
|
||||
|
||||
private IHttpClient GetHttpClient(string cacheKey, IHttpClientBuilder builder)
|
||||
private IHttpClient GetHttpClient(string cacheKey, IHttpClientBuilder builder, bool useCookieContainer, bool allowAutoRedirect)
|
||||
{
|
||||
var httpClient = _cacheHandlers.Get(cacheKey);
|
||||
|
||||
if (httpClient == null)
|
||||
{
|
||||
httpClient = builder.Create();
|
||||
httpClient = builder.Create(useCookieContainer, allowAutoRedirect);
|
||||
}
|
||||
return httpClient;
|
||||
}
|
||||
|
@ -15,11 +15,13 @@ namespace Ocelot.Requester
|
||||
/// <summary>
|
||||
/// Sets a PollyCircuitBreakingDelegatingHandler .
|
||||
/// </summary>
|
||||
IHttpClientBuilder WithQos(IQoSProvider qosProvider, IOcelotLogger logger);
|
||||
|
||||
/// <summary>
|
||||
IHttpClientBuilder WithQos(IQoSProvider qosProvider, IOcelotLogger logger);
|
||||
|
||||
/// <summary>
|
||||
/// Creates the <see cref="HttpClient"/>
|
||||
/// </summary>
|
||||
IHttpClient Create();
|
||||
/// </summary>
|
||||
/// <param name="useCookies">Defines if http client should use cookie container</param>
|
||||
/// <param name="allowAutoRedirect">Defines if http client should allow auto redirect</param>
|
||||
IHttpClient Create(bool useCookies, bool allowAutoRedirect);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user