.net core 实例教程(十三)配置接口JWT校验
本文源码下载地址:http://www.80cxy.com/Blog/ResourceView?arId=202403191532545995NAAqJh
系列教程地址:http://www.80cxy.com/Blog/ArticleView?arId=202403191517574161ay3s5V
本文实现访问接口时对前端传过来的token进行校验配置,也可以说是项目权限校验。首页配置项目启用JWT校验,然后通过Filter进行校验。
一、配置JWT校验
SignUp.Common项目WebApplicationBuilderExtensions类的ConfigureExtraServices方法增加如下配置代码:
JWTOptions jwtOpt = configuration.GetSection("JWT").Get<JWTOptions>(); services.AddJWTAuthentication(jwtOpt);
AddJWTAuthentication开展方法代码如下:
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Tokens; using System.Text; namespace SignUp.Common.JWT { public static class AuthenticationExtensions { /// <summary> /// 对JWT进行配置 /// </summary> /// <param name="services"></param> /// <param name="jwtOpt"></param> /// <returns></returns> public static AuthenticationBuilder AddJWTAuthentication(this IServiceCollection services, JWTOptions jwtOpt) { return services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(x => { x.TokenValidationParameters = new() { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = jwtOpt.Issuer, ValidAudience = jwtOpt.Audience, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOpt.SecKey)) }; }); } } }
在Program.cs文件app.MapControllers();代码上面增加如下配置:
app.UseForwardedHeaders(); app.UseAuthentication(); app.UseAuthorization();
二、实现IAuthorizationFilter过滤器
SignUp.Common项目Filter文件夹创建ApiAuthorizeFilter类,代码如下:
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using SignUp.Common.Commons; using SignUp.Common.Enum; using SignUp.Common.Extensions; using System.IdentityModel.Tokens.Jwt; namespace SignUp.Common.Filter { /// <summary> /// 权限过滤 /// </summary> public class ApiAuthorizeFilter : IAuthorizationFilter { private static readonly string replaceTokenPath = "/api/AdminLogin/ReplaceToken"; public void OnAuthorization(AuthorizationFilterContext context) { //控制器标记AllowAnonymous属性直接跳过 if (context.ActionDescriptor.EndpointMetadata.Any(c => c.GetType().Equals(typeof(AllowAnonymousAttribute)))) { return; } // 获取当前用户的信息 var user = context.HttpContext.User; // 如果用户未登录返回无权访问 if (user == null || !user.Identity.IsAuthenticated) { context.Result = new JsonResult(new ResponseContent().Error(ResponseType.LoginExpiration)); return; } //获取token过期时间 DateTime expDate = context.HttpContext.User.Claims.Where(x => x.Type == JwtRegisteredClaimNames.Exp) .Select(x => x.Value).FirstOrDefault().GetTimeSpmpToDate(); //判断jwt是否过期 if ((expDate - DateTime.Now).TotalMinutes < 0) { context.Result = new JsonResult(new ResponseContent().Error(ResponseType.TokenExpiration)); return; } //前端刷新token标识,离过期5分钟时开始刷新 if ((expDate - DateTime.Now).TotalMinutes < 5 && context.HttpContext.Request.Path != replaceTokenPath) { context.HttpContext.Response.Headers.Add("vol_exp", "1"); } return; } } }
三、接口增加校验规则
在需要JWT进行校验的接口上增加ApiAuthorizeFilter特性,代码如下:
/// <summary> /// 获取用户信息 /// </summary> /// <returns></returns> [HttpPost] [TypeFilter(typeof(ApiAuthorizeFilter))] public async Task<ActionResult<ResponseContent>> GetUserInfo() { ResponseContent response = new ResponseContent(); string userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier); var user = await _systemDomainService.FindOneUserById(userId); if (user == null)//可能用户注销了 { return new JsonResult(response.Error(ResponseType.LoginExpiration)); } //出于安全考虑,不要机密信息传递到客户端 //除非确认没问题,否则尽量不要直接把实体类对象返回给前端 return new JsonResult(response.Ok( new { userId = user.Id, userName = user.UserName, avatar = "", routes = new string[] { "Audit", "Preliminary", "Recheck", "System", "User", "Menu", "Role", "IndexAuth" }, roles = new string[] { "Matt", "Joanne", "Robert" }, buttons = new string[] { "Matt", "Joanne", "Robert" } })); }
学习交流
附笔者学习 .net core开发时参考相关项目实例源码:asp.net core webapi项目实例源代码锦集下载(72个)
猜您可能还喜欢
- net core+webapi+nginx windows 服务器部署(1321)
- .Nuget Packages 太占C盘,删除后可以放到其他盘(1194)
- ASP.NET Core 配置 Swagger 显示接口注释描述信息(1094)
- vue调用接口后获取不到后端返回的Header响应头(964)
- .net core 系列实例开发教程-权限管理系统功能介绍(952)
- .net core 6.0 web API + SwaggerUI + IIS部署(912)
- .net core 实例教程(十二)配置启用Swagger中的【Authorize】按钮(831)
- .net core 实例教程(一)新建项目(797)
- .net core 实例教程(十四)配置 Swagger 显示接口注释描述信息及支持版本控制(783)
- .net 6中使用log4net写sqlserver数据库日志(756)
评论列表
发表评论
文章分类
文章归档
- 2025年3月 (1)
- 2024年6月 (2)
- 2024年5月 (2)
- 2024年4月 (4)
- 2024年3月 (30)
- 2024年1月 (4)
- 2023年12月 (2)
- 2023年11月 (4)
- 2023年10月 (4)
- 2023年9月 (6)
- 2023年3月 (2)
- 2023年2月 (1)
- 2023年1月 (1)
- 2022年12月 (1)
- 2022年9月 (21)
- 2022年8月 (10)
- 2022年7月 (3)
- 2022年4月 (1)
- 2022年3月 (13)
- 2021年8月 (1)
- 2021年3月 (1)
- 2020年12月 (42)
- 2020年11月 (7)
- 2020年10月 (5)
- 2020年8月 (1)
- 2020年6月 (1)
- 2020年3月 (2)
- 2019年12月 (8)
- 2019年11月 (3)
- 2019年9月 (1)
- 2019年4月 (1)
- 2019年3月 (6)
- 2019年2月 (1)
- 2018年7月 (7)
阅读排行
- 1.asp.net mvc内微信pc端、H5、JsApi支付方式总结(5702)
- 2.各大搜索网站网站收录提交入口地址(3201)
- 3.Windows 10休眠文件更改存储位置(3163)
- 4.ECharts仪表盘实例及参数使用详解(3095)
- 5.windows 10安装myeclipse 10破解补丁cracker.jar、run.bat闪退解决办法(2991)
- 6.HTML5 WebSocket与C#建立Socket连接实现代码(2866)
- 7.华为鸿蒙系统清除微信浏览器缓存方法(2779)
- 8.CERT_HAS_EXPIRED错误如何解决(2245)
- 9.Js异步async、await关键字详细介绍(lambda表达式中使用async和await关键字)(2188)
- 10.HBuilder编辑器格式化代码(2118)