using DocumentServiceAPI.Model;
|
using DocumentServiceAPI.Services.IService;
|
using Furion.Authorization;
|
using Furion.DistributedIDGenerator;
|
using Furion.JsonSerialization;
|
using Furion.Logging.Extensions;
|
using Microsoft.IdentityModel.Tokens;
|
using NetTaste;
|
using SqlSugar.Extensions;
|
using System;
|
using System.Collections.Generic;
|
using System.IdentityModel.Tokens.Jwt;
|
using System.Linq;
|
using System.Reflection;
|
using System.Security.Claims;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace DocumentServiceAPI.Application.UserAndLogin.Services
|
{
|
/// <summary>
|
/// token服务类
|
/// </summary>
|
public class TokenService : IScoped
|
{
|
/// <summary>
|
/// Token是啥
|
/// </summary>
|
static string TokenKeys = "TokenKeys:";
|
private ISqlSugarClient _db;
|
private UserService _UserService;
|
private IRedisCacheService _RedisCase;
|
public TokenService(ISqlSugarClient db, UserService UserInfoService, IRedisCacheService RedisCase) {
|
_db = db;
|
_UserService = UserInfoService;
|
_RedisCase = RedisCase;
|
}
|
/// <summary>
|
/// 新建一个TOken
|
/// </summary>
|
/// <returns></returns>
|
public string CreateToken(JwtInfo jwt)
|
{
|
string TokenKey = TokenKeys + getTokenKey(jwt);
|
IDictionary<string, object> propertyDictionary = new Dictionary<string, object>();
|
|
PropertyInfo[] properties = jwt.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
|
foreach (PropertyInfo property in properties)
|
{
|
string propertyName = property.Name;
|
object propertyValue = property.GetValue(jwt);
|
|
propertyDictionary.Add(propertyName.ToLower(), propertyValue);
|
}
|
var ID = IDGen.NextID();
|
jwt.JID = ID;
|
var token = JWTEncryption.Encrypt(propertyDictionary, App.GetConfig<JWTSettingsOptions>("JWTSettings").ExpiredTime ?? 3600);
|
if (jwt.LogInSource == LogInFrom.PC)
|
_RedisCase.Add<string>(TokenKey, ID.ToString(), expireSeconds: (int)(App.GetConfig<JWTSettingsOptions>("JWTSettings").ExpiredTime ?? 3600));
|
return token;
|
}
|
|
/// <summary>
|
/// 校验token 的有效性
|
/// </summary>
|
/// <param name="Token"> Token校验</param>
|
/// <param name="validationParameters"></param>
|
/// <param name="securityToken"></param>
|
/// <returns></returns>
|
public bool CheckJwt(string Token, out SecurityToken? securityToken)
|
{
|
var configuration = App.Configuration;
|
// var value = configuration["xxx:xxx"];
|
|
string secretKey = configuration["ZCUserInfoJwtOP:SecurityKey"];
|
if (secretKey == null)
|
{
|
"没有配置用户中心的安全秘钥 ZCUserInfoJwtOP:SecurityKey 找不到".LogInformation<TokenService>();
|
throw Oops.Oh("配置错误,联系管理员");
|
}
|
|
JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
|
//JwtSecurityToken jwt = tokenHandler.ReadJwtToken(Token);
|
|
// 验证 JWT 签名并检查有效期
|
TokenValidationParameters validationParameters = new TokenValidationParameters
|
{
|
ValidateIssuerSigningKey = true,
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey)),
|
ValidateIssuer = false,
|
ValidateAudience = false,
|
ClockSkew = TimeSpan.Zero // 禁用时间偏移量
|
};
|
try
|
{
|
ClaimsPrincipal claimsPrincipal = tokenHandler.ValidateToken(Token, validationParameters, out SecurityToken validatedToken);
|
|
securityToken = validatedToken;
|
return true;
|
}
|
catch (SecurityTokenException)
|
{
|
securityToken=null;
|
return false;
|
}
|
}
|
|
|
/// <summary>
|
/// 校验jwt信息是否有效,判决单端登录验证
|
/// </summary>
|
/// <returns></returns>
|
public bool CheckToken()
|
{
|
|
var Jwtinfo= _UserService.GetJwtInfo();
|
|
var Key= getTokenKey(Jwtinfo);
|
|
string T = _RedisCase.Get<string>(Key);
|
if (T == null)
|
return true;
|
if (T != Jwtinfo.JID.ToString())
|
return false;
|
return true;
|
|
|
}
|
/// <summary>
|
/// 获取TokenKey
|
/// </summary>
|
/// <param name="jwt"></param>
|
/// <returns></returns>
|
private string getTokenKey(JwtInfo jwt)
|
{
|
string TokenKey = TokenKeys + jwt.EID?.ToString() + jwt.UID?.ToString() + jwt.TEID?.ToString();
|
return TokenKey;
|
}
|
|
|
|
|
}
|
|
}
|