2

我正在ocelot(asp.net核心中的api网关)中使用jwt auth设置身份验证,但没有成功。我在邮递员中执行我的代码,但输出不符合预期。

这是 Startup.cs 中的 ConfigureServices 我从 appsettings.json 获取 ApiKey,并添加了 Jwt 的参数,例如 issuer、audience 和 IssuerSigningKey。

public void ConfigureServices(IServiceCollection services)
        {
            //security key
            string securityKey = Configuration.GetValue<string>("ApiKey:DefaultKey");

            //symmetric security key
            var symmetricSecurityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(securityKey));

            services.AddDbContext<JwtEPDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options =>
                {
                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidateIssuer = true,
                        ValidateAudience = true,
                        ValidateIssuerSigningKey = true,

                        ValidIssuer = "smesk.in",
                        ValidAudience = "readers",
                        IssuerSigningKey = symmetricSecurityKey
                    };
                });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.AddOcelot(Configuration);
        }

还有这个 ocelot.json


{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 3000
        }
      ],
      "UpstreamPathTemplate": "/api/this-app",
      "AuthenticationOptions": {
        "SecurityKey": "0987654123456qwertysdfgvbniastrtyhajsbc8726ghuaiysdyt1276"
      }

    },

    {
      "DownstreamPathTemplate": "/api/svc_execScript2",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "10.12.4.155",
          "Port": 990

        }
      ],
      "UpstreamPathTemplate": "/api/test-ep"

    },

    {
      "DownstreamPathTemplate": "/api/svc_execScript2",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "10.12.4.155",
          "Port": 990

        }
      ],
      "UpstreamPathTemplate": "/api/test-login"

    }

  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:44319"

  }


}

我预计输出是 401 响应,但实际输出是 200

4

0 回答 0