Build Reactive APIs with Spring WebFlux

Build Reactive APIs with Spring WebFlux

You’ve heard that reactive programming can help you scale? But how do you implement it? Using Spring WebFlux, of course! This article shows you how.

Rajesh Samson

It’s really a very good article and clean explanation of each and every aspect. Thanks @Matt Raible for your efforts and time, it really helped me a lot understand the various concepts.

kojot

Great post, really. Thanks, @Matt Raible. I found up that there is a bug if we change the version of Spring Boot to 2.1.7. After WebSocket connection, the profile message arrives as expected, but if we reconnect (WebSocket), posting a new profile will not push the new message. Took me an hour to figure it up what’s wrong with my code, hope it’s going to help somebody.

Matt Raible

That’s good to know. Thanks for putting in the time to figure it out. I’d suggest you enter an issue in Spring Boot on GitHub. They might not know this issue exists.

Matt Raible

kojot: I tested and confirmed that it doesn’t reconnect after you upgrade to Spring Boot 2.1.7 or 2.1.8. I’ve sent an email to the Spring team with steps to reproduce. Thanks again!

Rüdiger Schulz

Awesome post! Everything else I found on plain WebSockets with Spring only scratched the surface, but this got quite deep into the topic.

Especially the part with the ProfileCreatedEventPublisher was very helpful for getting my first reactive backend to run.

Thank you (and now I’ll check out what okta even is :wink:

Rüdiger Schulz

Now one question came up. In the section about integration with Okta, i says

If you use OAuth 2.0 without OIDC, you will only know that a person is allowed to access your API. You won’t know who they are.

How do you access user information in the WebSocketHandler?

Matt Raible

Hello,

It’s not currently possible to secure WebSockets when using Spring WebFlux and Spring Security. That’s why we implemented the final solution (only returning an ID) the way we did. If you want to track support for WebFlux + WebSocket support in Spring Security, see issue 5188.

Rüdiger Schulz

That’s quite a setback!

Is there no workaround for that? Intercepting the initial websocket request, or headers sent with requests?

Full spring-security feature package wouldn’t be required for now, but somehow getting the User-ID would really be helpful.

Matt Raible

There is no workaround that I know of. You might try contacting the Spring Security team to see if/when they plan to add WebSocket support for WebFlux.

Bijan

Hi Matt,
Would it be possible to use:
HandshakeInfo info = session.getHandshakeInfo();
Mono<principal> principal = info.getPrincipal();

in order to access user information?

https://github.com/oktadeve…

https://docs.spring.io/spri…

Matt Raible

You’re welcome to try it. When I asked the Spring Security team, they said reactive WebSocket support hasn’t been implemented yet. I believe them.

Nergal

On thing to note here about Flux#share(). The documentation says the following

When all subscribers have cancelled it will cancel the source Flux

which means when all connection leaves then no new message will be published. Instead I would recommend Flux#publish().autoConnect().

Matt Raible

Thanks for the tip, Nergal!

Tes Van Der Vlist

Thanks for the great blogs! Only I came across the same problem with the newer spring boot versions. Is there a solution/workaround yet? To be clear it is the ‘Status and headers are already sent’ IllegalStateException.
I haven’t got the possibility of downgrading because of my choice for R2DBC.

Matt Raible

What version of Spring Boot are you seeing this with? I tried this tutorial in December 2019 with the latest Spring Boot release and didn’t have any issues.

Tes Van Der Vlist

Thanks for the quick resonse. Both 2.2.3 and 2.2.4 give the same result for me.github-repo-of-my-project is my project, but I can’t see anything wrong with it that can lead to this exception. Inserting and refreshing succeeds, but when I open localhost:3000 in the browser the exception occurs.

Matt Raible

Can you please provide the steps to reproduce this issue? If they’re the same steps as this tutorial, can you please provide details on when it happens? I’m not sure I’ll be able to look at it this week, but I should be able to next week. If it’s urgent, you might try posting your issue to Stack Overflow since it’s likely a related to Spring WebFlux, and not the Okta integration.

Tes Van Der Vlist

I have asked and described my question/problem further on Stack Overflow: stack-overflow-with-my-issue

Matt Raible

I redid this tutorial today with the latest version of Spring Boot (2.2.5) and Create React App and did not experience any issues.