r/javahelp • • Jul 22 '19

Which way of authentication is better and safer for Angular 8 with Spring Boot backend?

So, I searched the web and I found two popular ways to do it.

The first one is in the official Spring documentation: https://spring.io/guides/tutorials/spring-security-and-angular-js/

Second one is an Auth0 blog post: https://auth0.com/blog/implementing-jwt-authentication-on-spring-boot/

Both are outdated a little. The first maybe safer, because it is official, but seems a little complicated. The second one is using JWT, which is so popular.

What do you think, which way would you do and why?

17 Upvotes

12 comments sorted by

2

u/nutrecht Lead Software Engineer / EU / 20+ YXP Jul 22 '19

You should not use JWT unless you specifically need it. If it's a single monolithic back-end there's no use at all for JWTs. You're better off just using token authentication.

Also don't make the mistake of using OAuth2 for just user logins; that's not what it's for and it's also overkill.

1

u/markColibri Jul 23 '19

Thanks for the answer. So, the official documentation's first two section is pretty enough for a simple authentication? I also think that is OAuth2 is overkill for me.

2

u/nutrecht Lead Software Engineer / EU / 20+ YXP Jul 23 '19 edited Jul 23 '19

Yeah, I'd personally go for the first. It's not that hard to implement. I actually did an implementation example here. Planned to write a blog post on that but never finished it :)

Basically it's just basic auth but instead of sending the user credentials with every request you do it once, and then use the supplied token on further requests. It's pretty much the exact same flow if you were to use JWT, but you without JWT downsides. And if you somehow still want to to go for JWT you can; it's just slightly more complicated.

So basically you have:

  • Basic authentication: your client sends user/password with every request. You generally don't want this since you need to keep the password in memory

  • Token/session auth: your client sends user/password once, and then gets a token (random string) it keeps in memory for further requests.

  • JWT: An extension of the above; but instead of a token you store the user's claims on the client, in a signed JSON structure that the server can validate. Main benefit is in architectures with many small services

  • OAuth: used when your client wants to access resources from your user that are stored somewhere else. For example when your application wants access to the Facebook feed of your user. Oauth is mainly meant for scenario's like this and complete overkill for just standard client-server auth.

Important: no matter what you should really aim to only work via HTTPS is you're doing authentication. So this means getting a free Let's Encrypt HTTPS cert for your web app ;)

2

u/chkas Jul 22 '19

don't make the mistake of using OAuth2 for just user logins; that's not what it's for and it's also overkill.

Of course it is made for this and it is currently the most reasonable way to do user authentication without having to implement a user administration and without requiring the user to disclose passwords.

1

u/nutrecht Lead Software Engineer / EU / 20+ YXP Jul 23 '19

Of course it is made for this

Oauth is made for when there's 4 parties involved; an authorization server, a resource server, a resource owner and a client. When there's just a single server - client Oauth2 is simply overkill. You can use it; but there's not much of a point in building it in when a simple token auth would work just as well.

1

u/ndm250 Jul 22 '19

JWT is token authentication? Not sure what you're recommending

1

u/nutrecht Lead Software Engineer / EU / 20+ YXP Jul 23 '19

JWT is token authentication?

A cow is an animal but not all animals are cows. You can just use a session store on the back-end and use a long random token as a key. Basically how sessions work anyway. Easier to implement and doesn't have the downsides of JWTs.

1

u/BilBal82 Jul 23 '19

Can you expand a bit on the downside of JWT in this case? Wouldnt session auth have the same downsides this way?

1

u/nutrecht Lead Software Engineer / EU / 20+ YXP Jul 23 '19

The main downsides are having to have a secret shared between services and that JWT tokens can't be withdrawn. To solve the latter issue they typically get made to be short-lived (minutes) which requires the client to implement a refresh flow, making it more complicated. So unless you need the benefits of JWT's there's not much of a point dealing with the drawbacks.

Also there have been a few libraries that had shitty implementations of the signing that let clients override the algorithm. This is something you have to make really sure doesn't happen in your implementation. Most libraries/frameworks now prevent this though.

Unfortunately most tutorials don't explain this stuff, leading to people thinking JWT's are always the best to use.

5

u/aram535 Jul 22 '19

We use OAUTH2 with client secret to talk from webapp to middleware.

2

u/ndm250 Jul 22 '19

I've done JWT with a simple web filter. If you need complicated permissions, spring security may work well. Hard to recommend something without knowing your requirements.

1

u/BryanBugfrog Jul 23 '19

Not sure if this post will help, but it's about integrating Spring and a platform called FusionAuth. https://fusionauth.io/blog/2018/10/24/easy-integration-of-fusionauth-and-spring It might give you some additional info to work with. There's also a Angular 7 example here: https://github.com/FusionAuth/fusionauth-angular-example that might be useful.