【问题标题】:Getting User Roles based on Client Id根据客户端 ID 获取用户角色
【发布时间】:2019-06-16 15:10:00
【问题描述】:

我为我的 oauth2 身份验证服务器注册了多个客户端。我想根据clientId获取用户权限。假设 USER-1 拥有 CLIENT-1 的 ADMIN 权限,而 CLIENT-2 的 USER-1 拥有 USER 权限。

我试过这个issue。但我总是收到一个空请求。

final HttpServletRequest request = ((ServletRequestAttributes)
RequestContextHolder.getRequestAttributes()).getRequest();

我还添加了一个 WebListner,但没有成功。

@Configuration
@WebListener
public class MyRequestContextListener extends RequestContextListener {

}
@Service
public class DomainUserDetailsService implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private AuthorityRepository authorityRepository;

    @Override
    @Transactional
    public UserDetails loadUserByUsername(final String email) throws UsernameNotFoundException {
        User user = userRepository.findUserByUserName(email);
        if (user == null) {
            new UsernameNotFoundException("Username not found");
        }

        String clientId = "?"; // How to get clientId here?
        List<String> roles = authorityRepository.getUserAuthorities(email, clientId);

        return new DomainUser(email, user.getCredential(), user.getId(), fillUserAuthorities(roles));
    }

    public Collection<SimpleGrantedAuthority> fillUserAuthorities(Collection<String> roles) {
        Collection<SimpleGrantedAuthority> authorties = new ArrayList<SimpleGrantedAuthority>();
        for (String role : roles) {
            authorties.add(new SimpleGrantedAuthority(role.toUpperCase()));
        }
        return authorties;
    }
}

如果我走错了方向,任何建议都是可以接受的。

【问题讨论】:

  • 您在哪里有可用的 clientId?
  • 您好,先生,我正在使用 spring-security-oauth2。我想使用授予类型“授权码”来获取 access_token。因此,首先用户需要将他的身份提供给 AuthorizationServer。在这个过程中,我想访问clientId。
  • 您可以使用“;”等分隔符将 clientId 与电子邮件 ID 一起附加在调用身份验证()时。因此,在 loadByUserName 中,您可以将字符串拆分为 ;并获取电子邮件以及 clientId。
  • 用户名将由最终用户输入。为什么最终用户应该知道什么是 clientId?

标签: java spring-security-oauth2


【解决方案1】:

目前,我正在使用自定义 JWT 令牌增强器来实现要求。但我不确定这是否是正确的做法。我不知道为什么我认为这是错误的方式。但是您可以使用以下解决方案来实现这一点。

public class CustomTokenEnhancer implements TokenEnhancer {

    @Autowired
    private AuthorityRepository authRepository;

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        Map<String, Object> additionalInfo = new HashMap<>();
        DomainUser authPrincipal = (DomainUser) authentication.getPrincipal();
        List<String> clientBasedRoles = authRepository.getUserAuthorities(authPrincipal.getId(),
                authentication.getOAuth2Request().getClientId());
        additionalInfo.put("authorities", clientBasedRoles);
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
        return accessToken;
    }
}

然后,在您的 AuthorizationServerConfigurerAdapter 中。

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Bean
    public TokenEnhancer tokenEnhancer() {
        return new CustomTokenEnhancer();
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
        tokenEnhancerChain.setTokenEnhancers(Arrays.asList(tokenEnhancer(), accessTokenConverter()));
        endpoints.authenticationManager(this.authenticationManager).accessTokenConverter(accessTokenConverter())
                .tokenEnhancer(tokenEnhancerChain);
    }
}

【讨论】:

    猜你喜欢
    • 2018-08-20
    • 2020-09-19
    • 2014-10-22
    • 2020-08-25
    • 2018-11-09
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多