first
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
using FASS.Scheduler.Models;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.Text;
|
||||
|
||||
namespace FASS.Scheduler.Extensions.Configure
|
||||
{
|
||||
public static class AuthExtension
|
||||
{
|
||||
public static IServiceCollection AddAuth(this IServiceCollection services, AppSettings appSettings)
|
||||
{
|
||||
services
|
||||
.AddAuthentication(options =>
|
||||
{
|
||||
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
})
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.TokenValidationParameters = new TokenValidationParameters()
|
||||
{
|
||||
ValidateIssuer = true,
|
||||
ValidIssuer = appSettings.Auth.Issuer,
|
||||
|
||||
ValidateAudience = true,
|
||||
ValidAudience = appSettings.Auth.Audience,
|
||||
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(appSettings.Auth.SigningKey)),
|
||||
|
||||
ValidateLifetime = true,
|
||||
RequireExpirationTime = true,
|
||||
ClockSkew = TimeSpan.Zero
|
||||
};
|
||||
options.Events = new JwtBearerEvents()
|
||||
{
|
||||
OnMessageReceived = context =>
|
||||
{
|
||||
if (context.Request.Headers.ContainsKey("Authorization"))
|
||||
{
|
||||
context.Token = context.Request.Headers["Authorization"].FirstOrDefault()?.Substring("Bearer ".Length);
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
},
|
||||
OnAuthenticationFailed = context =>
|
||||
{
|
||||
if (context.Exception is SecurityTokenExpiredException)
|
||||
{
|
||||
context.Response.Headers.Append("Token-Expired", "true");
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
};
|
||||
});
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IApplicationBuilder UseAuth(this IApplicationBuilder app)
|
||||
{
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
return app;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using FASS.Scheduler.Models;
|
||||
using FASS.Scheduler.Services.CronTasks;
|
||||
using FASS.Scheduler.Services.EventBus;
|
||||
using FASS.Scheduler.Services.Extends;
|
||||
using FASS.Service.Extensions;
|
||||
|
||||
namespace FASS.Scheduler.Extensions.Configure
|
||||
{
|
||||
public static class BootExtension
|
||||
{
|
||||
public static IServiceCollection AddBoot(this IServiceCollection services, IConfiguration configuration, AppSettings appSettings)
|
||||
{
|
||||
services.AddSingleton<ExtendService>();
|
||||
|
||||
services.AddSingleton<CronTaskService>();
|
||||
services.AddSingleton<EventBusService>();
|
||||
|
||||
services.AddService(configuration, appSettings.App.ActivationCode, () => appSettings.Frame);
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceProvider UseBoot(this IServiceProvider provider)
|
||||
{
|
||||
provider.UseService();
|
||||
|
||||
return provider;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Common.NETCore.Extensions;
|
||||
using Common.NETCore.Models;
|
||||
using Microsoft.AspNetCore.Diagnostics;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace FASS.Scheduler.Extensions.Configure
|
||||
{
|
||||
public static class ExceptionExtension
|
||||
{
|
||||
public static IApplicationBuilder UseException(this IApplicationBuilder app)
|
||||
{
|
||||
var jsonSerializerOptions = new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase
|
||||
};
|
||||
app.UseExceptionHandler(builder =>
|
||||
{
|
||||
builder.Run(async context =>
|
||||
{
|
||||
var ex = context.Features.Get<IExceptionHandlerFeature>()?.Error.GetBaseException();
|
||||
var responseResult = new ResponseResult()
|
||||
{
|
||||
Success = false,
|
||||
Code = context.Response.StatusCode.ToString()
|
||||
};
|
||||
if (ex != null)
|
||||
{
|
||||
responseResult.Message = ex.Message;
|
||||
}
|
||||
else
|
||||
{
|
||||
responseResult.Message = "未知错误";
|
||||
}
|
||||
context.Response.StatusCode = StatusCodes.Status200OK;
|
||||
context.Response.ContentType = "application/json";
|
||||
await context.Response.Body.WriteAsync(responseResult.ToJson(jsonSerializerOptions).ToBytes());
|
||||
});
|
||||
});
|
||||
return app;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using Common.AspNetCore.Extensions;
|
||||
namespace FASS.Scheduler.Extensions.Configure
|
||||
{
|
||||
public static class SessionExtension
|
||||
{
|
||||
public static IApplicationBuilder UseCurrent(this IApplicationBuilder app)
|
||||
{
|
||||
return app.UseCurrentUserContext();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using Common.NETCore.Utility;
|
||||
using Microsoft.OpenApi;
|
||||
|
||||
namespace FASS.Scheduler.Extensions.Configure
|
||||
{
|
||||
public static class SwaggerExtension
|
||||
{
|
||||
public static IServiceCollection AddSwashbuckle(this IServiceCollection services)
|
||||
{
|
||||
services.AddSwaggerGen(options =>
|
||||
{
|
||||
options.SwaggerDoc("v1", new OpenApiInfo
|
||||
{
|
||||
Title = Session.AssemblyName.Name,
|
||||
Version = Session.AssemblyName.Version?.ToString()
|
||||
});
|
||||
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
||||
{
|
||||
In = ParameterLocation.Header,
|
||||
Type = SecuritySchemeType.ApiKey,
|
||||
Name = "Authorization",
|
||||
BearerFormat = "JWT",
|
||||
Description = "Value {Bearer Token}"
|
||||
});
|
||||
options.AddSecurityRequirement(document => new OpenApiSecurityRequirement
|
||||
{
|
||||
[new OpenApiSecuritySchemeReference("Bearer", document)] = []
|
||||
});
|
||||
options.OrderActionsBy(api => api.RelativePath);
|
||||
//options.TagActionsBy(api => [api.HttpMethod]);
|
||||
});
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IApplicationBuilder UseSwashbuckle(this IApplicationBuilder app)
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(options =>
|
||||
{
|
||||
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
|
||||
});
|
||||
return app;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using Common.AspNetCore.Helpers;
|
||||
using FASS.Scheduler.Models;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace FASS.Scheduler.Extensions
|
||||
{
|
||||
public static class TokenExtension
|
||||
{
|
||||
public static string GetToken(this AppSettings appSettings, IEnumerable<Claim> claims)
|
||||
{
|
||||
var signingKey = appSettings.Auth.SigningKey;
|
||||
var algorithm = SecurityAlgorithms.HmacSha256;
|
||||
var issuer = appSettings.Auth.Issuer;
|
||||
var audience = appSettings.Auth.Audience;
|
||||
var notBefore = DateTime.Now;
|
||||
var expires = notBefore.AddSeconds(appSettings.Auth.ExpireSeconds);
|
||||
var token = JwtHelper.CreateToken(signingKey, algorithm, issuer, audience, claims, notBefore, expires);
|
||||
return token;
|
||||
}
|
||||
|
||||
public static string RefreshToken(this AppSettings appSettings, string token)
|
||||
{
|
||||
var signingKey = appSettings.Auth.SigningKey;
|
||||
var refreshToken = JwtHelper.RefreshToken(token, signingKey);
|
||||
return refreshToken;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user