5.42Custom HTTP Declarative Extractor (Authorization)
The following is an example that shows how to implement automatic authorization and anonymous access by customizing the AuthenticationAttribute and AllowAnonymousAttribute attributes and adding the corresponding extractors.
1. Defining the AuthenticationAttribute attribute
Apply the AuthenticationAttribute attribute to methods or interfaces.
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Interface)]public class AuthenticationAttribute : Attribute;2. Implementing the AuthenticationDeclarativeExtractor and AllowAnonymousDeclarativeExtractor extractors
/// <summary>/// [Authentication] attribute extractor/// </summary>public class AuthenticationDeclarativeExtractor : IHttpDeclarativeExtractor{ /// <inheritdoc /> public void Extract(HttpRequestBuilder httpRequestBuilder, HttpDeclarativeParsingContext context) { // Skip if the [AllowAnonymous] attribute is applied if (context.IsMethodDefined<AllowAnonymousAttribute>(out _, true)) return; // Check whether authorization information has already been set if (httpRequestBuilder.AuthenticationHeader is not null) return; // Add the authorization header (any authorization logic can be implemented here, such as getting a token from a parameter, etc.) httpRequestBuilder.AddBearerAuthentication( "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"); }}/// <summary>/// [AllowAnonymous] attribute extractor/// </summary>public class AllowAnonymousDeclarativeExtractor : IHttpDeclarativeExtractor{ /// <inheritdoc /> public void Extract(HttpRequestBuilder httpRequestBuilder, HttpDeclarativeParsingContext context) { // Skip if the [AllowAnonymous] attribute is not applied if (!context.IsMethodDefined<AllowAnonymousAttribute>(out _, true)) return; // Remove the authorization header httpRequestBuilder.RemoveHeaders("Authorization"); }}3. Registering the custom extractors in configuration
In the Startup.cs or Program.cs file, configure and register the HttpRemote service to enable the custom HTTP declarative extractor functionality.
services.AddHttpRemote(builder =>{ // Add custom HTTP declarative extractors builder.AddHttpDeclarativeExtractors(() => [ new AuthenticationDeclarativeExtractor(), new AllowAnonymousDeclarativeExtractor() ]); // Scan assemblies to add HTTP declarative extractors in bulk (recommended) // builder.AddHttpDeclarativeExtractorsFromAssemblies([ assembly1, assembly2, ... ]); // When using the Furion framework, you can directly set App.Assemblies});4. Using the custom attributes in an HTTP declarative interface
[Authentication] // Add global authorizationpublic interface IAuthService : IHttpDeclarative{ [Get("https://furion.net/")] Task<string> GetDataAsync(); // Accessing this interface requires authorization [AllowAnonymous] // Anonymous access [Get("https://furion.net/")] Task<string> LoginAsync(string username, string password);}When the GetDataAsync method is called, the authorization header is automatically added (implementing authorization). When the LoginAsync method is called, the authorization request header is automatically removed (implementing anonymous access).
As this example shows, custom HTTP declarative extractors provide great flexibility for implementing complex authorization logic.