Spring Security 6 is a powerful framework for securing applications built with the Spring Framework. When paired with Spring Boot and JSON Web Tokens (JWT), it becomes a robust tool for protecting your web applications. In this tutorial, we'll guide you through implementing Spring Security 6 with Spring Boot and JWT for a comprehensive, secure, and user-friendly authentication system.

Before we dive in, make sure you have Java Development Kit (JDK) 17 or later, Apache Maven 3.6 or later, and your favorite IDE set up. We'll be using Spring Boot 2.5.2 and Spring Security 6.0. In this tutorial, we'll create a simple REST API with endpoints for user registration, login, and secure access to resources.

Setting Up the Project and Dependencies
We'll start by creating a new Spring Boot project using start.spring.io or your favorite IDE, with 'Spring Web' and 'Spring Security' as dependencies.

Next, add the JWT dependencies to your pom.xml file. If you're using Spring Boot 2.5.x, you'll need to add the following dependency for JWT decoding:
```xml
And for JWT signing:

```xml
Configuring Security in Spring Boot
In your application, create a new class SecurityConfig that extends WebSecurityConfigurerAdapter. This class will hold the configuration for Spring Security in your application.
In this class, configure the HTTP security rules, create a user details service, and implement JWT authentication and authorization. The user details service will be responsible for loading user-specific data from your database (in this case, we'll use an in-memory storage for simplicity).

Implementing JWT Authentication
In your controller, create endpoints for user registration and login. During user registration, securely hash and salt the password using BCrypt. Upon successful login, generate a JWT token and send it back to the user. This token will be used for subsequent requests to authenticate the user.
Here's an example of generating a JWT token:

```java String token = Jwts.builder() .setSubject(user.getUsername()) .setIssuedAt(new Date()) .setExpiration(dateAfter(15, TimeUnit.MINUTES)) .signWith(Keys.hmacShaKeyFor(SECRET_KEY.getBytes())) .compact(); ```
Securing API Endpoints
Now that we have JWT authentication set up, let's secure our API endpoints. Spring Security 6 allows us to use the @PreAuthorize annotation to secure controller methods.









Using JWT, we can verify the authenticity of the token sent with each request. Add the @PreAuthorize("hasRole('USER')") annotation to secured endpoints to ensure only authenticated users can access them.
Handling Refresh Tokens
To keep the user session active, we can implement refresh tokens. Refresh tokens have a longer lifespan than access tokens and are used to obtain new access tokens when the current one expires.
When refreshing the token, verify the refresh token, check its validity, and create a new access token and refresh token pair. Ensure refresh tokens are rotated to prevent token leakage.
Testing the Authentication System
Finally, test your endpoints using tools like Postman or curl. Register a new user, login, and use the JWT token to access secure endpoints. Test refreshing the token and ensure it works as expected.
Always remember to test edge cases and ensure your implementation is secure. JWT tokens can be vulnerable to attacks like token theft and replay attacks, so make sure to follow best practices when implementing authentication and authorization mechanisms.
That's it! You've successfully implemented Spring Security 6 with Spring Boot and JWT for user authentication and authorization. This tutorial provides a strong foundation for building secure REST APIs with Spring Boot. Happy coding, and stay secure!