修改前端组件、后端添加jwt

This commit is contained in:
橙子
2021-10-14 20:29:07 +08:00
parent 40f34618a2
commit 9ce9d4ed98
17 changed files with 407 additions and 208 deletions

View File

@@ -4,8 +4,9 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Yi.Framework.Common;
using Yi.Framework.Common.Helper;
using Yi.Framework.Common.Models;
using Yi.Framework.Core;
using Yi.Framework.Interface;
using Yi.Framework.Model.Models;
@@ -33,9 +34,11 @@ namespace Yi.Framework.ApiMicroservice.Controllers
[HttpPost]
public async Task<Result> Login(user _user)
{
if( await _userService.Login(_user))
if (await _userService.Login(_user))
{
return Result.Success().SetData(new { _user, token = 123456789 });
_user.roles = await _userService.GetRolesByUser(_user);
var toke = MakeJwt.app(_user);
return Result.Success().SetData(new { user = new { _user.id, _user.username, _user.introduction, _user.icon, _user.nick }, toke });
}
return Result.Error();
}
@@ -45,7 +48,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
/// </summary>
/// <returns></returns>
[HttpPost]
public Result Logout()
public Result Logout()
{
return Result.Success();
}
@@ -59,11 +62,11 @@ namespace Yi.Framework.ApiMicroservice.Controllers
[HttpPost]
public async Task<Result> Register(user _user, string code)
{
if (code!=null)
{
if (code != null)
{
await _userService.Register(_user);
}
return Result.Error();
return Result.Error();
}
/// <summary>

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Yi.Framework.Common.Const
{
public class JwtConst
{
public const string SecurityKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDI2a2EJ7m872v0afyoSDJT2o1+SitIeJSWtLJU8/Wz2m7gStexajkeD+Lka6DSTy8gt9UwfgVQo6uKjVLG5Ex7PiGOODVqAEghBuS7JzIYU5RvI543nNDAPfnJsas96mSA7L/mD7RTE2drj6hf3oZjJpMPZUQI/B1Qjb5H3K3PNwIDAQAB";
public const string Domain = "https://localhost:44329";
}
}

View File

@@ -5,7 +5,7 @@ using System.Net.Mail;
using System.Net.Sockets;
using System.Text;
namespace Yi.Framework.Common
namespace Yi.Framework.Common.Helper
{
public class EmailHelper
{

View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
namespace Yi.Framework.Core
namespace Yi.Framework.Common.Helper
{
/// <summary>

View File

@@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CC.ElectronicCommerce.Core
namespace Yi.Framework.Common.Helper
{
public static class SnowflakeHelper
{

View File

@@ -0,0 +1,53 @@
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Common.Const;
using Yi.Framework.Model.Models;
using JwtRegisteredClaimNames = Microsoft.IdentityModel.JsonWebTokens.JwtRegisteredClaimNames;
namespace Yi.Framework.Core
{
public class MakeJwt
{
/// <summary>
/// user需关联所有roles
/// </summary>
/// <param name="_user"></param>
/// <returns></returns>
public static string app(user _user)
{
//通过查询权限,把所有权限加入进令牌中
List<Claim> claims = new List<Claim>();
claims.Add(new Claim(JwtRegisteredClaimNames.Nbf, $"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}"));
claims.Add(new Claim(JwtRegisteredClaimNames.Exp, $"{new DateTimeOffset(DateTime.Now.AddMinutes(30)).ToUnixTimeSeconds()}"));
claims.Add(new Claim(ClaimTypes.Name, _user.username));
claims.Add(new Claim(ClaimTypes.Sid, _user.id.ToString()));
foreach (var k in _user.roles)
{
claims.Add(new Claim(ClaimTypes.Role, k.role_name));
}
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(JwtConst.SecurityKey));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
issuer: JwtConst.Domain,
audience: JwtConst.Domain,
claims: claims,
expires: DateTime.Now.AddMinutes(30),
signingCredentials: creds);
var tokenData = new JwtSecurityTokenHandler().WriteToken(token);
return tokenData;
}
}
}

View File

@@ -0,0 +1,39 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using System;
using System.IO;
using System.Text;
using Yi.Framework.Common.Const;
namespace Yi.Framework.WebCore.MiddlewareExtend
{
/// <summary>
/// 通用跨域扩展
/// </summary>
public static class JwtExtension
{
public static IServiceCollection AddJwtService(this IServiceCollection services)
{
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,//是否验证Issuer
ValidateAudience = true,//是否验证Audience
ValidateLifetime = true,//是否验证失效时间
ClockSkew = TimeSpan.FromDays(1),
ValidateIssuerSigningKey = true,//是否验证SecurityKey
ValidAudience = JwtConst.Domain,//Audience
ValidIssuer = JwtConst.Domain,//Issuer这两项和前面签发jwt的设置一致
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(JwtConst.SecurityKey))//拿到SecurityKey
};
});
return services;
}
}
}