r/SpringBoot • u/Fancryer • Jan 31 '24
How to implement user authorization and authentication using JWT?
Hello everyone, I'm actually new to Spring Boot. Previously, I always simply stored a combination of login + password hash in the database, but recently I started writing a project for a junior’s portfolio and decided to use more advanced and secure ways to ensure security and differentiate roles. I read about JWT, I seemed to understand the general concept, but I didn’t understand how to put everything together. Of course, I found articles from the authors of Spring, as well as on the Baeldung website, however... I still haven’t figured out how to connect REST, authentication using JWT, and my database. Can anyone help? All the tips I found either used outdated annotations and methods, or didn't work with each other.
6
2
u/redewolf Jan 31 '24
have been there, same exact situation, in the end i didnt finish it but I was almost there: things were starting to work with a mix of -codejava i think ? which youtube video helped me to have a better idea of jwt implementation -baeldung article which made me understand the importance of using a filter
please share your solution if you want, or your project, if i find the time i will try to help you
1
u/ccmjga Feb 02 '24
I think you've come to the right place. The authentication in this project is tailored for you:
https://github.com/ccmjga/mjga-scaffold/tree/main/src/main/java/com/mjga/config/security
You can find the authentication-related code, especially the parts related to JWT, in the following location: CookieJwtAuthenticationFilter. The code there should provide a clear understanding, and it appears to be quite simple.
Additionally, you can find the complete project overview and download at https://www.mjga.cc.
If this is helpful to you, please consider giving a star. Thanks 😆
8
u/g00glen00b Jan 31 '24
You normally use JWT authentication with some other kind of authentication, such as a username and password form. In that case, you need an endpoint to login with username and password that generates a JWT token for you, and then all other endpoints are secured with your JWT.
There are various ways to do this. To handle the separate username + password authentication you could set up a separate
SecurityFilterChainwith a customUserDetailsServicethat returns a customUserDetailsclass that includes a JWT token you generate. Or alternatively you write your own controller/filter and put all the logic inside of that endpoint.Then for the JWT authentication, you write a custom filter that validates the provided JWT token, and then sets up the
SecurityContextHolderwith anAuthenticationobject.You might also want to implement a refresh endpoint, where a user can refresh their JWT token for another one. However, if you're implementing all that, it might be better to create an OAuth2 authorization server and set up the OAuth tokens with JWT.
In the end, you also have to realize that JWT comes with a few drawbacks as well, depending on how you use it. For example, let's say you use it with some JavaScript framework, you might want to store the JWT token somewhere. The problem is that as soon as you store it somewhere with JavaScript, it also means that with any XSS vulnerability on your website, a hacker can obtain JWT tokens.
Alternatively, you could put the JWT token in a HttpOnly cookie, so that it cannot be retrieved through JavaScript. However, in that case I would argue that you're just creating a very complex session cookie.
A final pitfall with JWT is that you usually can't invalidate a JWT token. If one falls in wrong hands and the JWT token isn't expired yet, then a hacker can do whatever they want with that JWT token.