Urgent.News

What's breaking now, across thousands of outlets.

Tech

Replacing Basic Auth with JWT and OAuth2 in Spring Security

Basic auth was always the placeholder. It worked, but sending credentials on every single request never sat right — no real session, no expiry, no way to log someone out without changing their password. This week I finally replaced it with JWT, and wired up OAuth2 alongside it. Why Move Off Basic Auth The app was already stateless, so on paper basic auth and JWT look similar — no server-side…

Replacing Basic Authentication with JWT and OAuth2 in Spring Security involved significant changes to the authentication system. Basic auth had several issues, such as sending credentials in every request and lack of session management, expiry, and logout options.

Switching to JWT addressed these concerns by exchanging credentials once during login and subsequently using a signed token to carry the user's identity and role claims. After the login, the token is included in the Authorization: Bearer token header of each subsequent request. The server then validates the token's signature and expiry, creating an Authentication object from the claims and setting it into the security context.

This process allowed the existing role-based access control (RBAC) to function without modification.

OAuth2 login was added alongside JWT, providing users an alternative method to authenticate via external providers. The OAuth2 client support in Spring Security simplified the process, but mapping OAuth2-authenticated users onto the existing role system proved challenging. To resolve this, a default role was assigned to first-time OAuth2 logins, making them indistinguishable from normal registered users.

The primary challenge was managing token expiry. While issuing tokens was straightforward, handling expired tokens without causing confusion required careful consideration. The solution involved distinguishing between expired-token and invalid-token responses, allowing the client-side to differentiate between needing to log in again and encountering an error.

With these changes, Basic Authentication is no longer used, and every request now carries a token instead of raw credentials. The token has an expiry, and there are two ways to authenticate: direct login or OAuth2. Both methods lead to the same role-based system underneath. Although this resulted in a more complex system than before, each component addresses a limitation that Basic Authentication couldn't solve. The next step is to implement refresh tokens, as expired tokens currently require users to log in from scratch.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Bookmarks for Your Terminal

If you use Linux and spend a decent amount of time in the terminal, zoxide is one of those little tools that's absolutely worth trying if you haven't already .

  • Zoxide is terminal navigation tool
  • Automatically creates bookmarks based on frequent visits
  • Simplifies directory jumping in Linux

Y Combinator insurance tech alum Angle Health hits $2.7B valuation

Angle Health has grown to 5,000 customers and become profitable, by helping small businesses get "level-funded" health insurance.

  • Angle Health, AI-focused health insurance startup, reaches $2.7B valuation
  • $200M Series C round and $400M tender offer announced
  • Y Combinator alum secures funding from Vitruvian Partners and others

More from Friday 18 September →