千万级秒杀系统-8.1用户微服务登录判断及信息获取
gitee仓库地址
请求头加入token进行后台登录验证
先修改之前定义的http通用请求方法
vue-cookies除了在mian.js中定义还需要再这里定义,原因是(不是前端能搞出来就不错了!!有知道的请在评论区解答一下谢谢)
这两行在ts文件中必须要加 否则就会编译错误,查询不出来(困扰了我两三个小时的问题!!!)
import VueCookies from 'vue-cookies'
const $cookies: any = VueCookies
设置请求头的两种方式(个人觉得第二种比较好,先判断再加入请求头)
验证是都带入成功
聚合服务验证
引入nuget
<PackageReference Include="Volo.Abp.Http.Client.IdentityModel" Version="4.4.3" />
SeckillAggregateModule加入身份验证
//添加身份认证
context.Services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:44384";//授权接口地址
options.ApiName = "SeckillAggregate";//授权项目名称
options.RequireHttpsMetadata = false;//不开https
});
app.UseAuthentication();//身份认证(必须再UseAuthorization之上)
找个方法加入Authorize特性使其可以验证身份
/// <summary>
/// 获取用户信息
/// </summary>
/// <returns></returns>
[HttpPost("GetUserInfo")]
[Authorize]
public async Task<ActionResult> GetUserInfo()
{
var user = HttpContext.User;
return new JsonResult("验证成功");
}
页面调用
Post("/Users/GetUserInfo", null).then((res: any) => {
console.log(res);
});
提示了401说明没有权限(验证身份成功)
给用户设置权限
进入用户微服务swagger(需要用户添加SeckillAggregate访问权限)
{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"name": "SeckillAggregate",
"displayName": "SeckillAggregate",
"description": "string",
"claims": [
"string"
]
}
{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"name": "SeckillAggregate",
"displayName": "SeckillAggregate"
}
页面重新登录(使其重新获取token)
访问成功
通过模型绑定获取用户信息
/*----------------------------------------------------------------
* 创建者:WangBenChi
* 创建时间:2023/4/21 23:14:42
*----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WBC.Commons
{
public class SysUser
{
/// <summary>
/// 用户id
/// </summary>
public Guid UserId { get; set; }
/// <summary>
/// 用户昵称
/// </summary>
public string UserName { get; set; }
}
}
/*----------------------------------------------------------------
* 创建者:WangBenChi
* 创建时间:2023/4/21 23:15:30
*----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace WBC.Commons
{
/// <summary>
/// 用户模型绑定
/// </summary>
public class SysUserModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
if (bindingContext.ModelType == typeof(SysUser))
{
SysUser sysUser = new SysUser();
HttpContext httpContext = bindingContext.HttpContext;
ClaimsPrincipal claimsPrincipal = httpContext.User;
//取用户信息
IEnumerable<Claim> claims = claimsPrincipal.Claims;
//判断claims为空
if (claims == null || claims.Count() == 0)
{
throw new ExceptionCommon("未登录");
}
//获取用户信息
foreach (var item in claims)
{
if (item.Type.Equals("sub"))
{
sysUser.UserId = Guid.Parse(item.Value);
}
if (item.Type.Equals("amr"))
{
sysUser.UserName = item.Value;
}
}
//返回结果
bindingContext.Result = ModelBindingResult.Success(sysUser);
}
return Task.CompletedTask;
}
}
}
/*----------------------------------------------------------------
* 创建者:WangBenChi
* 创建时间:2023/4/21 23:22:37
*----------------------------------------------------------------*/
using System;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
namespace WBC.Commons
{
/// <summary>
/// 自定义模型绑定提供
/// </summary>
public class SysUserModelBinderProvider : IModelBinderProvider
{
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (context.Metadata.ModelType == typeof(SysUser))
{
return new BinderTypeModelBinder(typeof(SysUserModelBinder));
}
return null;
}
}
}
聚合服务添加模型绑定
//用户模型绑定
options.ModelBinderProviders.Insert(0, new SysUserModelBinderProvider());
请求页面验证
// <summary>
/// 获取用户信息
/// </summary>
/// <returns></returns>
[HttpPost("GetUserInfo")]
[Authorize]
public async Task<SysUser> GetUserInfo(SysUser user)
{
return user;
}