8000 添加Api跨域配置信息的JSON配置节点支持 · Issue #186 · dotnetcore/osharp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

添加Api跨域配置信息的JSON配置节点支持 #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gmf520 opened this issue Sep 7, 2020 · 2 comments
Closed

添加Api跨域配置信息的JSON配置节点支持 #186

gmf520 opened this issue Sep 7, 2020 · 2 comments
Labels
Feature 🔨 新功能,新特性 Finished ✔️ 实现并完工
Milestone

Comments

@gmf520
Copy link
Member
gmf520 commented Sep 7, 2020

您的功能请求与现有问题有关吗?请描述

现在跨域配置需要自己写代码实现,可以通过读取JSON配置信息来进行配置

@gmf520 gmf520 added the Feature 🔨 新功能,新特性 label Sep 7, 2020
@gmf520 gmf520 added this to the vNext milestone Sep 7, 2020
@wgs004
Copy link
wgs004 commented Dec 12, 2020

您好,现在跨域策略的增加是通过继承MvcPack类,重写AddCors方法来实现,但是FunctionAuthorizationPackBase类又DependsOnPacks于MvcFunctionPack,而MvcFunctionPack依赖于MvcPack,导致冲突:如果自己继承MvcPack会导致底层初始化pack时将MvcPack替换掉,导致系统异常找不到MvcPack,请问目前有解决的方法吗?

@gmf520
Copy link
Member Author
gmf520 commented Dec 13, 2020

添加新接口 ICorsInitializer 用于将Cors的初始化业务从MvcPack中剥离出来,防止自定义Cors时需要重写MvcPack导致 @wgs004 提到的冲突情况出现

ICorsInitializer

namespace OSharp.AspNetCore.Cors
{
    /// <summary>
    /// 定义Cors初始化器
    /// </summary>
    public interface ICorsInitializer
    {
        /// <summary>
        /// 添加Cors
        /// </summary>
        IServiceCollection AddCors(IServiceCollection services);

        /// <summary>
        /// 应用Cors
        /// </summary>
        IApplicationBuilder UseCors(IApplicationBuilder app);
    }
}

DefaultCorsInitializer

实现了一个常规的Cors初始化器

namespace OSharp.AspNetCore.Cors
{
    /// <summary>
    /// 默认Cors初始化器
    /// </summary>
    public class DefaultCorsInitializer : ICorsInitializer
    {
        private CorsOptions _cors;

        /// <summary>
        /// 添加Cors
        /// </summary>
        public virtual IServiceCollection AddCors(IServiceCollection services)
        {
            IConfiguration configuration = services.GetConfiguration();
            CorsOptions cors = new CorsOptions();
            configuration.Bind("OSharp:Cors", cors);
            _cors = cors;
            if (!cors.Enabled)
            {
                return services;
            }

            if (string.IsNullOrEmpty(cors.PolicyName))
            {
                throw new OsharpException("配置文件中OSharp:Cors节点的PolicyName不能为空");
            }

            services.AddCors(opts => opts.AddPolicy(cors.PolicyName,
                policy =>
                {
                    if (cors.AllowAnyHeader)
                        policy.AllowAnyHeader();
                    else if (cors.WithHeaders != null)
                        policy.WithHeaders(cors.WithHeaders);

                    if (cors.AllowAnyMethod)
                        policy.AllowAnyMethod();
                    else if (cors.WithMethods != null)
                        policy.WithMethods(cors.WithMethods);

                    if (cors.AllowCredentials) policy.AllowCredentials();
                    else if (cors.DisallowCredentials) policy.DisallowCredentials();

                    if (cors.AllowAnyOrigin) policy.AllowAnyOrigin();
                    else if (cors.WithOrigins != null) policy.WithOrigins(cors.WithOrigins);
                }));

            return services;
        }

        /// <summary>
        /// 应用Cors
        /// </summary>
        public virtual IApplicationBuilder UseCors(IApplicationBuilder app)
        {
            if (_cors.Enabled)
            {
                app.UseCors(_cors.PolicyName);
            }

            return app;
        }
    }
}

CorsOptions

相应的配置选项类

namespace OSharp.Core.Options
{
    /// <summary>
    /// Cors选项
    /// </summary>
    public class CorsOptions
    {
        /// <summary>
        /// 获取或设置 策略名称
        /// </summary>
        public string PolicyName { get; set; }

        /// <summary>
        /// 获取或设置 允许任意请求头
        /// </summary>
        public bool AllowAnyHeader { get; set; }

        /// <summary>
        /// 获取或设置 允许的请求头
        /// </summary>
        public string[] WithHeaders { get; set; }

        /// <summary>
        /// 获取或设置 允许任意方法
        /// </summary>
        public bool AllowAnyMethod { get; set; }

        /// <summary>
        /// 获取或设置 允许的方法
        /// </summary>
        public string[] WithMethods { get; set; }

        /// <summary>
        /// 获取或设置 允许跨域凭据
        /// </summary>
        public bool AllowCredentials { get; set; }

        /// <summary>
        /// 获取或设置 禁止跨域凭据
        /// </summary>
        public bool DisallowCredentials { get; set; }

        /// <summary>
        /// 获取或设置 允许任意来源
        /// </summary>
        public bool AllowAnyOrigin { get; set; }

        /// <summary>
        /// 获取或设置 允许的来源
        /// </summary>
        public string[] WithOrigins { get; set; }

        /// <summary>
        /// 获取或设置 是否启用
        /// </summary>
        public bool Enabled { get; set; }
    }
}

配置选项类型CorsOptions对应的JSON配置节点,位于 OSharp:Cors,示例如下:

    "Cors": {
      "PolicyName": "MyCors",
      "AllowAnyHeader": true,
      "WithMethods": [ "POST", "PUT", "DELETE" ],
      "WithOrigins": [ "http://example.com" ],
      "Enabled": true 
    }

如果要自定义ICorsInitializer实现,需要在AddOSharp之前添加

services.AddSingleton<ICorsInitializer>(new MyCorsInitializer());

@gmf520 gmf520 closed this as completed Dec 13, 2020
@gmf520 gmf520 added Fixed ✔️ bug已修复 Finished ✔️ 实现并完工 and removed Fixed ✔️ bug已修复 labels Dec 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature 🔨 新功能,新特性 Finished ✔️ 实现并完工
Projects
None yet
Development

No branches or pull requests

2 participants
0