Social Login & Token Integration with Spring Authorization Server — A chitchat with GPT 4

Syed Hasan
3 min readAug 14, 2023

--

Today, I had a very interesting conversation with GPT 4, regarding some clarification of confusion to make an ideal application design with Spring Authorization Server. Here I am putting it as an article for further reference.

Me:

I am trying to develop an application, where the angular server is running on port 4200. As resource servers, I have article service running on port 8081. Authorization client running on port 8080, and authorization server running on port 9000. In which server shall I put my google and github social login codes. After I receive the access token from google or github, how can I design the applications to work together and use this token for spring webClient calls?

GPT 4:

Based on the architecture you described, let’s break down your requirements:

1. Where to put your Google and GitHub social login codes?

Your social login codes (Google, GitHub) typically belong in the Authorization Server. This is where user authentication is handled, and once authenticated, the server will issue tokens that your other services will trust and use for authorization.

So, you would integrate the Google and GitHub login functionalities into the server running on port 9000 (your Authorization Server). The client application (Angular) would redirect users to this Authorization Server to log in via their Google or GitHub accounts.

2. After receiving the access token from Google or GitHub, how can you design the applications to work together to use this token for Spring WebClient calls?

Here’s a step-by-step approach:

a. Token Issuance:

Once a user logs in via Google or GitHub on the Authorization Server, the server will issue its own JWT (JSON Web Token) or some other token format.

This token represents the user’s session and permissions in your ecosystem.

b. Passing the Token to the Angular Client:

The Authorization Server sends the token to the Angular app, usually as a redirect with the token either in the URL fragment or as a cookie.

c. Using the Token in WebClient Calls:

When the Angular app makes requests to the article service (or any other service), it includes the token in the request header, often as a Bearer token.

Example: Authorization: Bearer YOUR_JWT_TOKEN

d. Validating the Token in Services:

The services (running on ports 8081) need to validate the token. They do this by either:

2.1. Validating the token signature and claims directly (if they have the necessary secret/key).

2.2. Making a call to the Authorization Server to check the token’s validity.

e. Making WebClient Calls:

In your Spring services, whenever you need to make WebClient calls, you can forward the token. Here’s a basic example of how you might use the token with WebClient:

String token = "YOUR_JWT_TOKEN"; // Usually extracted from the incoming request

WebClient webClient = WebClient.builder().baseUrl("http://target-service").build();

String result = webClient.get()
.uri("/target-endpoint")
.header("Authorization", "Bearer " + token)
.retrieve()
.bodyToMono(String.class)
.block();

f. Token Renewal:

Tokens might expire. If so, you need a strategy for renewing them. The simplest is to have the client (Angular app) request a new one from the Authorization Server, but there are other strategies like refresh tokens.

3. Security Considerations:

Always use HTTPS for all servers involved to ensure data in transit (especially tokens) is encrypted.

JWTs, if used, should be signed and possibly encrypted. Be wary of exposing sensitive information in JWT payloads.

Token expiration: Short-lived tokens are more secure but require more frequent renewals.

Handle token compromise scenarios: Have a strategy in place to revoke tokens if necessary.

4. Cross-Origin Resource Sharing (CORS):

Given that your Angular server is on a different port/domain, ensure your backend services handle CORS appropriately, allowing requests from your Angular application.

By following this design, your services will be able to trust and authorize requests based on the tokens they receive, all the while leveraging the benefits of social logins provided by platforms like Google and GitHub.

Reference to this conversation:

  1. https://chat.openai.com/share/146a534c-05cb-48ff-af95-fae1a1b5da76

--

--

Syed Hasan
Syed Hasan

Written by Syed Hasan

Software Engineer | Back-End Developer | Spring Developer | Cloud Enthusiast

No responses yet