|
@@ -1,4 +1,5 @@
|
|
|
-using System.Reflection;
|
|
|
+using System.IdentityModel.Tokens.Jwt;
|
|
|
+using System.Reflection;
|
|
|
using CallCenter.Api.Exceptions;
|
|
|
using CallCenter.Api.Filters;
|
|
|
using CallCenter.Api.Realtimes;
|
|
@@ -16,6 +17,9 @@ using CallCenter.CacheManager;
|
|
|
using FluentValidation;
|
|
|
using FluentValidation.AspNetCore;
|
|
|
using CallCenter.Settings;
|
|
|
+using Identity.Admin.HttpClient;
|
|
|
+using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
+using Microsoft.IdentityModel.Tokens;
|
|
|
using Serilog;
|
|
|
|
|
|
namespace CallCenter.Api;
|
|
@@ -44,9 +48,31 @@ internal static class StartupExtensions
|
|
|
.AddApplication()
|
|
|
;
|
|
|
|
|
|
+ services.AddIdentityClient(
|
|
|
+ new IdentityClientConfiguration("http://identity.fengwo.com", "http://open.identity.fengwo.com"),
|
|
|
+ d =>
|
|
|
+ {
|
|
|
+ d.ClientId = "hotline_server";
|
|
|
+ d.ClientSecret = "ce2fae0e-f0f6-46d6-bd79-1f1a31dff494";
|
|
|
+ d.ClientScope = "identity.admin_api";
|
|
|
+ });
|
|
|
+
|
|
|
+ JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
|
|
|
+ services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
+ .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, d =>
|
|
|
+ {
|
|
|
+ d.Authority = "http://identity.fengwo.com"; //todo
|
|
|
+ d.RequireHttpsMetadata = false;
|
|
|
+ d.TokenValidationParameters = new TokenValidationParameters
|
|
|
+ {
|
|
|
+ ValidateAudience = false
|
|
|
+ };
|
|
|
+ })
|
|
|
+ ;
|
|
|
+
|
|
|
services.AddControllers(options =>
|
|
|
{
|
|
|
- options.Filters.Add<TempTokenFilter>();
|
|
|
+ //options.Filters.Add<TempTokenFilter>();
|
|
|
options.Filters.Add<UnifyResponseFilter>();
|
|
|
options.Filters.Add<UserFriendlyExceptionFilter>();
|
|
|
});
|
|
@@ -55,16 +81,35 @@ internal static class StartupExtensions
|
|
|
services.AddSwaggerGen(c =>
|
|
|
{
|
|
|
//添加文档
|
|
|
- c.SwaggerDoc("v1", new OpenApiInfo() { Title = "WebApi", Version = "v1.0" });
|
|
|
+ c.SwaggerDoc("v1", new OpenApiInfo() { Title = "CallCenter Api", Version = "v1.0", Description = "呼叫中心api"});
|
|
|
//使用反射获取xml文件,并构造出文件的路径
|
|
|
- var xmlFile = "Document.xml";
|
|
|
+ var xmlFile = "document.xml";
|
|
|
+ //var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
|
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
|
|
// 启用xml注释. 该方法第二个参数启用控制器的注释,默认为false.
|
|
|
c.IncludeXmlComments(xmlPath, true);
|
|
|
+
|
|
|
+ var scheme = new OpenApiSecurityScheme()
|
|
|
+ {
|
|
|
+ Description = "Authorization header. \r\nExample: 'Bearer ***'",
|
|
|
+ Reference = new OpenApiReference
|
|
|
+ {
|
|
|
+ Type = ReferenceType.SecurityScheme,
|
|
|
+ Id = "Authorization"
|
|
|
+ },
|
|
|
+ Scheme = "oauth2",
|
|
|
+ Name = "Authorization",
|
|
|
+ In = ParameterLocation.Header,
|
|
|
+ Type = SecuritySchemeType.ApiKey,
|
|
|
+ };
|
|
|
+ c.AddSecurityDefinition("Authorization", scheme);
|
|
|
+ var requirement = new OpenApiSecurityRequirement();
|
|
|
+ requirement[scheme] = new List<string>();
|
|
|
+ c.AddSecurityRequirement(requirement);
|
|
|
});
|
|
|
|
|
|
//signalR
|
|
|
- builder.Services.AddSignalR().AddStackExchangeRedis("redis.fengwo.com:6380", options =>
|
|
|
+ builder.Services.AddSignalR().AddStackExchangeRedis(configuration.GetConnectionString("Redis"), options =>
|
|
|
{
|
|
|
options.Configuration.ChannelPrefix = "callcenter:signalR:";
|
|
|
});
|
|
@@ -134,11 +179,13 @@ internal static class StartupExtensions
|
|
|
|
|
|
app.UseCors(CorsOrigins);
|
|
|
|
|
|
+ app.UseAuthentication();
|
|
|
app.UseAuthorization();
|
|
|
//app.MapHub<CallCenterHub>("/hubs/callcenter");
|
|
|
//app.UseMiddleware<TempTokenMiddleware>();
|
|
|
|
|
|
- app.MapControllers();
|
|
|
+ app.MapControllers()
|
|
|
+ .RequireAuthorization();
|
|
|
|
|
|
return app;
|
|
|
}
|