【问题标题】:Spring oauth2 authorization server add roles user in tokenSpring oauth2授权服务器在令牌中添加角色用户
【发布时间】:2021-11-23 21:41:35
【问题描述】:

我使用 spring-security-oauth2-authorization-server (v. 0.2.0) 来实现我的授权服务器。 我希望用户角色在令牌中,是否可以添加它们?喜欢吗?

谢谢

【问题讨论】:

    标签: spring-security oauth-2.0


    【解决方案1】:

    我通过添加以下bean解决了这个问题

    @Bean
    OAuth2TokenCustomizer<JwtEncodingContext> jwtCustomizer() {
        return context -> {
            if (context.getTokenType() == OAuth2TokenType.ACCESS_TOKEN.getValue()) {
                Authentication principal = context.getPrincipal();
                Set<String> authorities = principal.getAuthorities().stream()
                        .map(GrantedAuthority::getAuthority)
                        .collect(Collectors.toSet());
                context.getClaims().claim("user-authorities", authorities);
            }
        };
    }
    

    【讨论】:

      猜你喜欢
      • 2020-04-08
      • 2015-03-31
      • 2017-12-27
      • 2015-05-22
      • 2020-11-03
      • 2022-11-04
      • 2022-10-23
      • 2022-08-21
      • 1970-01-01
      相关资源
      最近更新 更多