DEV Community

Aashish Panthi
Aashish Panthi

Posted on

Your Login Works, But Is Your API Secure? OAuth2 and JWT in Kotlin

I use AI coding agents almost every day, and I still read every change they make. One thing I’ve noticed, in my own code and in a lot of projects I see online, is that the login part usually works. You sign in, you get a token, the dashboard loads. It looks done. But “who is this user?” and “what is this user allowed to do?” are two different questions, and a lot of apps only really answer the first one.

That’s the difference between authentication and authorization. Authentication checks who you are. Authorization checks what you can do. When an agent writes a frontend that hides the “Delete” button for normal users, it feels secure. It isn’t. Anyone with a valid token can skip your UI and call the API directly.

So when I read this beginner tutorial from JetBrains on securing Kotlin and Spring Boot APIs with OAuth 2.0 and JWT, I was happy to read it.

What the tutorial covers

It starts with the fundamentals: authentication vs authorization, the roles in OAuth2, and how a JWT is structured. If you’ve only ever copied a token from a login response, this part is worth slowing down for. A JWT has a header, a payload with claims about the user, and a signature that proves nobody changed it.

Then it moves to a practical implementation with Spring Security. After that it gets into the parts I think beginners skip:

  • managing scopes and roles
  • extracting custom claims from the token
  • enforcing permissions with @PreAuthorize
  • validating the token’s issuer and audience

That last one surprised me the most. A token can be perfectly valid and still not be meant for your API. Checking who issued it and who it was issued for closes that gap.

Who it’s for

I’d recommend it to beginner backend developers and anyone learning Kotlin, Spring Boot or API security. You don’t need to be a security expert. You do need to be comfortable reading some Kotlin.

What I’m taking from it

The biggest takeaway for my own projects is simple. Permissions get validated on the backend, on every request. The frontend can hide buttons to make the UI cleaner, but it should never be the thing that decides who can do what.

If you’re building with AI agents too, that’s a good thing to check in the code they give you. Ask where the permission check actually happens. If the answer is “in the React component”, you have work to do.

You can read the full tutorial here: https://blog.jetbrains.com/kotlin/2026/07/secure-your-apis-oauth2-and-jwt-for-beginners/

Top comments (0)