微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring webflux oauth2 资源服务器自动配置身份验证转换器

如何解决Spring webflux oauth2 资源服务器自动配置身份验证转换器

有人知道为什么我需要下面的代码行才能让它工作吗?

我认为它应该通过 Configuration Bean 自动应用?

.jwtAuthenticationConverter(reactiveJwtAuthenticationConverter()) //

@Configuration
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
@Import(LabcubeOAuth2SecurityConfiguration.class)
public class RestServiceOAuthConfig {
    @Bean
    SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        http.authorizeExchange()
                .pathMatchers("/api/v1/*/"+ ResourceType.SHARED.toString()).permitAll()
                .pathMatchers("/api/v1/**").hasAnyAuthority(LabcubeOAuth2SecurityConfiguration.ALL_ACCESS_ScopE,LabcubeOAuth2SecurityConfiguration.ADMIN_ROLE,LabcubeOAuth2SecurityConfiguration.SUPPORT_ROLE)
                .pathMatchers("/api/**").authenticated()
                .and().oauth2ResourceServer().jwt()
                .jwtAuthenticationConverter(reactiveJwtAuthenticationConverter()) //<-- WHY DO I NEED THIS LINE???
        ;
        return http.build();
    }

    @Bean
    public ReactiveJwtAuthenticationConverter reactiveJwtAuthenticationConverter(){
        JwtGrantedAuthoritiesConverter grantedAuthoritiesConverterRoles = new JwtGrantedAuthoritiesConverter();
        grantedAuthoritiesConverterRoles.setAuthoritiesClaimName("role");
        grantedAuthoritiesConverterRoles.setAuthorityPrefix("");
        JwtGrantedAuthoritiesConverter grantedAuthoritiesConverterScopes = new JwtGrantedAuthoritiesConverter();
        grantedAuthoritiesConverterScopes.setAuthoritiesClaimName("scope");
        grantedAuthoritiesConverterScopes.setAuthorityPrefix("");

        ReactiveJwtAuthenticationConverter jwtAuthenticationConverter = new ReactiveJwtAuthenticationConverter();
        jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(jwt -> Flux.fromStream(Stream.concat(grantedAuthoritiesConverterRoles.convert(jwt).stream(),grantedAuthoritiesConverterScopes.convert(jwt).stream())));
        return jwtAuthenticationConverter;
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。