# Build Reactive APIs with Spring WebFlux

**URL:** <https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935>\
**Category:** Developer Blog Comments\
**Created:** [September 24, 2018, 2:29pm UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935 "2018-09-24T14:29:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [September 24, 2018, 2:29pm UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935/1 "2018-09-24T14:29:35Z")

</div>

[Build Reactive APIs with Spring WebFlux](https://developer.okta.com/blog/2018/09/24/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.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [April 3, 2019, 7:32am UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935/2 "2019-04-03T07:32:58Z")

</div>

**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.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [August 9, 2019, 9:54am UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935/3 "2019-08-09T09:54:14Z")

</div>

**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.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [August 14, 2019, 7:32pm UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935/4 "2019-08-14T19:32:41Z")

</div>

**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](https://github.com/spring-projects/spring-boot/issues "https://github.com/spring-projects/spring-boot/issues") on GitHub. They might not know this issue exists.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [September 6, 2019, 4:56pm UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935/5 "2019-09-06T16:56:02Z")

</div>

**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!

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [September 19, 2019, 10:13am UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935/6 "2019-09-19T10:13:58Z")

</div>

**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 😉

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [September 19, 2019, 11:06am UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935/7 "2019-09-19T11:06:42Z")

</div>

**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`?

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [September 19, 2019, 7:18pm UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935/8 "2019-09-19T19:18:34Z")

</div>

**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](https://github.com/spring-projects/spring-security/issues/5188 "https://github.com/spring-projects/spring-security/issues/5188").

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [September 23, 2019, 12:13pm UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935/9 "2019-09-23T12:13:51Z")

</div>

**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.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [September 23, 2019, 2:12pm UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935/10 "2019-09-23T14:12:14Z")

</div>

**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.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [October 1, 2019, 3:07pm UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935/11 "2019-10-01T15:07:33Z")

</div>

**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://github.com/oktadeveloper/okta-spring-webflux-react-example/blob/master/reactive-web/src/main/java/com/example/demo/WebSocketConfiguration.java#L57 "https://github.com/oktadeveloper/okta-spring-webflux-react-example/blob/master/reactive-web/src/main/java/com/example/demo/WebSocketConfiguration.java#L57")

[https://docs.spring.io/spri…](https://docs.spring.io/spring-framework/docs/current/javadoc-api/index.html?org/springframework/web/reactive/socket/WebSocketHandler.html "https://docs.spring.io/spring-framework/docs/current/javadoc-api/index.html?org/springframework/web/reactive/socket/WebSocketHandler.html")

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [October 1, 2019, 4:40pm UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935/12 "2019-10-01T16:40:27Z")

</div>

**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.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [October 26, 2019, 5:20pm UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935/13 "2019-10-26T17:20:20Z")

</div>

**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()`.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [November 2, 2019, 12:37am UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935/14 "2019-11-02T00:37:08Z")

</div>

**Matt Raible**

Thanks for the tip, Nergal!

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [January 20, 2020, 11:13pm UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935/15 "2020-01-20T23:13:59Z")

</div>

**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.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [January 21, 2020, 12:52am UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935/16 "2020-01-21T00:52:56Z")

</div>

**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.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [January 21, 2020, 8:21am UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935/17 "2020-01-21T08:21:31Z")

</div>

**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](https://github.com/Vlisie/reactivetourapp "https://github.com/Vlisie/reactivetourapp") 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.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [January 22, 2020, 1:18am UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935/18 "2020-01-22T01:18:40Z")

</div>

**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.

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [January 22, 2020, 3:06pm UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935/19 "2020-01-22T15:06:53Z")

</div>

**Tes Van Der Vlist**

I have asked and described my question/problem further on Stack Overflow: [stack-overflow-with-my-issue](https://stackoverflow.com/questions/59856292/how-to-fix-status-and-headers-already-sent-when-refreshing-react-app-webflux "https://stackoverflow.com/questions/59856292/how-to-fix-status-and-headers-already-sent-when-refreshing-react-app-webflux")

---

<div class="post-metadata">

**Author:** ![oktadev-blog](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/oktadev-blog/32/3422_2.png) [@oktadev-blog](https://devforum.okta.com/u/oktadev-blog)\
**Post date:** [March 13, 2020, 12:23am UTC](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935/20 "2020-03-13T00:23:32Z")

</div>

**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.

[Next page](https://devforum.okta.com/t/build-reactive-apis-with-spring-webflux/16935.md?page=2)
