VOOZH about

URL: https://thenewstack.io/secure-the-web-with-an-api-driven-backend-for-frontend/

⇱ Secure the Web with an API-Driven Backend for Frontend - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2022-06-28 07:00:55
Secure the Web with an API-Driven Backend for Frontend
contributed,sponsor-curity,sponsored,sponsored-post-contributed,
Frontend Development / Security

Secure the Web with an API-Driven Backend for Frontend

Jun 28th, 2022 7:00am by Gary Archer
👁 Featued image for: Secure the Web with an API-Driven Backend for Frontend
Feature image via Pixabay.
Curity sponsored this post.
Gary Archer
Gary is a product marketing engineer at Curity. For 20 years, he has worked as a lead developer and solutions architect.

Building and securing browser-based apps has always been a challenge. Development is much easier these days, with far better technical choices than we had only a decade ago. There are also fewer incompatibilities between browser vendors and some great JavaScript and CSS frameworks that allow us to build modern frontend web experiences.

However, browser security remains a challenging area of web development. Threats such as cross-site scripting (XSS) are a major concern, and a secure development life cycle must be followed to prevent them. Robust and extensible user authentication also must be implemented, including the use of security libraries for your technology stack. All of this effort can be difficult for companies that just want to focus on their business functionality.

Key Requirements

Below are some commonly desired behaviors that architects and engineers at software companies aim for when building web apps:

  • The web app must be considered secure and pass any security reviews.
  • There must be a highly interactive and fast experience for end users.
  • Web developers must be freed up to focus on the user interface.
  • Code in web apps and the APIs they call must be reliable and easy to extend.
  • The web app must perform well for all users globally.

👁 Image

To deliver web apps that meet these requirements, companies often choose to build single-page applications (SPA). However, security for SPAs is a complex topic. Here, I will show how an API-driven approach can provide additional options for meeting all the above goals.

Curity is a leading IAM and API security technology provider that enables user authentication and authorization for digital services. The Curity Identity Server is highly scalable, handles the complexities of the leading identity standards, making them easier to use, customize and deploy.
Learn More
The latest from Curity

Security Best Current Practice

Today, web security is often implemented via OAuth 2.0 and OpenID Connect, which help to externalize the difficult security work to an identity and access management (IAM) system. This enables the user to authenticate in multiple ways, after which tokens are returned to the web app. During data access, the browser calls APIs, which validate JWT access tokens and then authorize access to resources using scopes and claims.

The current best practice in OAuth 2.0 for browser-based apps is to avoid returning tokens directly in the browser due to the extra attack vectors this presents. Instead, an extra application cookie layer should be developed, and a Backend for Frontend (BFF) is recommended. The browser then sends the latest and strongest browser-based cookies in data access requests, with properties such as these:

  • HTTP-only
  • Encrypted
  • SameSite=strict
  • Secure
  • Domain=www.product.com
  • Path=/

Even if your web apps do not yet use OAuth, it’s a best practice to only use secure cookies in the browser. This post is not specific to OAuth technologies. Instead, it focuses on managing certain web technical concerns to achieve the best overall results.

👁 Image

Website Backend for Frontend

The traditional way to manage cookie responsibilities is by implementing a website. If a company uses Java for their backend APIs, they might also choose Java for their website with a setup similar to this:

👁 Image

The web backend then issues cookies but also introduces some wider problems that work against the key requirements mentioned earlier:

  • Developers cannot develop only in React since they need to run a Java backend to retrieve static content. They may also have to write backend code to route requests to APIs.
  • Using website stacks can result in usability problems such as dropped cookies during navigation or abrupt redirects when a session expires. Getting the frontend and backend technologies to interact reliably in areas like these can be a struggle.
  • The React app cannot be deployed without a Java runtime, which may limit deployment options to containers or virtual machines.

Website stacks can provide a quick way for developers to get started and implement working logins. However, some of the deeper requirements in areas such as development productivity may not become apparent until much later.

API-Driven Backend for Frontend

An alternative deployment is displayed below, where the web and API concerns are more cleanly separated, and Java is used only for API responsibilities. For secure cookies to work correctly for the frontend, the BFF must run in the same parent domain as the web origin:

👁 Image

In this setup, the BFF is a utility API that runs on the same site but has a different origin to the frontend. The BFF assists the frontend during authentication and issues cookies once it completes. This setup works in all modern browsers, since they all support cross-origin request sharing (CORS):

Cookies are then issued only for the API subdomain with properties such as these:

  • HTTP-only
  • Encrypted
  • SameSite=strict
  • Secure
  • Domain=api.product.com
  • Path=/

Cookies are not used in requests for static web content, which does not need securing and is not secured in websites either. With the API-driven design, though, anything that you consider confidential, including assets such as PDF files containing private data, must be served by APIs and not the web host.

Web Deployment

Once this setup is in place, web developers can focus solely on the UI, and a component such as the webpack dev server can be used to serve static content during development, with the BFF running remotely. This avoids the need to run backend components on the local computer.

In production, you can deploy the unsecured web resources anywhere, such as to a cloud provider’s content delivery network (CDN). This enables you to push static content to many global hosting servers at low cost.

Web Performance

For static unsecured web pages, server-side rendering (SSR) can be a valuable technique to achieve fast initial page loads and to ensure good search engine optimization (SEO) results. This involves bundling HTML and data together when the app is built and deployed. The user then downloads static HTML documents from the server, which are cached in the browser.

A different design is usually required for secured views within a single-page application. This is because users typically need to see personalized data, and the browser page must then be updated dynamically. Fast Ajax calls from the browser to APIs are usually the best way to manage this. Web resources for the secured SPA can then be deployed to a CDN. In some setups this can considerably improve performance for remote users by ensuring that web downloads in all locations have a similar latency.

Token Handler Pattern

At Curity, we have developed an API-driven Backend for Frontend that can be used by SPAs secured by OAuth 2.0 and OpenID Connect. The solution consists firstly of a utility API called an OAuth Agent, which helps initiate the authentication flow.

The agent then issues separate cookies containing the different types of OAuth tokens, and small opaque tokens are used that fit comfortably inside cookie size limits of browsers and HTTP servers. Use of cookie storage also keeps the BFF stateless and easy to manage.

👁 Image

During API requests, an OAuth Proxy gateway plugin is used to translate from cookies to JWT access tokens in a high-performance manner. JWTs are then forwarded to APIs, where they are used for authorization:

👁 Image

The OAuth Agent and OAuth Proxy only need to be deployed, not developed. They deal with the tricky security plumbing, including OpenID Connect, cookie issuing and CORS. All of this simplifies the code in your web apps and APIs.

Scaling the Architecture

Cookies add deployment complexity, though, especially when you have multiple web apps. An API-driven Backend for Frontend forces you to think through your current and future deployment early on to identify URLs, domains and subdomains.

For any web architecture where cookies secure the app, you need distinct routes into APIs for each web app. This ensures that if a user is logged into multiple apps, cookies for web app 1 can never be sent by web app 2. This is best managed at the gateway level, and the following illustration shows one way to achieve a valid routing:

👁 Image

When considering your web architecture and an API-driven Backend for Frontend, start by designing your deployed domains to see if this type of solution will work for you. Include development domains in this design process to enable an excellent setup for your web developers.

Conclusion

It is challenging to develop for the browser and meet your essential requirements. As part of your web security design, follow the current best practice of keeping tokens out of the browser and using only the latest and most secure HTTP-only cookies. Implement a Backend for Frontend for each web app to issue secure cookies and aim to separate web and API concerns. Also, follow a secure development life cycle and OWASP guidance for mitigating the Top 10 web security risks.

The following Curity resources provide further information on securing web apps for the best overall architecture, while keeping the security plumbing out of application code. There is also an end-to-end code example that you can run against any standards-based IAM system:

Curity is a leading IAM and API security technology provider that enables user authentication and authorization for digital services. The Curity Identity Server is highly scalable, handles the complexities of the leading identity standards, making them easier to use, customize and deploy.
Learn More
The latest from Curity
TRENDING STORIES
Gary Archer is a product marketing engineer at Curity. For 20 years, he has worked as a lead developer and solutions architect.
Read more from Gary Archer
Curity sponsored this post.
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: Pragma.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.