DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Running a Self-Hosted WAF in Production: Practical Lessons From SafeLine
  • Your Application Has an Unindexed Attack Surface. Do You Know What’s in It?
  • The Architect's Guide to Logging
  • 4 Essential Strategies for Enhancing Your Application Security Posture

Trending

  • How to Evaluate Coding Agents With Harbor Framework, Python, Docker
  • Claude Code Superpowers: The 5-Tool Stack That Ends Limits and Context Amnesia
  • Controller and Data Access: Understanding Two-Layer Architecture Through Abstraction and Encapsulation
  • When Production Stops Moving: Running Claude Code Across a Distributed Enterprise Integration Team

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. Token Based Security: Angular Applications, Part 3

Token Based Security: Angular Applications, Part 3

In part three of this series on token based security, we will learn how to authorize HTTP calls to resource API by using an Angular HTTP Interceptor.

By 
Jawad Hasan Shani
DZone Core CORE ·
Oct. 22, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
7.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Introduction

In the previous post of this series, we configured our Angular application as a client of IdnentityServer and completed the login/logout process.

However even though the user was logged in, the REST API calls were still not authorized:

Demo Angular App Products List

In this post, we will authorize HTTP calls by passing the bearer token as an Authorization Header.

Now, if you are new to token-based security or how we set up the Angular application, I will suggest you first go through the previous posts, and then the topics in this post will much easier to follow. Let’s see how to get an Access Token next.

Get Access Token

I’ve added the following method to authService (we created this service in the previous post) to get the access token:

AuthService method to get access token

Next, we will build a service based on HTTP Interceptor, which will call this method to get the access token and then further use this access token to set HTTP Headers for calls to the protected API.

Authorizing REST API Calls

We can add the bearer token to HTTP requests manually or we can use the HTTP Interceptor in the pipeline to do this automatically.

Here is the boilerplate code for such a service:

Authorizing REST API Calls

I’ll not go into the details of HttpInterceptor.  By the way, I used the Angular Snipped extension for VS-Code to quickly create the template code. You can install this extension and it will help you quickly write code for various purposes:

Angular Snippets Version 12

Setting Up HTTP Interceptor

Let’s update the HttpInterceptor to set header with bearer token:

HttpInterceptor to set header with bearer token

The code is very boilerplate. We are calling the getAccessToken() method which we created earlier in AuthService. Then we just set the retrieved bearer token on HTTP Header. To return an observable, we first used to toPromise function and wrap the code in an rxjs from operator.

Now, any HTTP Call from the Angular application will pass through this interception point and the token will be added automatically.

We also need to register this interceptor to the application as shown below:

Register interceptor to the application

Testing the Application

With all of these changes in place, let's run the applications (IdentityServer, ResourceAPI, and Angular client) and visit the Products List page again.  This time, the HTTP Request is authorized, data is retrieved, and shown on UI:

Products List page

Excluding Bearer Token When Needed

There might be situations when for some HTTP calls (maybe those to a different API), you don’t want to set the bearer token automatically or maybe want a different bearer token. One way to do that is to just introduce a simple check as shown below:

Excluding Bearer Token

Now, only the call intended for the specific API will have a bearer token added automatically.

Summary

In this post, we learned how to authorize HTTP calls to resource API by using an Angular HTTP Interceptor. You can download the source code from this git repository. If you have some comments or questions, let me know. Until next time, Happy Coding.

application AngularJS security

Published at DZone with permission of Jawad Hasan Shani. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Running a Self-Hosted WAF in Production: Practical Lessons From SafeLine
  • Your Application Has an Unindexed Attack Surface. Do You Know What’s in It?
  • The Architect's Guide to Logging
  • 4 Essential Strategies for Enhancing Your Application Security Posture

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook