I am trying to understand token-based authentication
these days, which claims to be a stateless authentication
method. And I met the concept of stateless web application
.
Below are some threads I read about:
- Use Spring MVC for Stateless web application development (no response yet)
- Stateless Spring MVC
- How to make a java web application fully stateless
- How do I make my Web Application stateless yet still do something useful?
At first, I was thrilled at this idea. But more and more I think stateless
is a pseudo-proposition
.
For example, suppose we use client-stored token for authentication, how can we make a statistic of online users (suppose there's no log)? Shall we store the token in DB? Doesn't that mean we store state info on server? And even more, is the plain user info such as name, age, etc. in DB also some kind of state info?
I think the real question here is not to make a web app stateless, but to make the web app properly handle the state info such that it won't jeopardize scalability.
That depends on how to interpret the word stateless
:
- Web app doesn't have state.
- Or web app doesn't store state itself.
I prefer 2 because there can always be some inevitable global state
(quoted from @deceze's comment to his answer). And no matter we store state info as HTML 5 web storage, or HTTP header, or hidden form fields, or Cookie, the state still exists. Only that it is stored somewhere other than on the server.
Am I missing something great? Could anybody shed some light on this so I can be relieved from this mental struggle?
ADD 1
Just read about the book RESTful Web Services by Leonard Richardson
. In chapter 4, at end of the section Statelessness
, it classifies the state into Application State
and Resource State
. So the plain user info and data I mentioned before like images, etc. can be classified as Resource State
. And what stateless
refers to is Application State
. So it doesn't break the code of stateless to store resource state
on server.
But the book also mentions the scenario where an application key is used to restrict how many times a user can invoke a web service.
It admits that such info cannot be stored on client side. And having to store it on server side breaks the code of stateless and introduce the issue of session affinity. It claims stateless can avoid session affinity issue but doesn't explain how. I really don't see how stateless can handle this scenario. Anyone could shed some light here?