2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								[[rsocket]]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								= RSocket Security
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Spring Security's RSocket support relies on a `SocketAcceptorInterceptor`.
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The main entry point into security is in `PayloadSocketAcceptorInterceptor`, which adapts the RSocket APIs to allow intercepting a `PayloadExchange` with `PayloadInterceptor` implementations.
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The following example shows a minimal RSocket Security configuration:
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 13:57:52 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-06-21 09:56:05 -03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								* Hello RSocket {gh-samples-url}/reactive/rsocket/hello-security[hellorsocket]
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 13:57:52 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								* https://github.com/rwinch/spring-flights/tree/security[Spring Flights]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								== Minimal RSocket Security Configuration
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You can find a minimal RSocket Security configuration below:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2021-05-28 12:03:16 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								@Configuration
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@EnableRSocketSecurity
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public class HelloRSocketSecurityConfig {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									@Bean
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									public MapReactiveUserDetailsService userDetailsService() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										UserDetails user = User.withDefaultPasswordEncoder()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											.username("user")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											.password("user")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											.roles("USER")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											.build();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return new MapReactiveUserDetailsService(user);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2021-05-28 12:03:16 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Configuration
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@EnableRSocketSecurity
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								open class HelloRSocketSecurityConfig {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @Bean
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    open fun userDetailsService(): MapReactiveUserDetailsService {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        val user = User.withDefaultPasswordEncoder()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .username("user")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .password("user")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .roles("USER")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .build()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return MapReactiveUserDetailsService(user)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-02 22:45:45 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								This configuration enables <<rsocket-authentication-simple,simple authentication>> and sets up <<rsocket-authorization,rsocket-authorization>> to require an authenticated user for any request.
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 13:57:52 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								== Adding SecuritySocketAcceptorInterceptor
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								For Spring Security to work, we need to apply `SecuritySocketAcceptorInterceptor` to the `ServerRSocketFactory`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Doing so connects our `PayloadSocketAcceptorInterceptor` with the RSocket infrastructure.
							 
						 
					
						
							
								
									
										
										
										
											2023-12-18 12:00:17 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Spring Boot registers it automatically in `RSocketSecurityAutoConfiguration` when you include {gh-samples-url}/reactive/rsocket/hello-security/build.gradle[the correct dependencies].
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Or, if you are not using Boot's auto-configuration, you can register it manually in the following way:
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 13:57:52 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:31:35 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2022-03-05 17:09:24 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 13:57:52 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Bean
							 
						 
					
						
							
								
									
										
										
										
											2021-06-18 14:15:45 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								RSocketServerCustomizer springSecurityRSocketSecurity(SecuritySocketAcceptorInterceptor interceptor) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return (server) -> server.interceptors((registry) -> registry.forSocketAcceptor(interceptor));
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 13:57:52 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2022-03-05 17:09:24 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:31:35 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2022-03-05 17:09:24 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Bean
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								fun springSecurityRSocketSecurity(interceptor: SecuritySocketAcceptorInterceptor): RSocketServerCustomizer {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return RSocketServerCustomizer { server ->
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        server.interceptors { registry ->
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            registry.forSocketAcceptor(interceptor)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:31:35 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 13:57:52 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-12-18 12:00:17 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								To customize the interceptor itself, use `RSocketSecurity` to add <<rsocket-authentication,authentication>> and <<rsocket-authorization,authorization>>.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								[[rsocket-authentication]]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								== RSocket Authentication
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								RSocket authentication is performed with `AuthenticationPayloadInterceptor`, which acts as a controller to invoke a `ReactiveAuthenticationManager` instance.
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[[rsocket-authentication-setup-vs-request]]
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								=== Authentication at Setup versus Request Time
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Generally, authentication can occur at setup time or at request time or both.
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Authentication at setup time makes sense in a few scenarios.
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								A common scenarios is when a single user (such as a mobile connection) uses an RSocket connection.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In this case, only a single user uses the connection, so authentication can be done once at connection time.
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								In a scenario where the RSocket connection is shared, it makes sense to send credentials on each request.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								For example, a web application that connects to an RSocket server as a downstream service would make a single connection that all users use.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In this case, if the RSocket server needs to perform authorization based on the web application's users credentials, authentication for each request makes sense.
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								In some scenarios, authentication at both setup and for each request makes sense.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Consider a web application, as described previously.
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								If we need to restrict the connection to the web application itself, we can provide a credential with a `SETUP` authority at connection time.
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Then each user can have different authorities but not the `SETUP` authority.
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								This means that individual users can make requests but not make additional connections.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-02-04 23:17:57 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[[rsocket-authentication-simple]]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								=== Simple Authentication
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Spring Security has support for the https://github.com/rsocket/rsocket/blob/5920ed374d008abb712cb1fd7c9d91778b2f4a68/Extensions/Security/Simple.md[Simple Authentication Metadata Extension].
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-02-04 23:17:57 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[NOTE]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								====
							 
						 
					
						
							
								
									
										
										
										
											2021-06-04 14:12:59 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Basic Authentication evolved into Simple Authentication and is only supported for backward compatibility.
							 
						 
					
						
							
								
									
										
										
										
											2020-02-04 23:17:57 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								See `RSocketSecurity.basicAuthentication(Customizer)` for setting it up.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								====
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The RSocket receiver can decode the credentials by using `AuthenticationPayloadExchangeConverter`, which is automatically setup by using the `simpleAuthentication` portion of the DSL.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The following example shows an explicit configuration:
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Bean
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								PayloadSocketAcceptorInterceptor rsocketInterceptor(RSocketSecurity rsocket) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rsocket
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										.authorizePayload(authorize ->
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											authorize
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													.anyRequest().authenticated()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													.anyExchange().permitAll()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										)
							 
						 
					
						
							
								
									
										
										
										
											2020-02-04 23:17:57 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										.simpleAuthentication(Customizer.withDefaults());
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									return rsocket.build();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Bean
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								open fun rsocketInterceptor(rsocket: RSocketSecurity): PayloadSocketAcceptorInterceptor {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    rsocket
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        .authorizePayload { authorize -> authorize
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                .anyRequest().authenticated()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                .anyExchange().permitAll()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        .simpleAuthentication(withDefaults())
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return rsocket.build()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The RSocket sender can send credentials by using `SimpleAuthenticationEncoder`, which you can add to Spring's `RSocketStrategies`.
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								RSocketStrategies.Builder strategies = ...;
							 
						 
					
						
							
								
									
										
										
										
											2020-02-04 23:17:57 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								strategies.encoder(new SimpleAuthenticationEncoder());
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								var strategies: RSocketStrategies.Builder = ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								strategies.encoder(SimpleAuthenticationEncoder())
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								You can then use it to send a username and password to the receiver in the setup:
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2020-02-04 23:17:57 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								MimeType authenticationMimeType =
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									MimeTypeUtils.parseMimeType(WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION.getString());
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								UsernamePasswordMetadata credentials = new UsernamePasswordMetadata("user", "password");
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Mono<RSocketRequester> requester = RSocketRequester.builder()
							 
						 
					
						
							
								
									
										
										
										
											2020-02-04 23:17:57 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									.setupMetadata(credentials, authenticationMimeType)
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									.rsocketStrategies(strategies.build())
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									.connectTcp(host, port);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								val authenticationMimeType: MimeType =
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    MimeTypeUtils.parseMimeType(WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION.string)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								val credentials = UsernamePasswordMetadata("user", "password")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								val requester: Mono<RSocketRequester> = RSocketRequester.builder()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .setupMetadata(credentials, authenticationMimeType)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .rsocketStrategies(strategies.build())
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .connectTcp(host, port)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								Alternatively or additionally, a username and password can be sent in a request.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Mono<RSocketRequester> requester;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								UsernamePasswordMetadata credentials = new UsernamePasswordMetadata("user", "password");
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public Mono<AirportLocation> findRadar(String code) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return this.requester.flatMap(req ->
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										req.route("find.radar.{code}", code)
							 
						 
					
						
							
								
									
										
										
										
											2020-02-04 23:17:57 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											.metadata(credentials, authenticationMimeType)
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											.retrieveMono(AirportLocation.class)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import org.springframework.messaging.rsocket.retrieveMono
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								var requester: Mono<RSocketRequester>? = null
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								var credentials = UsernamePasswordMetadata("user", "password")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								open fun findRadar(code: String): Mono<AirportLocation> {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return requester!!.flatMap { req ->
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        req.route("find.radar.{code}", code)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .metadata(credentials, authenticationMimeType)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .retrieveMono<AirportLocation>()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								[[rsocket-authentication-jwt]]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								=== JWT
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Spring Security has support for the https://github.com/rsocket/rsocket/blob/5920ed374d008abb712cb1fd7c9d91778b2f4a68/Extensions/Security/Bearer.md[Bearer Token Authentication Metadata Extension].
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The support comes in the form of authenticating a JWT (determining that the JWT is valid) and then using the JWT to make authorization decisions.
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The RSocket receiver can decode the credentials by using `BearerPayloadExchangeConverter`, which is automatically setup by using the `jwt` portion of the DSL.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The following listing shows an example configuration:
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Bean
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								PayloadSocketAcceptorInterceptor rsocketInterceptor(RSocketSecurity rsocket) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rsocket
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										.authorizePayload(authorize ->
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											authorize
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												.anyRequest().authenticated()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												.anyExchange().permitAll()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										.jwt(Customizer.withDefaults());
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return rsocket.build();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Bean
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								fun rsocketInterceptor(rsocket: RSocketSecurity): PayloadSocketAcceptorInterceptor {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    rsocket
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        .authorizePayload { authorize -> authorize
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .anyRequest().authenticated()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .anyExchange().permitAll()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        .jwt(withDefaults())
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return rsocket.build()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								The configuration above relies on the existence of a `ReactiveJwtDecoder` `@Bean` being present.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								An example of creating one from the issuer can be found below:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Bean
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								ReactiveJwtDecoder jwtDecoder() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return ReactiveJwtDecoders
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										.fromIssuerLocation("https://example.com/auth/realms/demo");
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Bean
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								fun jwtDecoder(): ReactiveJwtDecoder {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return ReactiveJwtDecoders
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        .fromIssuerLocation("https://example.com/auth/realms/demo")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The RSocket sender does not need to do anything special to send the token, because the value is a simple `String`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The following example sends the token at setup time:
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2020-02-04 23:17:57 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								MimeType authenticationMimeType =
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									MimeTypeUtils.parseMimeType(WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION.getString());
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								BearerTokenMetadata token = ...;
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								Mono<RSocketRequester> requester = RSocketRequester.builder()
							 
						 
					
						
							
								
									
										
										
										
											2020-02-04 23:17:57 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									.setupMetadata(token, authenticationMimeType)
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									.connectTcp(host, port);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								val authenticationMimeType: MimeType =
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    MimeTypeUtils.parseMimeType(WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION.string)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								val token: BearerTokenMetadata = ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								val requester = RSocketRequester.builder()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .setupMetadata(token, authenticationMimeType)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .connectTcp(host, port)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Alternatively or additionally, you can send the token in a request:
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2020-02-04 23:17:57 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								MimeType authenticationMimeType =
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									MimeTypeUtils.parseMimeType(WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION.getString());
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								Mono<RSocketRequester> requester;
							 
						 
					
						
							
								
									
										
										
										
											2020-02-04 23:17:57 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								BearerTokenMetadata token = ...;
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public Mono<AirportLocation> findRadar(String code) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return this.requester.flatMap(req ->
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										req.route("find.radar.{code}", code)
							 
						 
					
						
							
								
									
										
										
										
											2020-02-04 23:17:57 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									        .metadata(token, authenticationMimeType)
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											.retrieveMono(AirportLocation.class)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								val authenticationMimeType: MimeType =
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    MimeTypeUtils.parseMimeType(WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION.string)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								var requester: Mono<RSocketRequester>? = null
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								val token: BearerTokenMetadata = ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								open fun findRadar(code: String): Mono<AirportLocation> {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return this.requester!!.flatMap { req ->
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        req.route("find.radar.{code}", code)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .metadata(token, authenticationMimeType)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .retrieveMono<AirportLocation>()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								[[rsocket-authorization]]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								== RSocket Authorization
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								RSocket authorization is performed with `AuthorizationPayloadInterceptor`, which acts as a controller to invoke a `ReactiveAuthorizationManager` instance.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You can use the DSL to set up authorization rules based upon the `PayloadExchange`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The following listing shows an example configuration:
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								rsocket
							 
						 
					
						
							
								
									
										
										
										
											2021-06-18 14:39:15 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									.authorizePayload(authz ->
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										authz
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											.setup().hasRole("SETUP") // <1>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											.route("fetch.profile.me").authenticated() // <2>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											.matcher(payloadExchange -> isMatch(payloadExchange)) // <3>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												.hasRole("CUSTOM")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											.route("fetch.profile.{username}") // <4>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												.access((authentication, context) -> checkFriends(authentication, context))
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											.anyRequest().authenticated() // <5>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											.anyExchange().permitAll() // <6>
							 
						 
					
						
							
								
									
										
										
										
											2021-06-18 14:39:15 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									);
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								rsocket
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .authorizePayload { authz ->
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        authz
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .setup().hasRole("SETUP") // <1>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .route("fetch.profile.me").authenticated() // <2>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .matcher { payloadExchange -> isMatch(payloadExchange) } // <3>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .hasRole("CUSTOM")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .route("fetch.profile.{username}") // <4>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .access { authentication, context -> checkFriends(authentication, context) }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .anyRequest().authenticated() // <5>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .anyExchange().permitAll()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } // <6>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								<1> Setting up a connection requires the `ROLE_SETUP` authority.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								<2> If the route is `fetch.profile.me`, authorization only requires the user to be authenticated.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								<3> In this rule, we set up a custom matcher, where authorization requires the user to have the `ROLE_CUSTOM` authority.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								<4> This rule uses custom authorization.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The matcher expresses a variable with a name of `username` that is made available in the `context`.
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								A custom authorization rule is exposed in the `checkFriends` method.
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								<5> This rule ensures that a request that does not already have a rule requires the user to be authenticated.
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								A request is where the metadata is included.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								It would not include additional payloads.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								<6> This rule ensures that any exchange that does not already have a rule is allowed for anyone.
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								In this example, it means that payloads that have no metadata also have no authorization rules.
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 11:01:50 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Note that authorization rules are performed in order.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Only the first authorization rule that matches is invoked.