I have a web application that should allow users to use different web clients (browser, their own mobile application, etc.) for registration. After logging in, they can access limited content or their own content (for example, the records they create, etc.).
What I have done so far: I created the jax-rs rests web service (I place my application on a glass shawl) which provides the following methods:
- register - POST user his desired username / password / email / etc; if the username / email address is unique, an entry for that user is created in the database (I use Hibernate to save)
- login - POST username and password. If they are ok, a UUID is created and returned to the user (this will be used as a token for future requests). I have a table called logedusers, with user id, token, validSince as columns.
That's where it gets confusing for me.
Let's say that I have another getUserEntries method that should return all entries made by the user. To make this clearer, there will be an Entry table with the following fields: entryId, userId, text.
What is the best approach here?
What I am doing now, I make a request for receipt and pass the token as follows:
localhost:8080/myApp/getUserEntries?token=erf34c34
Subsequently, if the token is valid, I get the user ID from the logedusers table and based on this userId, I get all the entries and return them as json.
Something like that:
@GET @Path("getUserEntries") @Produces(MediaType.APPLICATION_JSON) public Response getUserEntries(@QueryParam("token") String token) { String userId=getUserIdFromToken(token); if (userId == null){ return Response.status(Response.Status.UNAUTHORIZED).build(); } else {
However, what happens if I have more methods that provide data if called by a valid user?
I need to do this check at the beginning of each method.
I want to make this authorization process transparent
So, two main questions:
- Is this design good? All authentication with a user / password, the server creates and saves and sends the token to the user, the user sends the token in future requests.
- What should I do if I have many endpoints that should determine the identity of the caller? Can I mark them with some annotations, use some kind of security provider / authenticator (where I can add my own logic for verification - for example, verify that there is no token token no older than 5 days, etc.).
thanks
Timo89
source share