mirror of
https://gitee.com/ccnetcore/Yi
synced 2026-04-12 12:16:38 +08:00
修改前端组件、后端添加jwt
This commit is contained in:
@@ -1,47 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Yi.Framework.Core
|
||||
|
||||
{
|
||||
/// <summary>
|
||||
/// 加密用的
|
||||
/// </summary>
|
||||
public class MD5Helper
|
||||
{
|
||||
/// <summary>
|
||||
/// MD5 加密字符串
|
||||
/// </summary>
|
||||
/// <param name="content">源字符串</param>
|
||||
/// <returns>加密后字符串</returns>
|
||||
public static string MD5EncodingOnly(string content)
|
||||
{
|
||||
// 创建MD5类的默认实例:MD5CryptoServiceProvider
|
||||
MD5 md5 = MD5.Create();
|
||||
byte[] bs = Encoding.UTF8.GetBytes(content);
|
||||
byte[] hs = md5.ComputeHash(bs);
|
||||
StringBuilder stb = new StringBuilder();
|
||||
foreach (byte b in hs)
|
||||
{
|
||||
// 以十六进制格式格式化
|
||||
stb.Append(b.ToString("x2"));
|
||||
}
|
||||
return stb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// MD5盐值加密
|
||||
/// </summary>
|
||||
/// <param name="content">源字符串</param>
|
||||
/// <param name="salt">盐值</param>
|
||||
/// <returns>加密后字符串</returns>
|
||||
public static string MD5EncodingWithSalt(string content, string salt)
|
||||
{
|
||||
if (salt == null) return content;
|
||||
return MD5EncodingOnly(content + "{" + salt.ToString() + "}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
53
Yi.Framework/Yi.Framework.Core/MakeJwt.cs
Normal file
53
Yi.Framework/Yi.Framework.Core/MakeJwt.cs
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CC.ElectronicCommerce.Core
|
||||
{
|
||||
public static class SnowflakeHelper
|
||||
{
|
||||
public static long Next()
|
||||
{
|
||||
SnowflakeTool snowflakeTool = new SnowflakeTool(1);
|
||||
return snowflakeTool.NextId();
|
||||
}
|
||||
|
||||
private class SnowflakeTool
|
||||
{
|
||||
//机器ID
|
||||
private static long nodeId;
|
||||
private static long twepoch = 687888001020L; //唯一时间,这是一个避免重复的随机量,自行设定不要大于当前时间戳
|
||||
private static long sequence = 0L;
|
||||
private static int workerIdBits = 4; //机器码字节数。4个字节用来保存机器码(定义为Long类型会出现,最大偏移64位,所以左移64位没有意义)
|
||||
public static long maxWorkerId = -1L ^ -1L << workerIdBits; //最大机器ID
|
||||
private static int sequenceBits = 10; //计数器字节数,10个字节用来保存计数码
|
||||
private static int workerIdShift = sequenceBits; //机器码数据左移位数,就是后面计数器占用的位数
|
||||
private static int timestampLeftShift = sequenceBits + workerIdBits; //时间戳左移动位数就是机器码和计数器总字节数
|
||||
public static long sequenceMask = -1L ^ -1L << sequenceBits; //一微秒内可以产生计数,如果达到该值则等到下一微妙在进行生成
|
||||
private long lastTimestamp = -1L;
|
||||
|
||||
/// <summary>
|
||||
/// 机器码
|
||||
/// </summary>
|
||||
/// <param name="workerId"></param>
|
||||
public SnowflakeTool(long workerId)
|
||||
{
|
||||
if (workerId > maxWorkerId || workerId < 0)
|
||||
throw new Exception(string.Format("节点id 不能大于 {0} 或者 小于 0 ", workerId));
|
||||
SnowflakeTool.nodeId = workerId;
|
||||
|
||||
}
|
||||
|
||||
public long NextId()
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
long timestamp = TimeGen();
|
||||
if (this.lastTimestamp == timestamp)
|
||||
{ //同一微妙中生成ID
|
||||
SnowflakeTool.sequence = (SnowflakeTool.sequence + 1) & SnowflakeTool.sequenceMask; //用&运算计算该微秒内产生的计数是否已经到达上限
|
||||
if (SnowflakeTool.sequence == 0)
|
||||
{
|
||||
//一微妙内产生的ID计数已达上限,等待下一微妙
|
||||
timestamp = TillNextMillis(this.lastTimestamp);
|
||||
}
|
||||
}
|
||||
else
|
||||
{ //不同微秒生成ID
|
||||
SnowflakeTool.sequence = 0; //计数清0
|
||||
}
|
||||
if (timestamp < lastTimestamp)
|
||||
{ //如果当前时间戳比上一次生成ID时时间戳还小,抛出异常,因为不能保证现在生成的ID之前没有生成过
|
||||
throw new Exception(string.Format("Clock moved backwards. Refusing to generate id for {0} milliseconds",
|
||||
this.lastTimestamp - timestamp));
|
||||
}
|
||||
this.lastTimestamp = timestamp; //把当前时间戳保存为最后生成ID的时间戳
|
||||
long nextId = (timestamp - twepoch << timestampLeftShift) | SnowflakeTool.nodeId << SnowflakeTool.workerIdShift | SnowflakeTool.sequence;
|
||||
return nextId;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下一微秒时间戳
|
||||
/// </summary>
|
||||
/// <param name="lastTimestamp"></param>
|
||||
/// <returns></returns>
|
||||
private long TillNextMillis(long lastTimestamp)
|
||||
{
|
||||
long timestamp = TimeGen();
|
||||
while (timestamp <= lastTimestamp)
|
||||
{
|
||||
timestamp = TimeGen();
|
||||
}
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成当前时间戳
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private long TimeGen()
|
||||
{
|
||||
return (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user