【问题标题】:How to configure RSocket security in a Spring Boot application with Spring Security如何使用 Spring Security 在 Spring Boot 应用程序中配置 RSocket 安全性
【发布时间】:2019-09-27 08:26:29
【问题描述】:

对于微服务到微服务的通信,RSocket 似乎是 HTTP/S 的一个不错的替代方案。幸运的是,Spring Boot 已经有一个平滑的集成,可以简化它的配置。

但是,我在 RSocket 和 Spring (Boot, Security) 文档中都缺少有关 RSocket 安全性的所有信息。

我的问题是:

1) 我们如何配置 RSocket 以使用 TLS(在 Spring Boot 应用程序的上下文中)?

2) Spring Security 是否为 RSocket 安全性添加了任何附加功能?我想到的事情,假设我们想将 JWT 令牌从一个应用程序传播到另一个应用程序,它如何通过 RSocket 传递和验证?

【问题讨论】:

    标签: spring-boot spring-security rsocket


    【解决方案1】:

    我最近写了一篇post,介绍了如何将 Spring Security Basic Authentication 与 RSocket 一起使用。对于您的第一个问题,您可以在连接到RSocketServer 时使用TcpClientTransport.create(TcpClient.create().port(7000).secure())

    RSocketRequester.builder()
                    .dataMimeType(MimeTypeUtils.APPLICATION_JSON)
                    .rsocketStrategies(rSocketStrategies)
                    .rsocketFactory(clientRSocketFactory -> {
                        clientRSocketFactory.frameDecoder(PayloadDecoder.ZERO_COPY);
                    })
                    .setupMetadata(credentials, UsernamePasswordMetadata.BASIC_AUTHENTICATION_MIME_TYPE)
                    .connect(TcpClientTransport.create(TcpClient.create().port(7000).secure()))
                    .block();
    

    对于第二个问题,当访问 RSocket 消息端点时,您可以使用

            BearerTokenMetadata credentials = new BearerTokenMetadata("jwt-token");
            return rSocketRequester
                    .route("taxis")
                    .metadata(credentials, BearerTokenMetadata.BEARER_AUTHENTICATION_MIME_TYPE)
                    .data(new TaxisRequest(type, from, to))
                    .retrieveMono(TaxisResponse.class);
    

    在为PayloadSocketAcceptorInterceptor 设置RSocketServer 期间,您可以使用jwt,如下所示。

        @Bean
        public PayloadSocketAcceptorInterceptor rsocketInterceptor(RSocketSecurity rsocket) {
            rsocket.authorizePayload(authorize -> {
                authorize
                        // must have ROLE_SETUP to make connection
                        .setup().hasRole("SETUP")
                        // must have ROLE_ADMIN for routes starting with "taxis."
                        .route("taxis*").hasRole("ADMIN")
                        // any other request must be authenticated for
                        .anyRequest().authenticated();
                })
                .jwt(Customizer.withDefaults());
    
                return rsocket.build();
            }
    

    【讨论】:

    • 感谢您的详细回答和精彩的帖子!
    • 如何在 Spring Boot rsocket 中设置服务器端 ssl 配置?
    猜你喜欢
    • 2019-01-02
    • 2016-08-26
    • 1970-01-01
    • 2014-07-16
    • 2020-10-05
    相关资源
    最近更新 更多