DEV Community

Pranav Ghorpade
Pranav Ghorpade

Posted on

I Built a Secure REST API — Then I Tried to Break It

A practical journey through API security, authorization, JWT, rate limiting, and common vulnerabilities.

Why this is a strong Medium story

Instead of writing a boring article titled "What is API Security?", make it a story:

I thought my REST API was secure because it had JWT authentication. Then I started testing what would happen if a user changed an ID in the URL.

That immediately creates curiosity.

OWASP's API Security Top 10 specifically highlights risks such as Broken Object Level Authorization, Broken Authentication, Broken Function Level Authorization, unrestricted resource consumption, SSRF, security misconfiguration, and improper inventory management.

Story structure

  1. Authentication isn't authorization

Imagine:

GET /api/incidents/1001
Authorization: Bearer

The token is valid.

But what if user A can request:

GET /api/incidents/1002

and incident 1002 belongs to user B?

The API authenticated the user.

But it didn't authorize access to the object.

That's the difference between:

Authentication
"Who are you?"

Authorization
"What are you allowed to access?"

  1. JWT doesn't automatically make an API secure

A typical flow:

Login

Username + Password

Spring Security

JWT generated

Client stores token

API request

JWT validation

Authorization

But JWT validation alone doesn't solve:

privilege escalation
broken access control
insecure endpoints
excessive data exposure
rate abuse

  1. Secure the endpoint

For example:

@PreAuthorize("hasRole('ADMIN')")
@GetMapping("/admin/incidents")
public List getAllIncidents() {
return incidentService.findAll();
}

But method-level security is only one layer.

Your service should also verify ownership where appropriate.

  1. Never trust IDs from the client

This is dangerous:

incidentRepository.findById(id);

without checking whether the current user has permission to access it.

A better conceptual approach:

Request

Authenticate

Extract user

Load resource

Check ownership / permission

Return resource

  1. Rate limiting

An endpoint such as:

POST /api/login

can become a target for brute-force attempts.

A production API should consider:

Request

Rate Limiter

Authentication

Authorization

Business Logic

  1. Security checklist

Before calling an API production-ready, check:

Authentication
Authorization
Input validation
Rate limiting
Secure headers
Error handling
Logging
Secret management
Dependency scanning
HTTPS
API documentation
Access control

OWASP maintains the API Security project specifically to help developers and security teams identify and mitigate these API-specific risks.

Ending

The biggest lesson wasn't how to implement JWT.

It was understanding that security isn't a feature you add to an API. Security is a property of the entire API design.

Tags:

Cybersecurity #SpringBoot #APISecurity #Java #Backend #OWASP

Top comments (0)