Skip to content

建议OAuth登录Portal时,支持自动创建用户 #5362

@yuanhaoliang

Description

@yuanhaoliang

你的特性请求和某个问题有关吗?请描述

由于使用OIDC登录时,同时校验userinfo中需要有sub并且和id_token相同,所以apollo portal只能以openid作为username,但旧的username都是姓名拼音,从账号密码登录改成OIDC登录时没办法对应得上旧账号。

好不容易改成了OAuth2.0登录了,但又不支持创建新账户。此时Poral的用户管理也无法创建新用户的,代码里限定了只有用账号密码登录时,才给手动创建新用户。

清晰简洁地描述一下你希望的解决方案

OidcAuthenticationSuccessEventListener里增加OAuthUser的登录,实现自动创建账号。

  @Override
  public void onApplicationEvent(AuthenticationSuccessEvent event) {
    Object principal = event.getAuthentication().getPrincipal();
    if (principal instanceof OidcUser) {
      this.oidcUserLogin((OidcUser) principal);
      return;
    }
    if (principal instanceof Jwt) {
      this.jwtLogin((Jwt) principal);
      return;
    }

    // 增加对OAuthUser的识别
    if(principal instanceof OAuth2User) {
      this.oauth2UserLogin((OAuth2User) principal);
      return;
    }

    log.warn("principal is neither oidcUser nor jwt, principal=[{}]", principal);
  }

  // 增加oauth2UserLogin方法。
  private void oauth2UserLogin(OAuth2User oauth2User) {
    log.warn("oauth2UserLogin, oauth2User=[{}]", oauth2User);
    UserInfo newUserInfo = new UserInfo();
    newUserInfo.setUserId(oauth2User.getName());
    newUserInfo.setName(oauth2User.getAttribute(StandardClaimNames.PREFERRED_USERNAME));
    newUserInfo.setEmail(oauth2User.getAttribute(StandardClaimNames.EMAIL);

    if (this.contains(oauth2User.getName())) {
      this.oidcLocalUserService.updateUserInfo(newUserInfo);
      return;
    }
    this.oidcLocalUserService.createLocalUser(newUserInfo);
  }

清晰简洁地描述一下这个特性的备选方案
用户管理→创建用户时的判断,支持OAuthUser登录时手动创建账户。

其它背景

在这里添加和这个特性请求有关的背景说明、截图

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions