feat: 搭建双token
fix: 修复签到记录问题
This commit is contained in:
@@ -9,6 +9,7 @@ using Volo.Abp.Application.Services;
|
|||||||
using Volo.Abp.Users;
|
using Volo.Abp.Users;
|
||||||
using Yi.Framework.Bbs.Application.Contracts.Dtos.Integral;
|
using Yi.Framework.Bbs.Application.Contracts.Dtos.Integral;
|
||||||
using Yi.Framework.Bbs.Domain.Managers;
|
using Yi.Framework.Bbs.Domain.Managers;
|
||||||
|
using Yi.Framework.Rbac.Domain.Authorization;
|
||||||
|
|
||||||
namespace Yi.Framework.Bbs.Application.Services.Integral
|
namespace Yi.Framework.Bbs.Application.Services.Integral
|
||||||
{
|
{
|
||||||
@@ -42,33 +43,34 @@ namespace Yi.Framework.Bbs.Application.Services.Integral
|
|||||||
[HttpGet("integral/sign-in/record")]
|
[HttpGet("integral/sign-in/record")]
|
||||||
public async Task<SignInDto> GetSignInRecordAsync()
|
public async Task<SignInDto> GetSignInRecordAsync()
|
||||||
{
|
{
|
||||||
var output = new SignInDto();
|
var output = new SignInDto();
|
||||||
DateTime lastMonth = DateTime.Now.AddMonths(-1);
|
DateTime lastMonth = DateTime.Now.AddMonths(-1);
|
||||||
DateTime lastDayOfMonth = new DateTime(lastMonth.Year, lastMonth.Month, 1).AddMonths(1).AddDays(-1);
|
DateTime lastDayOfMonth = new DateTime(lastMonth.Year, lastMonth.Month, 1).AddMonths(1).AddDays(-1);
|
||||||
DateTime startOfLastDay = new DateTime(lastDayOfMonth.Year, lastDayOfMonth.Month, lastDayOfMonth.Day, 0, 0, 0);
|
DateTime startOfLastDay = new DateTime(lastDayOfMonth.Year, lastDayOfMonth.Month, lastDayOfMonth.Day, 0, 0, 0);
|
||||||
|
|
||||||
//获取当前用户本月的数据+上个月最后一天的数据
|
//获取当前用户本月的数据+上个月最后一天的数据
|
||||||
var entities = await _integralManager._signInRepository.GetListAsync(x => x.CreatorId == CurrentUser.Id
|
var entities = await _integralManager._signInRepository.GetListAsync(x => x.CreatorId == CurrentUser.Id
|
||||||
&& x.CreationTime >= startOfLastDay);
|
&& x.CreationTime >= startOfLastDay);
|
||||||
|
|
||||||
if (entities is null)
|
if (entities.Count() == 0)
|
||||||
{
|
{
|
||||||
//返回默认值
|
//返回默认值
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
//拿到最末尾的数据
|
||||||
|
var lastEntity = entities.OrderBy(x => x.CreationTime).LastOrDefault();
|
||||||
|
|
||||||
|
//判断当前时间和最后时间是否为连续的
|
||||||
|
if (lastEntity.CreationTime.Day >= DateTime.Now.AddDays(-1).Day)
|
||||||
|
{
|
||||||
|
|
||||||
|
output.CurrentContinuousNumber = lastEntity.ContinuousNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
//去除上个月查询的数据
|
||||||
|
output.SignInItem = entities.Where(x => x.CreationTime.Month == DateTime.Now.Month).Select(x => new SignInItemDto { Id = x.Id, CreationTime = x.CreationTime }).OrderBy(x => x.CreationTime).ToList();
|
||||||
return output;
|
return output;
|
||||||
}
|
|
||||||
//拿到最末尾的数据
|
|
||||||
var lastEntity = entities.OrderBy(x => x.CreationTime).LastOrDefault();
|
|
||||||
|
|
||||||
//判断当前时间和最后时间是否为连续的
|
|
||||||
if (lastEntity.CreationTime.Day >= DateTime.Now.AddDays(-1).Day)
|
|
||||||
{
|
|
||||||
|
|
||||||
output.CurrentContinuousNumber = lastEntity.ContinuousNumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
//去除上个月查询的数据
|
|
||||||
output.SignInItem = entities.Where(x=>x.CreationTime.Month==DateTime.Now.Month) .Select(x => new SignInItemDto { Id = x.Id, CreationTime = x.CreationTime }).OrderBy(x=>x.CreationTime).ToList();
|
|
||||||
return output;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,11 @@ namespace Yi.Framework.Rbac.Domain.Shared.Options
|
|||||||
|
|
||||||
public string Audience { get; set; }
|
public string Audience { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
public string RefreshIssuer { get; set; }
|
||||||
|
|
||||||
|
public string RefreshAudience { get; set; }
|
||||||
|
|
||||||
public string SecurityKey { get; set; }
|
public string SecurityKey { get; set; }
|
||||||
|
|
||||||
public long ExpiresMinuteTime { get; set; }
|
public long ExpiresMinuteTime { get; set; }
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Yi.Framework.Rbac.Domain.Shared.Options
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 刷新token
|
||||||
|
/// </summary>
|
||||||
|
public class RefreshJwtOptions: JwtOptions
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,7 +16,7 @@ namespace Yi.Framework.Rbac.Domain.Shared
|
|||||||
{
|
{
|
||||||
var configuration = context.Services.GetConfiguration();
|
var configuration = context.Services.GetConfiguration();
|
||||||
Configure<JwtOptions>(configuration.GetSection(nameof(JwtOptions)));
|
Configure<JwtOptions>(configuration.GetSection(nameof(JwtOptions)));
|
||||||
|
Configure<RefreshJwtOptions>(configuration.GetSection(nameof(RefreshJwtOptions)));
|
||||||
Configure<RbacOptions>(configuration.GetSection(nameof(RbacOptions)));
|
Configure<RbacOptions>(configuration.GetSection(nameof(RbacOptions)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,14 +3,11 @@ using System.Security.Claims;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using Mapster;
|
using Mapster;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Identity;
|
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Microsoft.IdentityModel.Tokens;
|
using Microsoft.IdentityModel.Tokens;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using TencentCloud.Tdmq.V20200217.Models;
|
|
||||||
using Volo.Abp;
|
using Volo.Abp;
|
||||||
using Volo.Abp.Domain.Entities;
|
using Volo.Abp.Domain.Entities;
|
||||||
using Volo.Abp.Domain.Repositories;
|
|
||||||
using Volo.Abp.Domain.Services;
|
using Volo.Abp.Domain.Services;
|
||||||
using Volo.Abp.EventBus.Local;
|
using Volo.Abp.EventBus.Local;
|
||||||
using Volo.Abp.Security.Claims;
|
using Volo.Abp.Security.Claims;
|
||||||
@@ -38,11 +35,13 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
|||||||
private IHttpContextAccessor _httpContextAccessor;
|
private IHttpContextAccessor _httpContextAccessor;
|
||||||
private UserManager _userManager;
|
private UserManager _userManager;
|
||||||
private ISqlSugarRepository<RoleEntity> _roleRepository;
|
private ISqlSugarRepository<RoleEntity> _roleRepository;
|
||||||
|
private RefreshJwtOptions _refreshJwtOptions;
|
||||||
public AccountManager(IUserRepository repository
|
public AccountManager(IUserRepository repository
|
||||||
, IHttpContextAccessor httpContextAccessor
|
, IHttpContextAccessor httpContextAccessor
|
||||||
, IOptions<JwtOptions> jwtOptions
|
, IOptions<JwtOptions> jwtOptions
|
||||||
, ILocalEventBus localEventBus
|
, ILocalEventBus localEventBus
|
||||||
, UserManager userManager
|
, UserManager userManager
|
||||||
|
,IOptions<RefreshJwtOptions> refreshJwtOptions
|
||||||
, ISqlSugarRepository<RoleEntity> roleRepository)
|
, ISqlSugarRepository<RoleEntity> roleRepository)
|
||||||
{
|
{
|
||||||
_repository = repository;
|
_repository = repository;
|
||||||
@@ -51,6 +50,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
|||||||
_localEventBus = localEventBus;
|
_localEventBus = localEventBus;
|
||||||
_userManager = userManager;
|
_userManager = userManager;
|
||||||
_roleRepository = roleRepository;
|
_roleRepository = roleRepository;
|
||||||
|
_refreshJwtOptions= refreshJwtOptions.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -86,6 +86,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
|||||||
//将用户信息添加到缓存中,需要考虑的是更改了用户、角色、菜单等整个体系都需要将缓存进行刷新,看具体业务进行选择
|
//将用户信息添加到缓存中,需要考虑的是更改了用户、角色、菜单等整个体系都需要将缓存进行刷新,看具体业务进行选择
|
||||||
|
|
||||||
var accessToken = CreateToken(this.UserInfoToClaim(userInfo));
|
var accessToken = CreateToken(this.UserInfoToClaim(userInfo));
|
||||||
|
|
||||||
return accessToken;
|
return accessToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +112,23 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
|||||||
return returnToken;
|
return returnToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string CreateRefreshToken()
|
||||||
|
{
|
||||||
|
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_refreshJwtOptions.SecurityKey));
|
||||||
|
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
||||||
|
var claims =new List<Claim> { new Claim("Refresh", "true") } ;
|
||||||
|
var token = new JwtSecurityToken(
|
||||||
|
issuer: _refreshJwtOptions.Issuer,
|
||||||
|
audience: _refreshJwtOptions.Audience,
|
||||||
|
claims: claims,
|
||||||
|
expires: DateTime.Now.AddMinutes(_refreshJwtOptions.ExpiresMinuteTime),
|
||||||
|
notBefore: DateTime.Now,
|
||||||
|
signingCredentials: creds);
|
||||||
|
string returnToken = new JwtSecurityTokenHandler().WriteToken(token);
|
||||||
|
|
||||||
|
return returnToken;
|
||||||
|
|
||||||
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 登录效验
|
/// 登录效验
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -118,19 +118,17 @@ namespace Yi.Abp.Web
|
|||||||
|
|
||||||
//jwt鉴权
|
//jwt鉴权
|
||||||
var jwtOptions = configuration.GetSection(nameof(JwtOptions)).Get<JwtOptions>();
|
var jwtOptions = configuration.GetSection(nameof(JwtOptions)).Get<JwtOptions>();
|
||||||
|
var refreshJwtOptions = configuration.GetSection(nameof(RefreshJwtOptions)).Get<RefreshJwtOptions>();
|
||||||
|
|
||||||
context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||||
.AddJwtBearer(options =>
|
.AddJwtBearer(options =>
|
||||||
{
|
{
|
||||||
options.TokenValidationParameters = new TokenValidationParameters
|
options.TokenValidationParameters = new TokenValidationParameters
|
||||||
{
|
{
|
||||||
ClockSkew = TimeSpan.Zero,
|
ClockSkew = TimeSpan.Zero,
|
||||||
ValidateIssuer = true,
|
|
||||||
ValidateAudience = true,
|
|
||||||
ValidateLifetime = true,
|
|
||||||
ValidateIssuerSigningKey = true,
|
ValidateIssuerSigningKey = true,
|
||||||
ValidIssuer = jwtOptions.Issuer,
|
ValidIssuer = jwtOptions.Issuer,
|
||||||
ValidAudience = jwtOptions.Audience,
|
ValidAudience = jwtOptions.Audience,
|
||||||
RequireExpirationTime = true,
|
|
||||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOptions.SecurityKey))
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOptions.SecurityKey))
|
||||||
};
|
};
|
||||||
options.Events = new JwtBearerEvents
|
options.Events = new JwtBearerEvents
|
||||||
@@ -146,6 +144,17 @@ namespace Yi.Abp.Web
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
|
.AddJwtBearer("Refresh", options => {
|
||||||
|
options.TokenValidationParameters = new TokenValidationParameters
|
||||||
|
{
|
||||||
|
ClockSkew = TimeSpan.Zero,
|
||||||
|
ValidateIssuerSigningKey = true,
|
||||||
|
ValidIssuer = refreshJwtOptions.Issuer,
|
||||||
|
ValidAudience = refreshJwtOptions.Audience,
|
||||||
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(refreshJwtOptions.SecurityKey))
|
||||||
|
};
|
||||||
|
|
||||||
|
})
|
||||||
.AddQQ(options =>
|
.AddQQ(options =>
|
||||||
{
|
{
|
||||||
configuration.GetSection("OAuth:QQ").Bind(options);
|
configuration.GetSection("OAuth:QQ").Bind(options);
|
||||||
|
|||||||
@@ -37,6 +37,13 @@
|
|||||||
"SecurityKey": "zqxwcevrbtnymu312412ihe9rfwhe78rh23djoi32hrui3ryf9e8wfh34iuj54y0934uti4h97fgw7hf97wyh8yy69520",
|
"SecurityKey": "zqxwcevrbtnymu312412ihe9rfwhe78rh23djoi32hrui3ryf9e8wfh34iuj54y0934uti4h97fgw7hf97wyh8yy69520",
|
||||||
"ExpiresMinuteTime": 86400
|
"ExpiresMinuteTime": 86400
|
||||||
},
|
},
|
||||||
|
//刷新token
|
||||||
|
"RefreshJwtOptions": {
|
||||||
|
"Issuer": "https://yi.ccnetcore.com",
|
||||||
|
"Audience": "https://yi.ccnetcore.com",
|
||||||
|
"SecurityKey": "67ij4o6jo4i5j6io45j6i4j74p5k6i54ojoi5t9g8ergoj34ofgkrtbmreog894jbioemgropihj48rj4io5juopjgior",
|
||||||
|
"ExpiresMinuteTime": 172800
|
||||||
|
},
|
||||||
|
|
||||||
//第三方登录
|
//第三方登录
|
||||||
"OAuth": {
|
"OAuth": {
|
||||||
|
|||||||
Reference in New Issue
Block a user