Abpvnext 通过统一接口访问授权服务 /connect/token 获取token

本文介绍了一个代码实现,旨在通过公共网关隐藏鉴权服务地址,统一前端访问端口,同时利用Swagger文档方便前端导入接口,减少TS代码编写。作者分享了Login接口的实现,并欢迎其他开发者提出更好的建议。

纯属是想统一前端访问端口,不暴露鉴权服务地址,
当然,也可以通过公共网关访问是完全可以实现这个效果的
另一个方面,后端swagger文档引入该接口
方便前端使用nswag工具导入swagger,少写很多前端ts代码
这只是一种特别的处理

个人代码实现,各位大佬有更好的想法,欢迎留言告知,虚心求教

public class AccountAppService : BaseAppService, IAccountAppService
    {
        private readonly IdentityUserManager _userManager;
        private readonly IHttpClientFactory _httpClientFactory;

        public AccountAppService(
            IdentityUserManager userManager,
            IHttpClientFactory httpClientFactory)
        {
            _userManager = userManager;
            _httpClientFactory = httpClientFactory;
        }
        [AllowAnonymous]
        public async Task<LoginOutput> LoginAsync(LoginInput input)
        {
            try
            {
                var client = _httpClientFactory.CreateClient(HttpClientNameConsts.AuthServer);
                var dic = new Dictionary<string, object>
                {
                    {"client_id","basic-web"},
                    {"client_secret","1q2w3e*"},
                    {"grant_type","password"},
                    {"username",input.Name},
                    {"password",input.Password},
                };
                var dicStr = dic.Select(m => m.Key + "=" + m.Value).DefaultIfEmpty().Aggregate((m, n) => m + "&" + n);
                HttpContent httpContent = new StringContent(dicStr);
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
                var oauthRep = await client.PostAsync("connect/token", httpContent);
                var oauthStr = await oauthRep.Content.ReadAsStringAsync();
                var oauthResult = default(Id4TokenOutput);
                if (oauthRep.IsSuccessStatusCode)
                {
                    if (!string.IsNullOrEmpty(oauthStr))
                        oauthResult = JsonConvert.DeserializeObject<Id4TokenOutput>(oauthStr);
                }
                else
                {
                    if (string.IsNullOrEmpty(oauthStr))
                        throw new BusinessException(oauthRep.ReasonPhrase);
                }
                var user = await _userManager.FindByNameAsync(input.Name);
                if (!user.IsActive)
                {
                    throw new UserFriendlyException("用户已锁定");
                }
                return await BuildResult(user, oauthResult?.access_token);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        #region 私有方法
        private async Task<LoginOutput> BuildResult(IdentityUser user, string token)
        {
            var roles = await _userManager.GetRolesAsync(user);
            if (roles == null || roles.Count == 0) throw new UserFriendlyException("当前用户未分配角色");
            var loginOutput = ObjectMapper.Map<IdentityUser, LoginOutput>(user);
            loginOutput.Token = token;
            loginOutput.Roles = roles.ToList();
            return loginOutput;
        }
        #endregion
    }
public class LoginInput : IValidatableObject
    {
        /// <summary>
        /// 用户名或者邮箱
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }


        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (Name.IsNullOrWhiteSpace())
            {
                yield return new ValidationResult("Email can not be null", new[] { "Email" });
            }

            if (Password.IsNullOrWhiteSpace())
            {
                yield return new ValidationResult("Password can not be null", new[] { "Password" });
            }
        }
    }
public class Id4TokenOutput
    {
        public string access_token { get; set; }

        public int expires_in { get; set; }

        public string token_type { get; set; }

        public string refresh_token { get; set; }

        public string scope { get; set; }

    }

个人代码实现,各位大佬有更好的想法,欢迎留言告知,虚心求教

在Spring Boot集成OAuth 2.1的场景中,新版本的 /oauth2/token 接口是OAuth 2.1协议下用于获取访问令牌的重要端点。 当客户端向 /oauth2/token 接口发起请求时,会携带特定的参数,如授权类型(grant_type)、客户端凭证(client_id 和 client_secret)等。授权类型决定了获取令牌的方式,常见的有密码模式(password)、授权码模式(authorization_code)等。例如在密码模式下,还需携带用户名(username)和密码(password)。 服务器端接收到请求后,会对请求进行验证。首先验证客户端凭证的合法性,确保客户端是经过注册且被信任的。接着根据授权类型,检查请求中其他参数的完整性和正确性。以密码模式为例,服务器会验证用户名和密码是否匹配。 验证通过后,服务器会调用相应的令牌授予器(Token Granter)来生成访问令牌。就像在某些代码实现中会调用 `OAuth2AccessToken token = this.getTokenGranter().grant(tokenRequest.getGrantType(), tokenRequest);` 这样的方法来完成令牌的授予工作 [^2]。 生成的访问令牌会包含一些必要的信息,如令牌值、过期时间、刷新令牌(如果适用)等。服务器将这些信息以JSON格式返回给客户端,客户端就可以使用这个访问令牌来访问受保护的资源。 在Spring Boot的配置中,需要在 `AuthorizationServerConfig` 和 `SecurityConfig` 等配置类中对 /oauth2/token 接口进行相关配置,以确保接口能正常工作,包括配置客户端详情服务、令牌存储方式等 [^1]。 示例中提到老版本的 /oauth/token 接口在新版本升级为 /oauth2/token ,说明在OAuth协议的发展过程中,接口有了变化和改进,以更好地遵循OAuth 2.1协议的规范 [^1]。 ### 代码示例 以下是一个简单的获取 /oauth2/token 的请求示例(使用Python的`requests`库): ```python import requests url = 'http://localhost:9001/oauth2/token' data = { 'grant_type': 'password', 'username': 'user', 'password': 'pass', 'client_id': 'your_client_id', 'client_secret': 'your_client_secret' } response = requests.post(url, data=data) print(response.json()) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值