如何在SpringBoot中创建具有自定义授权类型的webclient

mpbci0fu  于 2021-07-16  发布在  Java
关注(0)|答案(0)|浏览(237)

我们有一个用于委托目的的自定义授权类型(代表用户从微服务调用服务)。我有以下配置

@Bean("delegationRegistration")
    ReactiveClientRegistrationRepository getDelegationRegistration(OAuth2ClientAuthenticationSettings settings, AuthorizationGrantType delegationGrantType) {
        ClientRegistration registration = ClientRegistration
                .withRegistrationId("delegation")
                .tokenUri(settings.getAuthorisationUrl() + settings.getAccessTokenPath())
                .clientId(settings.getClientId())
                .clientSecret(settings.getClientSecret())
                .authorizationGrantType(delegationGrantType)
                .build();
        return new InMemoryReactiveClientRegistrationRepository(registration);
    }

授权授予类型如下:

@Bean(name = "delegationGrantType")
    @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS) 
    public AuthorizationGrantType delegationGrantType() {
        String username = authenticationFacade.getPrincipal().getUsername();
        return new AuthorizationGrantType("custom:authorityOfId:" + username + ";authorityOf:Admin");
    }

但我想是因为范围太大了 request 它尝试使用代理和 AuthorizationGrantType 是最后一节课,所以失败了

Caused by: java.lang.IllegalArgumentException: Cannot subclass final class org.springframework.security.oauth2.core.AuthorizationGrantType
    at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:657) ~[spring-core-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) ~[spring-core-5.2.4.RELEASE.jar:5.2.4.RELEASE]

这里的范围是 request 是因为我们代表发起请求的用户调用其他微服务,所以我们需要设置用户名。有没有什么方法可以做或任何其他方式来设置它,以便它自动请求具有自定义授予类型的访问令牌?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题