任务38:JWT 设计解析及定制

 

改造jwt token

token的值不放在Authorize里面,而是放在header的token里面

任务38:JWT 设计解析及定制

 asp.net core的源代码

Security的下面

https://github.com/aspnet/AspNetCore/tree/master/src/Security

 

 github源代码的讲解

 这是jwtBearer的认证的源码:

https://github.com/aspnet/AspNetCore/tree/master/src/Security/Authentication/JwtBearer/src

 AddJwtBearer的扩展方法在这里。

 https://github.com/aspnet/AspNetCore/blob/master/src/Security/Authentication/JwtBearer/src/JwtBearerExtensions.cs

 任务38:JWT 设计解析及定制

 

 就是我们的StartUp里面的方法调用的AddJwtBearer

任务38:JWT 设计解析及定制

 

JwtBearerHandler.cs

https://github.com/aspnet/AspNetCore/blob/master/src/Security/Authentication/JwtBearer/src/JwtBearerHandler.cs

这里提供我们一种方式Token可以从别的地方去拿,而不是从Header的Authorize的里面去拿token:

任务38:JWT 设计解析及定制

 

1分55秒

 

任务38:JWT 设计解析及定制

拿到token以后,下面会进行一些验证。我们要看关键是如何去加在自己的认证。

我们就是要加一个自己的Validate,在循环里面完成自己的业务逻辑。

也就是我们需要把token读取的地方给换掉。同时把token验证的逻辑换掉。

任务38:JWT 设计解析及定制

 

 

 

2分57秒

我们看到这里new了一个MessageReceivedContext 实际上它是包装,把我们当前的HttpContext和Scheme以为Options配置全都放进来

然后下面调用了 MessageReceived的这种方式

 任务38:JWT 设计解析及定制

 

我们的MessageReceivedContext 在这个地方:

https://github.com/aspnet/AspNetCore/blob/master/src/Security/Authentication/JwtBearer/src/MessageReceivedContext.cs

然后是:JwtBearerEvents

https://github.com/aspnet/AspNetCore/blob/master/src/Security/Authentication/JwtBearer/src/JwtBearerEvents.cs

 

我们可以把这个地方给改掉:

任务38:JWT 设计解析及定制

 

同时最下面还有TokenValidated方法,我们用来手动写一下

开始写代码

开始改写我们之前写的代码:3分27秒

我们先把原来的代码注释掉

任务38:JWT 设计解析及定制

          //SecurityTokenValidators这是一个数组,我们先把它清空掉,否则它会从里面拿验证
                o.SecurityTokenValidators.Clear();
                //new一个event,我们要对它进行改造
                o.Events = new JwtBearerEvents() {
                    OnMessageReceived = context =>{
                        var token = context.Request.Headers["mytoken"];//获取header中的token
                        context.Token = token.FirstOrDefault();//赋值给Context中的Token
                        return Task.CompletedTask;
                    }
                };

 

然后再加Validator验证

根目录下创建类:MyTokenValidator.cs

任务38:JWT 设计解析及定制

继承:ISecurityTokenValidator在这个命名空间下using Microsoft.IdentityModel.Tokens;

任务38:JWT 设计解析及定制

 

修改接口的方法的返回值。

任务38:JWT 设计解析及定制

完成认证,以及给indetity赋值的过程

任务38:JWT 设计解析及定制

 

上面判断了token的值如果不对就返回了null是不可以的。必须要返回一个principal对象,哪怕是一个空的

任务38:JWT 设计解析及定制

 

再次修改如下:

任务38:JWT 设计解析及定制

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using System.Security.Claims;
using Microsoft.IdentityModel.Tokens;

namespace JwtAuthSample
{
    public class MyTokenValidator : ISecurityTokenValidator
    {
        public bool CanValidateToken => true;

        public int MaximumTokenSizeInBytes { get; set; }

        public bool CanReadToken(string securityToken)
        {
            return true;
        }

        public ClaimsPrincipal ValidateToken(string securityToken, TokenValidationParameters validationParameters, out SecurityToken validatedToken)
        {
            //进行验证
            validatedToken = null;//要先进行赋值

            var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme);
          
            if (securityToken == "abcdefg")
            {
                identity.AddClaim(new Claim("name", "wjw"));
                identity.AddClaim(new Claim("SuperAdminOnly", "true"));
                identity.AddClaim(new Claim(ClaimsIdentity.DefaultRoleClaimType, "user"));
            }
          

            var principal = new ClaimsPrincipal(identity); ;

            return principal;//不能直接返回null这里必须返回一个pincipal对象

        }
    }
}
MyTokenValidator

相关文章:

  • 2021-07-01
  • 2021-08-27
  • 2021-05-31
  • 2022-12-23
  • 2021-11-29
  • 2021-08-22
猜你喜欢
  • 2021-11-06
  • 2021-10-16
  • 2021-04-19
  • 2021-12-23
  • 2021-09-03
  • 2021-12-21
相关资源
相似解决方案