Database and Web Fundamentals· 30 min

JSON, Sessions, and Authentication

Compare cookies, sessions, and tokens while understanding JSON serialization and authentication boundaries.

Java interview lesson
Question 15 · Not reviewed

Web fundamentals · identity

Can you explain this clearly in an interview?

Use the strong answer as your reference, explain it aloud in your own words, then choose how soon you want to review it again.

Interview prompt

JSON, Sessions, and Authentication

Explain it aloud in your own words

Strong answer

Authentication proves who a caller is; authorization decides what that caller may do. Cookie sessions keep server-side state referenced by a cookie, while bearer tokens carry claims that a service validates. Both require expiry, revocation strategy, secure transport, and careful storage.

Key points

  • Never treat a decoded JWT as trusted until its signature and claims are verified.
  • Passwords must be salted and hashed with a password-specific algorithm.
  • Cookies need Secure, HttpOnly, and appropriate SameSite settings.
  • A stateless token does not remove the need to manage logout and compromise.

Interviewer looks for

Explain the complete request flow and where identity is verified, rather than only naming JWT or sessions.

Java example

1Authorization: Bearer eyJ...
2
3// The server verifies signature, issuer, audience, expiry, and required scopes.
4// It then authorizes the requested resource.

Topic interview questions

Revise this topic like an interviewer

These questions are tied to this lesson. Read each question aloud, then compare your explanation with the strong answer.

5 questions
Q1Basic

What is the difference between authentication and authorization?

Strong answer

Authentication verifies identity, while authorization decides whether that identity may perform an action on a resource. A valid login does not automatically grant access to every operation.

Q2Intermediate

What must a server validate in a JWT?

Strong answer

It should validate the signature and expected algorithm, issuer, audience, expiry, not-before time where used, and required scopes or roles. Decoding the payload alone provides no trust.

Q3Intermediate

How does a cookie-backed session work?

Strong answer

The browser stores a session identifier in a cookie and sends it on later requests; server-side state maps that identifier to the authenticated session. The cookie should use Secure, HttpOnly, and appropriate SameSite settings.

Q4Intermediate

How should passwords be stored?

Strong answer

Store a salted, adaptive password hash using a password-specific algorithm such as Argon2id, bcrypt, or scrypt. Never store plaintext passwords or use a fast general-purpose hash such as SHA-256 alone.

Q5Intermediate

What is the difference between CSRF and CORS?

Strong answer

CSRF tricks a browser into sending an authenticated request to a trusted site, while CORS controls which origins browser JavaScript may read responses from. They solve different problems and need separate policies.

How well do you know this?

Your review choice is saved locally and synced to your learner profile when signed in.