SPRING SECURITY / 02

API and features

HTTP contract, examples and rules actually enforced by the server.

Endpoint map

The examples use the deployment at compose.yml: http://localhost:8088/api. If you run Spring with Maven and a database accessible from the host, replace that base URL with http://localhost:7070/api. Send JSON using Content-Type: application/json. Protected routes require Authorization: Bearer TOKEN.

MethodRouteAccessSuccessPurpose
POST/api/auth/registerPublic201Create USER + issue JWT
POST/api/auth/loginPublic200Verify credentials + issue JWT
GET/api/meUSER or ADMIN200JWT subject profile
GET/api/usersADMIN200Complete list ordered by id
POST/api/usersADMIN201Create an account and return its profile
PUT/api/users/{id}ADMIN200Update the complete account
DELETE/api/users/{id}ADMIN204Delete without a response body

Public registration

shell
curl -i http://localhost:8088/api/auth/register   -H 'Content-Type: application/json'   -d '{"name":"Ana Pérez","email":"ana@example.com","password":"Aprender123!"}'

RegisterRequest accepts name (2–80 characters, nonblank), email (valid format, maximum 160) and password (8–72 characters, maximum 72 UTF-8 bytes). The server trims the name, lowercases the email, rejects duplicates, calculates a BCrypt hash and saves an active USER account. Sending role: "ADMIN" grants no permissions: the DTO has no such field and the controller assigns USER.

Login and authentication response

shell
curl -i http://localhost:8088/api/auth/login   -H 'Content-Type: application/json'   -d '{"email":"ana@example.com","password":"Aprender123!"}'
json
{
  "accessToken": "JWT_DEVUELTO_POR_EL_SERVIDOR",
  "tokenType": "Bearer",
  "expiresIn": 900,
  "user": {
    "id": 3,
    "name": "Ana Pérez",
    "email": "ana@example.com",
    "role": "USER",
    "enabled": true
  }
}

Login returns 200; registration returns 201 in the same format. The id and token above are illustrative. Login validates nonblank email and password, up to 160 and 72 characters respectively, and the BCrypt limit of 72 bytes. It does not reapply the eight-character minimum: it verifies the stored credential. A missing email, incorrect password or disabled account produces 401 with a common message.

Query profiles and users

shell
# Copia un token propio de la respuesta de login.
TOKEN='PEGA_AQUI_UN_TOKEN_USER_O_ADMIN'
curl -i http://localhost:8088/api/me -H "Authorization: Bearer $TOKEN"

# Usa un token ADMIN para esta operación.
ADMIN_TOKEN='PEGA_AQUI_UN_TOKEN_ADMIN'
curl -i http://localhost:8088/api/users -H "Authorization: Bearer $ADMIN_TOKEN"

/me returns a UserResponse like the user object above. Its id comes from the validated JWT subject. /users returns an array of these objects, even when empty. There is no GET /api/users/{id} or search endpoint. Password hashes never appear in responses.

Create an account as ADMIN

shell
curl -i http://localhost:8088/api/users   -H "Authorization: Bearer $ADMIN_TOKEN"   -H 'Content-Type: application/json'   -d '{"name":"Luis Gómez","email":"luis@example.com","password":"Aprender123!","role":"USER","enabled":true}'

Creation requires a non-null, nonblank password. ADMIN can create USER or ADMIN accounts and choose enabled. This endpoint returns only UserResponse; it neither signs in as the new account nor returns its token.

Edit and delete

shell
# Sustituye 4 por el id real de otra cuenta de pruebas.
curl -i -X PUT http://localhost:8088/api/users/4   -H "Authorization: Bearer $ADMIN_TOKEN"   -H 'Content-Type: application/json'   -d '{"name":"Luis editado","email":"luis@example.com","password":"","role":"ADMIN","enabled":true}'

# Elimina esa cuenta de pruebas de forma permanente.
curl -i -X DELETE http://localhost:8088/api/users/4   -H "Authorization: Bearer $ADMIN_TOKEN"

PUT requires name, email, role and enabled; it is not a partial PATCH update. An empty, null or omitted password preserves the hash; a new password must satisfy 8–72 characters and at most 72 bytes. Role accepts only USER or ADMIN; enabled must be a non-null boolean.

You cannot remove your own ADMIN role, disable or delete your own account. These operations return 409. You can modify other accounts, including other ADMIN accounts: there is no general last-administrator protection rule. Deletion removes the account row; disabling preserves it and prevents authentication.

Error responses

json
{
  "status": 400,
  "message": "Revisa los campos del formulario.",
  "errors": {"email": "must be a well-formed email address"}
}

Validation messages may vary with the validator's language. The common format is status, message and errors (a field map, usually empty for general errors). The additional BCrypt constraint may appear under the name passwordWithinBcryptLimit.

CodeMeaningExample
400Invalid inputMalformed JSON, unknown role, short or excessive password.
401Invalid identityMissing token, altered signature, expiration, wrong issuer, deleted or disabled user.
403Insufficient permissionUSER attempts to manage users. A rejected CORS origin may also produce 403 with a different body.
404Account not foundADMIN edits or deletes a nonexistent id.
409ConflictDuplicate email or protection of the acting account.

Guided practice

  1. Call /me without a token: 401.
  2. Register USER and use it on /me: 200; on /users: 403.
  3. Modify the JWT signature: 401.
  4. Repeat the registration email: 409.
  5. As ADMIN, create and disable an account. Its token stops granting access on the next request.
  6. Change a role and reuse the original token: the current database role applies.

You can also open docs/peticiones.http in a client supporting .http files. This project has no configured Swagger/OpenAPI.

CV & portfolio · ES / EN
EspañolEnglish