解决.net6中signalr使用JWT验证出现的negotiate跨域问题

  1. 安装JWT,nuget:Microsoft.AspNetCore.Authentication.JwtBearer。
  2. 在 Program.cs 文件中signalr和JWT代码。
    //跨域处理
    builder.Services.AddCors(options =>
    {
        options.AddPolicy("allowAllCors",
        builder =>
        {
            builder
            .SetIsOriginAllowed(origin => true)
            //.AllowAnyOrigin()
            .AllowAnyHeader()
            .WithMethods(new string[] { "POST", "GET", "OPTIONS" })
            .AllowCredentials();
        });
    });
    
    //使用SignalR
    builder.Services.AddSignalR();
    
    //配置JWT验证服务
    builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
    {
        options.TokenValidationParameters = new TokenValidationParameters()
        {
            ValidateIssuer = true,
            ValidIssuer = "issuer",
            ValidateAudience = true,
            ValidAudience = "audience",
            ValidateLifetime = true,
            IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("secretKey"))
        };
        options.Events = new JwtBearerEvents
        {
            OnMessageReceived = context =>
            {
                var path = context.HttpContext.Request.Path;
                //如果是signalr请求,需要将token转存,否则JWT获取不到token。OPTIONS请求需要过滤到,因为OPTIONS请求获取不到Token,用NGINX过滤掉OPTION请求.
                if (path.StartsWithSegments("/signalr"))
                {
                    string accessToken = context.Request.Query["access_token"].ToString();
                    if (string.IsNullOrWhiteSpace(accessToken))
                    {
                        accessToken = context.Request.Headers["Authorization"].ToString();
                    }
                    context.Token = accessToken.Replace("Bearer ", "").Trim();
                }
                return Task.CompletedTask;
            }
        };
    });
    //鉴权
    app.UseAuthentication();
    //授权
    app.UseAuthorization();
    //启用跨域配置
    app.UseCors("allowAllCors");
    //启用signalR
    app.MapHub<ServerHub>("/signalr").RequireCors("allowAllCors");
  3. ServerHub添加注解。
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    [EnableCors("allowAllCors")]
    public class ServerHub : Hub{
    }
  4.  使用NGINX做个代理,过滤掉OPTIONS方法,因为预检方法无法携带Token,这样会使JWT验证不通过,导致negotiate跨域问题的出现。
        server {
           listen       8000;
           server_name  0.0.0.0;
           location /signalr {
                if ($request_method = 'OPTIONS') {
                    access_log off;
                    add_header Access-Control-Allow-Origin null;
                    add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
                    add_header Access-Control-Allow-Credentials true;
                    add_header Access-Control-Allow-Headers authorization,x-requested-with,x-signalr-user-agent;
                    # 直接在这里返回204响应,不转发到后台服务程序
                    return 204;
                }
                proxy_pass         http://x.x.x.x:5000/signalr;
                proxy_http_version 1.1;
                proxy_set_header   Upgrade $http_upgrade;
                proxy_set_header   Connection upgrade;
                proxy_set_header   Host $host;
                proxy_cache_bypass $http_upgrade;
                proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header   X-Forwarded-Proto $scheme;
        	}
        }
  5.  前端代码:
            this.socket_connection = new signalR.HubConnectionBuilder({
                skipNegotiation: true, //针对webSocket为默认协议的时候,可以跳过协商
                transport: signalR.HttpTransportType.WebSockets
            }).withUrl(this.url, {
                accessTokenFactory: () => "我的token"
            }).configureLogging(signalR.LogLevel.Error).build();

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值