关于客户端模式

客户端模式(Client Credentials Grant)指客户端以自己的名义,而不是以用户的名义,向”服务提供商”进行认证。严格地说,客户端模式并不属于 OAuth 框架所要解决的问题。在这种模式中,用户直接向客户端注册,客户端以自己的名义要求”服务提供商”提供服务,其实不存在授权问题。

  • A)客户端向认证服务器进行身份认证,并要求一个访问令牌。
  • B)认证服务器确认无误后,向客户端提供访问令牌。

客户端模式测试使用

给目标客户端分配客户端模式

INSERT INTO `pig`.`sys_oauth_client_details`(`id`,`client_id`, `resource_ids`, `client_secret`, `scope`, `authorized_grant_types`, `web_server_redirect_uri`, `authorities`, `access_token_validity`, `refresh_token_validity`, `additional_information`, `autoapprove`, `create_time`, `update_time`, `create_by`, `update_by`) VALUES (10001,'client', NULL, 'client', 'server', 'password,app,refresh_token,authorization_code,client_credentials', 'https://pig4cloud.com', NULL, NULL, NULL, NULL, 'false', NULL, NULL, 'admin', 'admin');

v3.8+ 版本需要设置客户端跳过验证码

参考 apifox 调用服务接口 导入 apifox 脚本直接调试即可

返回报文

{
  "access_token": "1af5faf2-bb8c-441d-952b-82df970ae474",
  "token_type": "bearer",
  "expires_in": 43199,
  "scope": "server"
}

特别注意

相较于密码模式,返回的数据不包含刷新令牌(令牌容易丢失,避免安全问题)。 不存在用户概念,无法通过 SecurityUtils.getUser 获取当前用户 也无法通过 @PreAuthorize 权限拦截

@GetMapping("/client/info")
public R getClientInfo(){
    // SecurityUtils.getUser() == null
    String requestClientId = SecurityUtils.getAuthentication().getName();
    return R.ok();
}

♥️ 获取支持

遇到问题?

如果您在使用过程中遇到任何问题、有功能建议或需求,请点击此卡片前往 Gitee 仓库提交 Issue。