简体   繁体   中英

Read env variables in React bundled with Spring Boot

I have a Spring Boot application with a bundled React frontend. It's configured with maven frontend plugin to build the react app and put it in the /static so it is served by Spring Boot. Everything is wrapped in a single.jar and deployed to a k8s cluster where the env variables are being handled separately and read by the Spring Boot.

Now, I have to add an API key as a env variable to be read by the React frontend.

As far as I've figured, I can't use .env files since it's not an independent React deployment and won't be seen by the frontend. The only thing I can think of is to pass it to Spring Boot and then expose it via an endpoint to the frontend.

Am I missing something, is there a way to use .env files in this setup, or some other way that is better for this use case?

This was eventually resolved by setting it as an environment variable for the Spring Boot application which then read that value and exposed it via a dedicated controller on a secured endpoint. The react app would then simply fetch it via a GET request from the controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM