.net core8 使用JWT鉴权(附当前源码)

说明

该文章是属于OverallAuth2.0系列文章,每周更新一篇该系列文章(从0到1完成系统开发)。

该系统文章,我会尽量说的非常详细,做到不管新手、老手都能看懂。

说明:OverallAuth2.0 是一个简单、易懂、功能强大的权限+可视化流程管理系统。

结合上一篇文章使用,味道更佳:.net core8 使用Swagger(附当前源码)

有兴趣的朋友,请关注我吧(*^▽^*)。

第一步:安装最新Jwt包

包名:Microsoft.AspNetCore.Authentication.JwtBearer

第二步:appsettings.json中配置jwt

复制代码
 /*jwt鉴权*/
 "JwtSetting": {
   "Issuer": "微信公众号:不只是码农", //发行人
   "Audience": "微信公众号:不只是码农", //订阅人
   "ExpireSeconds": 120, //过期时间,默认分钟
   "ENAlgorithm": "HS256", //秘钥算法 
   "SecurityKey": "bzsmn=Start20240913EndOverallAuth-WebApi" //秘钥构成
 }

第三步:创建jwt解析模型

在OverallAuth-WebApi项目的目录下创建文件夹【model】,并创建一个类文件JwtSettingModel.cs

OverallAuth-WebApi结构,见上一篇文章:.net core8 使用Swagger(附当前源码)

复制代码
 /// <summary>
 /// jwt配置模型
 /// </summary>
 public class JwtSettingModel
 {
     /// <summary>
     /// 密钥
     /// </summary>
     public string SecurityKey { get; set; }

     /// <summary>
     /// 加密算法
     /// </summary>
     public string ENAlgorithm { get; set; }

     /// <summary>
     /// 颁发者
     /// </summary>
     public string Issuer { get; set; }

     /// <summary>
     /// 接收者
     /// </summary>
     public string Audience { get; set; }

     /// <summary>
     /// 过期时间    单位:秒
     /// </summary>
     public int ExpireSeconds { get; set; }
 }

目录结构如下:

第四步:创建Jwt、AppSettings插件

目录结构如下:

上图可以看到,我们创建了JwtPlugInUnit和AppSettingsPlugInUnit2个插件,它分别对应jwt和AppSettings配件文件的解析。

那么我们看下,这2个类里面的具体内容。

JwtPlugInUnit如下:

复制代码
/// <summary>
/// jwt插件
/// </summary>
public static class JwtPlugInUnit
{
    /// <summary>
    /// 初始化JWT
    /// </summary>
    /// <param name="services"></param>
    public static void InitJWT(this IServiceCollection services)
    {
        var jwtsetting = AppSettingsPlugInUnit.GetNode<JwtSettingModel>("JwtSetting");
        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(o =>
            {
                o.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuerSigningKey = true,
                    ValidIssuer = jwtsetting.Issuer,
                    ValidAudience = jwtsetting.Audience,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtsetting.SecurityKey)),
                    ValidateIssuer = true,
                    ValidateAudience = true,
                    ValidateLifetime = true,
                    ClockSkew = TimeSpan.Zero
                };
            });
    }
}

AppSettingsPlugInUnit如下:

复制代码
 /// <summary>
 /// AppSettings配置文件插件
 /// </summary>
 public class AppSettingsPlugInUnit
 {
     /// <summary>
     /// 声明配置属性
     /// </summary>
     public static IConfiguration Configuration { get; set; }

     /// <summary>
     /// 构造函数
     /// </summary>
     static AppSettingsPlugInUnit()
     {
         Configuration = new ConfigurationBuilder()
              .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
              .Build();
     }

     /// <summary>
     /// 获得配置文件的对象值
     /// </summary>
     /// <param name="jsonPath">文件路径</param>
     /// <param name="key"></param>
     /// <returns></returns>
     public static string GetJson(string jsonPath, string key)
     {
         if (string.IsNullOrEmpty(jsonPath) || string.IsNullOrEmpty(key)) return null;
         IConfiguration config = new ConfigurationBuilder().AddJsonFile(jsonPath).Build();//json文件地址
         return config.GetSection(key).Value;//json某个对象
     }

     /// <summary>
     /// 获取数据库连接字符串
     /// </summary>
     /// <returns></returns>
     public static string GetMysqlConnection()
     {
         return Configuration.GetConnectionString("MySql").Trim();
     }

     /// <summary>
     /// 根据节点名称获取配置模型
     /// </summary>
     /// <typeparam name="T"></typeparam>
     /// <param name="Node"></param>
     /// <returns></returns>
     public static T GetNode<T>(string Node) where T : new()
     {
         T model = Configuration.GetSection(Node).Get<T>();
         return model;

     }
 }

第五步:让jwt遵守Swagger协议

因为我们系统使用到了Swagger,所以要让jwt遵守Swagger协议,因此我们要在Swagger中添加如下代码。

复制代码
/// <summary>
/// 初始化Swagger
/// </summary>
/// <param name="services"></param>
public static void InitSwagger(this IServiceCollection services)
{
    //添加swagger
    services.AddSwaggerGen(optinos =>
    {
        typeof(ModeuleGroupEnum).GetEnumNames().ToList().ForEach(version =>
        {
            optinos.SwaggerDoc(version, new OpenApiInfo()
            {
                Title = "权限管理系统",
                Version = "V2.0",
                Description = "求关注,求一键三连",
                Contact = new OpenApiContact { Name = "微信公众号作者:不只是码农   b站作者:我不是码农呢", Url = new Uri("http://www.baidu.com") }
            });

        });

        //反射获取接口及方法描述
        var xmlFileName = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
        optinos.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFileName), true);

        //使用jwt
        optinos.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
        {
            Description = "请在下方输入框子输入Bearer Token 开启JWT鉴权",
            Name = "Authorization", // 默认名称,不能修改
            In = ParameterLocation.Header,
            Type = SecuritySchemeType.ApiKey,
            Scheme = "Bearer"
        });

        //让swagger遵守jwt协议
        optinos.AddSecurityRequirement(new OpenApiSecurityRequirement
         {
           {
             new OpenApiSecurityScheme
             {
                Reference = new OpenApiReference
                {
                     Type = ReferenceType.SecurityScheme,
                    Id = "Bearer"
                }
             },
            new List<string>()
            }
         });

    });
}

说明:InitSwagger方法是初始化Swagger的方法,在上一篇文章:.net core8 使用Swagger(附当前源码) 中有讲到。

第六步:初始化Jwt

在Program中添加一下代码,初始化Jwt

第七步:验证Jwt

做好以上步骤,jwt就可以正常使用。

当你看到图中标识时,就表示jwt初始化成功,就可以在系统中使用jwt鉴权等操作。

使用【Authorize】、【 AllowAnonymous】特性测试鉴权。

以下2个接口一个需要验证、一个不需要验证,我们来测试下。

Authorize开启验证测试

CheckJwt接口:开启验证,不传token

可以看到,开启jwt验证的,接口在没有传入token的情况下,访问失败。

UnCheckJwt接口:不开启验证。

以上就是.net core8 使用jwt系统鉴权的配置过程。

感谢你的耐心观看。

如果对你有帮助,请关注我微信公众号吧(*^▽^*)。

源代码地址:https://gitee.com/yangguangchenjie/overall-auth2.0-web-api

帮我Star,谢谢。

有兴趣的朋友,请关注我微信公众号吧(*^▽^*)。

关注我:一个全栈多端的宝藏博主,定时分享技术文章,不定时分享开源项目。关注我,带你认识不一样的程序世界

相关推荐
陪我一起学编程2 天前
vue3笔记
前端·webpack·vue3·vite·脚手架·vue3笔记·组件式
Json____4 天前
家居装修 AI 智能咨询助手:让装修咨询从“大海捞针“变成“一问即答“
java·后端·vue3·it学习·wwwoop.com
liangshanbo12155 天前
面试题:什么是 Reflect?它解决了什么问题?
vue3·reflect
RuoyiOffice5 天前
SpringBoot3+Vue3 低代码业务表单:从拖拽设计、独立建表到审批与数据查询
spring boot·vue3·数据建模·flowable·版本管理·低代码表单·ruoyi office
RuoyiOffice5 天前
SpringBoot3 企业薪酬核算:考勤、绩效与薪资规则如何算出一张可解释的工资单
spring boot·vue3·规则引擎·hrm·ruoyi office·薪酬核算·薪资试算
RuoyiOffice12 天前
2026最推荐基于SpringBoot+Vue3的项目管理经营一体化:从商机、合同、任务与工时走到回款和利润
spring boot·项目管理·vue3·crm·合同管理·ruoyi office·项目经营
RuoyiOffice13 天前
SpringBoot+Vue3 节日主题换肤实战:一条参数换全站配色,节后自动还原
spring boot·vue3·spring boot 3·vben admin·ruoyi office·节日主题·换肤方案
csdn_aspnet14 天前
.NetCore Webapi 接口验证请求来源
.netcore·ip·token·签名·webapi·cors·sign
爱喝水的鱼丶15 天前
SAP-ABAP:SAP 权限控制深度解析:自定义程序与标准事务码的边界与协同
性能优化·sap·abap·权限·开发交流·交流学习
RuoyiOffice15 天前
SpringBoot3+Vue3 流程抄送已读:打开详情消红点,待办与知会怎么分开
spring boot·vue3·flowable·spring boot 3·ruoyi office·流程抄送·已读回执