03-Jwt在.netcore中的实现
1)jwt的加密解密过程
jwt验证的核心就是加密解密的过程,掌握了这个过程,也就掌握了jwt的原理。jwt的三部分中,header和payload是明文的,能够直接读出来,签名Signature部分是进行了加密处理的。
Signature的加密过程
为了得到签名部分,你必须有编码过的header、编码过的payload、一个秘钥,签名算法是header中指定的那个,然对它们签名即可。一般的编码是base64url编码格式。
例如:
HMACSHA256(base64UrlEncode(header) + "." + base64UrlEncode(payload), secret)
签名是用于验证消息在传递过程中有没有被更改,并且,对于使用私钥签名的token,它还可以验证JWT的发送方是否为它所称的发送方。
解密方也会拿着秘钥对token最对应的解密,然后将解密的内容和原文内容做对比,如果一样就没有被篡改。
2)代码实现主要包含两部分,授权中心(token颁发),api服务器(验证token)
相对来说颁发token比较简单,上代码
[HttpPost]
public string Login()
{
var claims = new[]
{
new Claim(ClaimTypes.Name, "TestUser1"),
new Claim("Role","Administrator"),//传递其他信息
};
return CreateAccessToken(claims, _configuration);
}
private string CreateAccessToken(IEnumerable<Claim> claims, IConfiguration configuration, TimeSpan? expiration = null)
{
var now = DateTime.UtcNow;
var SecurityKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(configuration["Authentication:JwtBearer:SecurityKey"]));
var jwtSecurityToken = new JwtSecurityToken(
issuer: configuration["Authentication:JwtBearer:Issuer"],
audience: configuration["Authentication:JwtBearer:Audience"],
claims: claims,
notBefore: now,
expires: now.Add(expiration ?? TimeSpan.FromSeconds(configuration.GetValue<long>("Authentication:JwtBearer:Expiration"))),
signingCredentials: new SigningCredentials(SecurityKey, SecurityAlgorithms.HmacSha256)
);
return new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);
}
这里只是省去了验证用户登录的过程,架设登录验证通过,直接生成token。
验证端相对代码逻辑会多一些,继续上代码:
public static void Configure(IServiceCollection services, IConfiguration configuration)
{
if (bool.Parse(configuration["Authentication:JwtBearer:IsEnabled"]))
{
string authenticationScheme = JwtBearerDefaults.AuthenticationScheme;
services.AddAuthentication(authenticationScheme)
.AddJwtBearer(authenticationScheme, options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
SaveSigninToken = false,
ValidateActor = false,
ValidateTokenReplay = false,
// The signing key must match!
RequireSignedTokens = true,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(configuration["Authentication:JwtBearer:SecurityKey"])),
// Validate the JWT Issuer (iss) claim
ValidateIssuer = true,
ValidIssuer = configuration["Authentication:JwtBearer:Issuer"],
// Validate the JWT Audience (aud) claim
RequireAudience = true,
ValidateAudience = true,
ValidAudience = configuration["Authentication:JwtBearer:Audience"],
// Validate the token expiry
RequireExpirationTime = true,
ValidateLifetime = true,
// If you want to allow a certain amount of clock drift, set that here
ClockSkew = TimeSpan.Zero
};
});
}
}
这里面的配置信息和颁发端的配置要保持一致,否则无法验证通过。
验证端需要再控制器或者Action中加入验证,继续上代码
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
//Jwt
AuthConfigurer.Configure(services, Configuration);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
#region jwt
//身份验证又称“认证”、“鉴权”,是指通过一定的手段,完成对用户身份的确认。
//身份验证的目的是确认当前所声称为某种身份的用户,确实是所声称的用户。
app.UseAuthentication();//注意添加这一句,启用验证
#endregion
app.UseRouting();
//授权一般是指对信息安全或计算机安全相关的资源定义与授予访问权限,尤指访问控制。
//动词“授权”可指定义访问策略与接受访问。
//例如,人力资源人员通常被授权访问员工记录,而这个策略通常被形式化为计算机系统中的访问控制规则。
//在运行期间,系统使用已定义的访问控制规则决定是接受还是拒绝经过身份验证的访问请求。
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
控制器访问限制:
[ApiController]
[Route("[controller]/[action]")]
//[Authorize]
public class BaseController : ControllerBase, IActionFilter
{
protected IEnumerable<Claim> claims { get; set; }
public void OnActionExecuted(ActionExecutedContext context)
{
}
public void OnActionExecuting(ActionExecutingContext context)
{
//claims = context.HttpContext.AuthenticateAsync().Result.Principal.Claims;//这种方式必须在控制器上方拥有[Authorize]特性时才可用
claims = context.HttpContext.User.Claims;
}
}
[ApiController]
[Route("[controller]/[action]")]
[Authorize]
public class PatientController : BaseController
{
private readonly ILogger<PatientController> _logger;
public PatientController(ILogger<PatientController> logger)
{
_logger = logger;
}
[AllowAnonymous]
[HttpGet]
public async Task<string> GetByPatientId(long patientId)
{
return await Task.FromResult($"GetByPatientId:{patientId}");
}
[HttpPost]
public async Task<Patient> CreateorUpdatePatient([FromBody] Patient patient)
{
var claims = this.claims;
patient.PatientName = "PatientName";
return await Task.FromResult(patient);
}
}
这里我们自定义了一个控制器的基类,实现了接口IActionFilter的两个方法,在控制器执行中,先执行了 OnActionExecuting,这里面可以获得token中的所有Claims,两种方法见代码,一种是直接使用
HttpContext.User.Claims
一种是在控制器拥有[Authorize]特性,然后可以使用
HttpContext.AuthenticateAsync().Result.Principal.Claims;
综上,jwt就可以实现了。至于过期时候刷新token,异地登录之后本地token失效等逻辑,单纯的依靠jwt是无法实现的,有想法的同学欢迎探讨。
附代码(支持国产):demo
03-Jwt在.netcore中的实现的更多相关文章
- .NetCore中的日志(2)集成第三方日志工具
.NetCore中的日志(2)集成第三方日志工具 0x00 在.NetCore的Logging组件中集成NLog 上一篇讨论了.NetCore中日志框架的结构,这一篇讨论一下.NetCore的Logg ...
- .NetCore中的日志(1)日志组件解析
.NetCore中的日志(1)日志组件解析 0x00 问题的产生 日志记录功能在开发中很常用,可以记录程序运行的细节,也可以记录用户的行为.在之前开发时我一般都是用自己写的小工具来记录日志,输出目标包 ...
- AutoMapper在asp.netcore中的使用
# AutoMapper在asp.netcore中的使用 automapper 是.net 项目中针对模型之间转换映射的一个很好用的工具,不仅提高了开发的效率还使代码更加简洁,当然也是开源的,htt ...
- netcore中的缓存介绍
Cache(缓存)是优化web应用的常用方法,缓存存放在服务端的内存中,被所有用户共享.由于Cache存放在服务器的内存中,所以用户获取缓存资源的速度远比从服务器硬盘中获取快,但是从资源占有的角度考虑 ...
- 在netcore中如何注入同一个接口的多个实现
netcore中自带了Ioc框架,这也影响了我们的编码习惯,以前都是静态类或者直接new对象,现在有了Ioc框架的支持,我们也不必守旧,应当使用起来,接受这种对象管理方式.使用过java的同仁,都习惯 ...
- .NetCore中EFCore的使用整理(二)-关联表查询
EF常用处理关联加载的方式有3中:延迟加载(Lazy Loading).贪婪加载 (Eager Loading)以及显示加载. 一.EF Core 1.1 1.当前的版本,还不支持延迟加载(Lazy ...
- .NetCore中EFCore for MySql整理(三)之Pomelo.EntityFrameworkCore.MySql
一.Pomelo.EntityFrameworkCore.MySql简介 Git源代码地址:https://github.com/PomeloFoundation/Pomelo.EntityFrame ...
- .NetCore中如何实现权限控制 基于Claim角色、策略、基于Claim功能点处理
.NetCore中如果实现权限控制的问题,当我们访问到一个Action操作的时候,我们需要进行权限控制 基于claims 角色控制 基于角色控制总觉得范围有点过大,而且控制起来感觉也不是太好,举一个例 ...
- .NetCore中EFCore for MySql整理(二)
一.简介 EF Core for MySql的官方版本MySql.Data.EntityFrameworkCore 目前正是版已经可用当前版本v6.10,对于以前的预览版参考:http://www.c ...
- NetCore中使用Myrmec
NetCore中使用Myrmec Myrmec 是什么? Myrmec 是一个用于检测文件格式的库,Myrmec不同于其它库或者手写检测代码,Myrmec不依赖文件扩展名(在实际使用中,你的用户很可能 ...
随机推荐
- QPointer的使用以及场景
QPointer的使用以及场景 在我们项目开发中,经常会遇到这种情况,在A中引用了B的对象,但是你却不知道B什么时候会析构,所以使用它会出现异常:所以今天的主角要登场了QPointer可以完美的解决这 ...
- 【小技巧】java的List分页
今天,工作上,由于业务的一些特殊性,需要拿到数据后在java代码中进行分页. 写了一个工具类,记录如下: import java.util.ArrayList; import java.util.Li ...
- Eclipse插件 -- 阿里巴巴扫描编码规插件
一.github地址: https://github.com/alibaba/p3c 二..eclipse插件的安装 此处示例采用eclipse,版本为 Neon.1 Release RC3 (4.6 ...
- docker安装与配置nginx详细过程
注:大鸟飞过,此方式只用于快速搭建使用 第一步 pull nginx 命令:docker pull nginx 第二步 启动nginx 命令:docker run --name nginx -p 80 ...
- for循环操作(for...in、forEach)
1.for...in语句用于对数组或者对象的属性进行循环操作,是for循环的一种. 注意:该方法可用于数组或对象. 语法: for(变量 in 对象/数组){} 如: var obj = { nam ...
- mybaits源码分析--类型转换模块(三)
一.类型转换模块 String sql = "SELECT id,user_name,real_name,password,age,d_id from t_user where id = ? ...
- Data Augmentation
常见操作: 水平翻转-Horizontal Flip Scale-亮度变化 透视变换-perspective,旋转-rotation,错切-Shear,仿射-affine等 尺寸变幻-Resize,可 ...
- 用C++实现的增强Eratosthenes筛法程序
运行示例 PS H:\Read\num\x64\Release> .\eSievePro Eratosthenes sieve: a method to find out all primes ...
- java变量类型和常量类型
变量类型 局部变量 实例变量 类变量 public class 变量类型 { //属性:变量 //必须先定义再使用,并初始化 //布尔型:默认值为false //3. 类变量(静态变量) static ...
- 20201219 u,v,w
开考前刚起床,所以一边考一边吃饭,然后整场都很迷... A. u 考场 半天才搞懂"下三角区域"指哪个区域,手模样例确认后打了 \(O(qn^2)\) 的裸暴力,然后就不会做了. ...