mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-09-20 02:29:24 +08:00
🐛fix:修复正式环境没有关闭SWAGGER
This commit is contained in:
@@ -38,6 +38,7 @@ OpenAuth.Net是一个基于.NET 9的企业级权限管理和快速开发框架
|
|||||||
- 私有字段使用_camelCase,公共属性使用PascalCase
|
- 私有字段使用_camelCase,公共属性使用PascalCase
|
||||||
- 常量使用UPPER_CASE
|
- 常量使用UPPER_CASE
|
||||||
- 异步方法必须添加Async后缀
|
- 异步方法必须添加Async后缀
|
||||||
|
- 修改完代码后不需要编译
|
||||||
|
|
||||||
### 命名规范
|
### 命名规范
|
||||||
- **Controller**: 以Controller结尾,如`UsersController`
|
- **Controller**: 以Controller结尾,如`UsersController`
|
||||||
|
@@ -28,6 +28,11 @@ RUN dotnet publish -c Release -o /app/publish/identity
|
|||||||
|
|
||||||
FROM base AS final
|
FROM base AS final
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
# 设置 Production 环境变量
|
||||||
|
ENV ASPNETCORE_ENVIRONMENT=Production
|
||||||
|
ENV DOTNET_ENVIRONMENT=Production
|
||||||
|
|
||||||
# 复制 WebApi 发布文件
|
# 复制 WebApi 发布文件
|
||||||
COPY --from=build /app/publish/webapi ./webapi
|
COPY --from=build /app/publish/webapi ./webapi
|
||||||
|
|
||||||
|
41
OpenAuth.WebApi/Controllers/SystemController.cs
Normal file
41
OpenAuth.WebApi/Controllers/SystemController.cs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
|
namespace OpenAuth.WebApi.Controllers
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 系统信息相关接口
|
||||||
|
/// </summary>
|
||||||
|
[Route("api/[controller]/[action]")]
|
||||||
|
[ApiController]
|
||||||
|
public class SystemController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly IHostEnvironment _environment;
|
||||||
|
|
||||||
|
public SystemController(IHostEnvironment environment)
|
||||||
|
{
|
||||||
|
_environment = environment;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取系统环境信息
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet]
|
||||||
|
[AllowAnonymous]
|
||||||
|
public IActionResult GetEnvironmentInfo()
|
||||||
|
{
|
||||||
|
return Ok(new
|
||||||
|
{
|
||||||
|
EnvironmentName = _environment.EnvironmentName,
|
||||||
|
IsDevelopment = _environment.IsDevelopment(),
|
||||||
|
IsProduction = _environment.IsProduction(),
|
||||||
|
IsStaging = _environment.IsStaging(),
|
||||||
|
SwaggerEnabled = _environment.IsDevelopment(), // Swagger 只在开发环境启用
|
||||||
|
Timestamp = DateTime.Now
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -61,67 +61,73 @@ namespace OpenAuth.WebApi
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加MiniProfiler服务
|
// 只在开发环境中添加MiniProfiler服务
|
||||||
services.AddMiniProfiler(options =>
|
if (Environment.IsDevelopment())
|
||||||
{
|
{
|
||||||
// 设定访问分析结果URL的路由基地址
|
services.AddMiniProfiler(options =>
|
||||||
options.RouteBasePath = "/profiler";
|
{
|
||||||
|
// 设定访问分析结果URL的路由基地址
|
||||||
|
options.RouteBasePath = "/profiler";
|
||||||
|
|
||||||
options.ColorScheme = StackExchange.Profiling.ColorScheme.Auto;
|
options.ColorScheme = StackExchange.Profiling.ColorScheme.Auto;
|
||||||
options.PopupRenderPosition = StackExchange.Profiling.RenderPosition.BottomLeft;
|
options.PopupRenderPosition = StackExchange.Profiling.RenderPosition.BottomLeft;
|
||||||
options.PopupShowTimeWithChildren = true;
|
options.PopupShowTimeWithChildren = true;
|
||||||
options.PopupShowTrivial = true;
|
options.PopupShowTrivial = true;
|
||||||
options.SqlFormatter = new StackExchange.Profiling.SqlFormatters.InlineFormatter();
|
options.SqlFormatter = new StackExchange.Profiling.SqlFormatters.InlineFormatter();
|
||||||
// options.IgnoredPaths.Add("/swagger/");
|
// options.IgnoredPaths.Add("/swagger/");
|
||||||
}).AddEntityFramework(); //显示SQL语句及耗时
|
}).AddEntityFramework(); //显示SQL语句及耗时
|
||||||
|
}
|
||||||
|
|
||||||
//添加swagger
|
// 只在开发环境中添加swagger
|
||||||
services.AddSwaggerGen(option =>
|
if (Environment.IsDevelopment())
|
||||||
{
|
{
|
||||||
foreach (var controller in GetControllers())
|
services.AddSwaggerGen(option =>
|
||||||
{
|
{
|
||||||
var groupname = GetSwaggerGroupName(controller);
|
foreach (var controller in GetControllers())
|
||||||
|
|
||||||
option.SwaggerDoc(groupname, new OpenApiInfo
|
|
||||||
{
|
{
|
||||||
Version = "v1",
|
var groupname = GetSwaggerGroupName(controller);
|
||||||
Title = groupname,
|
|
||||||
Description = "by yubaolee"
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.LogInformation($"api doc basepath:{AppContext.BaseDirectory}");
|
option.SwaggerDoc(groupname, new OpenApiInfo
|
||||||
foreach (var name in Directory.GetFiles(AppContext.BaseDirectory, "*.*",
|
|
||||||
SearchOption.AllDirectories).Where(f => Path.GetExtension(f).ToLower() == ".xml"))
|
|
||||||
{
|
|
||||||
option.IncludeXmlComments(name, includeControllerXmlComments: true);
|
|
||||||
// logger.LogInformation($"find api file{name}");
|
|
||||||
}
|
|
||||||
|
|
||||||
option.OperationFilter<GlobalHttpHeaderOperationFilter>(); // 添加httpHeader参数
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(identityServer))
|
|
||||||
{
|
|
||||||
//接入identityserver
|
|
||||||
option.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
|
|
||||||
{
|
|
||||||
Type = SecuritySchemeType.OAuth2,
|
|
||||||
Description = "OAuth2登陆授权",
|
|
||||||
Flows = new OpenApiOAuthFlows
|
|
||||||
{
|
{
|
||||||
Implicit = new OpenApiOAuthFlow
|
Version = "v1",
|
||||||
|
Title = groupname,
|
||||||
|
Description = "by yubaolee"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.LogInformation($"api doc basepath:{AppContext.BaseDirectory}");
|
||||||
|
foreach (var name in Directory.GetFiles(AppContext.BaseDirectory, "*.*",
|
||||||
|
SearchOption.AllDirectories).Where(f => Path.GetExtension(f).ToLower() == ".xml"))
|
||||||
|
{
|
||||||
|
option.IncludeXmlComments(name, includeControllerXmlComments: true);
|
||||||
|
// logger.LogInformation($"find api file{name}");
|
||||||
|
}
|
||||||
|
|
||||||
|
option.OperationFilter<GlobalHttpHeaderOperationFilter>(); // 添加httpHeader参数
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(identityServer))
|
||||||
|
{
|
||||||
|
//接入identityserver
|
||||||
|
option.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
|
||||||
|
{
|
||||||
|
Type = SecuritySchemeType.OAuth2,
|
||||||
|
Description = "OAuth2登陆授权",
|
||||||
|
Flows = new OpenApiOAuthFlows
|
||||||
{
|
{
|
||||||
AuthorizationUrl = new Uri($"{identityServer}/connect/authorize"),
|
Implicit = new OpenApiOAuthFlow
|
||||||
Scopes = new Dictionary<string, string>
|
|
||||||
{
|
{
|
||||||
{"openauthapi", "同意openauth.webapi 的访问权限"} //指定客户端请求的api作用域。 如果为空,则客户端无法访问
|
AuthorizationUrl = new Uri($"{identityServer}/connect/authorize"),
|
||||||
|
Scopes = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{"openauthapi", "同意openauth.webapi 的访问权限"} //指定客户端请求的api作用域。 如果为空,则客户端无法访问
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
option.OperationFilter<AuthResponsesOperationFilter>();
|
||||||
option.OperationFilter<AuthResponsesOperationFilter>();
|
}
|
||||||
}
|
});
|
||||||
});
|
}
|
||||||
services.Configure<AppSetting>(Configuration.GetSection("AppSetting"));
|
services.Configure<AppSetting>(Configuration.GetSection("AppSetting"));
|
||||||
services.AddControllers(option => { option.Filters.Add<OpenAuthFilter>(); })
|
services.AddControllers(option => { option.Filters.Add<OpenAuthFilter>(); })
|
||||||
.ConfigureApiBehaviorOptions(options =>
|
.ConfigureApiBehaviorOptions(options =>
|
||||||
@@ -270,34 +276,37 @@ namespace OpenAuth.WebApi
|
|||||||
//配置ServiceProvider
|
//配置ServiceProvider
|
||||||
AutofacContainerModule.ConfigServiceProvider(app.ApplicationServices);
|
AutofacContainerModule.ConfigServiceProvider(app.ApplicationServices);
|
||||||
|
|
||||||
|
// 只在开发环境中启用 Swagger 和 MiniProfiler
|
||||||
if (env.IsDevelopment())
|
if (env.IsDevelopment())
|
||||||
{
|
{
|
||||||
app.UseMiniProfiler();
|
app.UseMiniProfiler();
|
||||||
app.UseDeveloperExceptionPage();
|
app.UseDeveloperExceptionPage();
|
||||||
app.UseSwagger();
|
app.UseSwagger();
|
||||||
|
|
||||||
|
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
|
||||||
|
// specifying the Swagger JSON endpoint.
|
||||||
|
app.UseSwaggerUI(c =>
|
||||||
|
{
|
||||||
|
c.IndexStream = () =>
|
||||||
|
IntrospectionExtensions.GetTypeInfo(GetType()).Assembly
|
||||||
|
.GetManifestResourceStream("OpenAuth.WebApi.index.html");
|
||||||
|
|
||||||
|
foreach (var controller in GetControllers())
|
||||||
|
{
|
||||||
|
var groupname = GetSwaggerGroupName(controller);
|
||||||
|
|
||||||
|
c.SwaggerEndpoint($"/swagger/{groupname}/swagger.json", groupname);
|
||||||
|
}
|
||||||
|
|
||||||
|
c.DocExpansion(DocExpansion.List); //默认展开列表
|
||||||
|
c.OAuthClientId("OpenAuth.WebApi"); //oauth客户端名称
|
||||||
|
c.OAuthAppName("开源版webapi认证"); // 描述
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
|
|
||||||
// specifying the Swagger JSON endpoint.
|
|
||||||
app.UseSwaggerUI(c =>
|
|
||||||
{
|
|
||||||
c.IndexStream = () =>
|
|
||||||
IntrospectionExtensions.GetTypeInfo(GetType()).Assembly
|
|
||||||
.GetManifestResourceStream("OpenAuth.WebApi.index.html");
|
|
||||||
|
|
||||||
foreach (var controller in GetControllers())
|
|
||||||
{
|
|
||||||
var groupname = GetSwaggerGroupName(controller);
|
|
||||||
|
|
||||||
c.SwaggerEndpoint($"/swagger/{groupname}/swagger.json", groupname);
|
|
||||||
}
|
|
||||||
|
|
||||||
c.DocExpansion(DocExpansion.List); //默认展开列表
|
|
||||||
c.OAuthClientId("OpenAuth.WebApi"); //oauth客户端名称
|
|
||||||
c.OAuthAppName("开源版webapi认证"); // 描述
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
Reference in New Issue
Block a user