-
Notifications
You must be signed in to change notification settings - Fork 4k
Using JWT authentication without OAuth setup #368
Comments
+1 |
3 similar comments
+1 |
+1 |
+1 |
Can someone show me why it's so difficult to set up an OAuth2 provider with JWT tokens? If you want JWT tokens all the code is already here. Why is it so hard to just use it? |
Maybe it's not difficult but 1) it feels unnatural to do so and 2) it can be easier. Instead of using @EnableResourceServer and other setup I would like something much more easier like:
What you typically want set to for JWT is the login url (can be defaulted to |
note that @jhipster in latest 2.1.0 release also delivered their custom token authentication yesterday: https://jhipster.github.io/security.html (alongside OAuth2 support btw)
Another reason to look into this to provide something out of the box? |
Some one has tried to implement the JWT authentication with Spring Security. https://github.com/lordofthejars/jwt-springsecurity One Off the track Question ? This would make our app truly stateless. As we are getting authentication as well as authorization data from same token. |
@javajack yes the granted authorities should be part of JWT token indeed. |
@dsyer is there a simple JWT sample (based on spring security oauth) with just having 1 server/backend which serves both the authentication endpoint, resources and html pages? Basically what I need is simple JWT token with username, authorities and a 1d expiry. |
@marceloverdijk this sample is pretty minimal: JWT Sample. Given that you only need one client (which could be autoconfigured in Spring Boot) for your use case, it would only be a few lines of code. |
@javajack as Marcel pointed out, a JWT can contain whatever you need it to. I noticed that the JHipster custom tokens require a |
Thanks @dsyer I will have a look at the sample this evening (after work). |
@dsyer I created this repo (https://github.com/marceloverdijk/spring-security-jwt-sample) with a spring-security-jwt-sample based on the JWT Sample you mentioned. (PS it's not related to the other repo we discussed on gitter https://github.com/marceloverdijk/jwt-security). Adding the
|
/oauth/token using the password grant probably, if you have a trusted client. You can change the path by setting a prefix in the
That's what the /token endpoint does.
It's an OAuth2 bearer token (so |
Isn't the use case described in this issue conceptually different from the OAuth2 case? Here we have a password as an input and JWT token as an output, and JWT token is then used for accessing the resources. The JWT profile for OAuth 2 spec specifies a different case, where a JWT token is an input to the token service and the access token is an output, and access token is then used for accessing the resources. |
I have the same requirement with @marceloverdijk . Just wanna login to get a JWT and every request add JWT in the header to authenticate. I think this is nothing related whit oauth . Using @marceloverdijk I enter the url |
Honestly, I never get the oauth way to work... I ended up writing a custom Spring Security filter. |
@marceloverdijk Thank you for the feedback. I readed |
I don't really think Marcel tried very hard to use the existing support in Sprig OAuth. It might not be perfect but I would start here and make improvements rather than re-invent it all (my opinion only, please feel free to differ). |
I vote to have a simple JWT from spring security |
for me jwt and spring ouath2 together is bit confusing. Should be simple as Marcel pointed.... |
+1 |
+1 |
+1 |
Has anybody able to implement a clean solution of JWT Token with refresh Token? I have taken inspiration from this: http://blog.jdriven.com/2014/10/stateless-spring-security-part-2-stateless-authentication/ |
+1 |
+1 |
2 similar comments
+1 |
+1 |
I'm new to this JWT / OAuth topic, so I'm not an expert here. I've played around a bit with these technologies/frameworks for the last days and I think, that this token-refresh-thingy is a real though one. Here are my thoughts - maybe they'll help further discussing this topic. @alchemistgo87 I think there's no clean refresh token implementation available, because there's no proper way to implement them in web applications (like AngularJS). First of all, having a separate refresh token (in addition to a regular authentication token) makes no sense in web applications (like AngularJS) IMHO - the refresh token can be steal as easy/hard as the authentication token (using XSS). So I'd prefer to use only one token - with a reasonable expiration timestamp defined (either explicitly via the exp claim or implicitly via the iat claim). A very simple implementation to refresh tokens would be to pass a (still valid) token to the server and get back a new one. Problem here is, that if someone gets your token, he can also refresh it endlessly. One solution to prevent this, that is often discussed, is a token blacklist where all tokens (their jti claim - an UUID for example) are blacklisted, so that they aren't valid anymore. These are typically tokens where the user has logged out or refreshed his token and thus got a new one. Tokens on this blacklist can be cleaned up as soon as their expiration timestamp is in the past. But this "solution" would imply that the token thief could refresh the stolen token, gets a new one and the original owner's token isn't valid any more (and the owner will be logged out). Now, the thief has his own "token thread" that can be refreshed endlessly - nobody would notice. To prevent this, you could save the ID (jti) of the most recent token in the database (user table) and overwrite this value on login, but this would cause that a user cannot be logged in from different clients (desktop, tablet, phone, ...) at the same time. After thinking about all these issues, I think the best solution for a web application would be to combine the token authentication stuff with the concept of remember-me authentication. On logon, the user would get two tokens - an authentication token and a remember-me token. The authentication token has an expiration timestamp set to 1 day, the remember-me token to 14 days for example. There is no way to refresh these tokens (at least not the authentication token) and nothing special happens on logout (no blacklist). The authentication token could be stored in browsers sessionStorage, the remember-me token in browsers localStorage. So after 1 day (expiration time for the authentication token) or after browser restart (sessionStorage), the authentication token isn't valid anymore and the remember-me token has to be used. The user will be granted to access with limited permissions only (isAuthenticated() but not isAuthenticatedFully()). If he wants to gain access to more restricted areas of the application (isAuthenticatedFully()) he has to provide his credentials again and thus will get a new authentication token as well as a new remember-me token. A token thief still could steal an authentication token and/or the remember-me token. But the authentication token can only be used for 1 day (in this example) and the remember-me token only grants for limited access. Problems solved!? Am I missing something here? Does OAuth already provide a ready-to-use solution for this and I haven't found it so far? :-) |
+1 |
1 similar comment
+1 |
+1 and will help integration with other SSO technologies like CAS (supported by spring security). CAS inherits scaling issue from session oriented technologies. Providing oauth-less JWT mechanism can really help to turn CAS to stateless auth. |
+1 |
7 similar comments
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
Locked because there are too many +1s (which add nothing to the debate). If you have a more constructive comment please use a back channel (e.g. email) and we can unlock if necessary. |
After discussing with @rwinch and @dsyer on various channel I created this issue to discuss further.
A blog post from Robbert van Waveren on stateless authentication (http://blog.jdriven.com/2014/10/stateless-spring-security-part-2-stateless-authentication/) triggered me to look into JWT authentication again. The described implementation provides basically two filters: 1) a login filter (acting as endpoint) and 2) a token authentication filter.
I personally hesitate to bring in OAuth when I only need JWT authentication. It feels confusing and honestly I do not want the additional complexity to use @EnableResourceServer etc. Maybe it's just a couple of lines of configuration but if feels like overkill.
A little bit of topic, but a good start :-)
When using JSON to communicate between client and server also typical
application/x-www-form-urlencoded
form authentication is awkward. Besides using Basic HTTP authentication a JSON based username/password authentication would be nice to have out-of-the-spring-security-box. It would not be difficult to implement aJsonUsernamePasswordAuthenticationFilter
. It would be similar like the already existingUsernamePasswordAuthenticationFilter
. Just like the latter the username / password fields should be configurable. This might already help some people.Extending on this JsonUsernamePasswordAuthenticationFilter we could create a
JwtUsernamePasswordAuthenticationFilter
serving as endpoint for the authentication. It would just the basis fromJsonUsernamePasswordAuthenticationFilter
to do the actual authentication, but it should set the JWT token in the response header.Next we would need a
JwtTokenAuthenticationFilter
which verifies the received token and sets the authentication. Both filters could be based on the implementation worked out in the mentioned blog post.From @dsyer last blog post 'The internet, and people’s Spring backend projects, are littered with custom token-based authentication solutions.'. I think this is true. Just like the provided implementation in the blog post it's not that difficult to implement it but everybody does it on it's own way.
It's a pity that compared to what Spring Security alrady offers in terms of form authentication, HTTP Basic authentication, CAS etc. out of the box, there is not much for JSON based authentication or JWT authentication.
The text was updated successfully, but these errors were encountered: