Single logo
  • Courses
  • Articles
  • About
Join for freeJoin free
  1. Blog
  2. /
  3. S02L01 - Spring Boot RestApi, JWT demo

Spring Boot RESTFUL API Articles
1 views

S02L01 - Spring Boot RestApi, JWT demo


January 28, 2025

Integrating Swagger and JWT in Spring Boot RESTful APIs: A Comprehensive Guide


Table of Contents

  1. Introduction
  2. Understanding Swagger
  3. Introduction to JWT (JSON Web Tokens)
  4. Setting Up Swagger in Spring Boot
  5. Implementing JWT Authentication
  6. Customizing Swagger UI
  7. Testing APIs with Swagger
  8. Best Practices and Security Considerations
  9. Conclusion

Introduction

In the realm of modern web development, creating secure and well-documented APIs is paramount. This eBook delves into the integration of Swagger and JWT (JSON Web Tokens) within Spring Boot RESTful APIs. By leveraging Swagger for API documentation and JWT for authentication, developers can build robust, secure, and easily maintainable applications. This guide is tailored for beginners and developers with basic knowledge, providing clear, concise instructions and practical examples.


Understanding Swagger

What is Swagger?

Swagger is an open-source framework that assists in designing, building, documenting, and consuming RESTful web services. It provides a user-friendly interface to visualize and interact with the API's resources without any implementation logic.

Key Features of Swagger

  • API Documentation: Automatically generates interactive documentation.
  • API Testing: Facilitates testing endpoints directly from the UI.
  • Schema Definitions: Defines data models and structures for requests and responses.

Importance of Swagger in API Development

Swagger enhances the development workflow by providing clear documentation, which is crucial for collaboration and maintenance. It simplifies the process of understanding API endpoints and their functionalities.


Introduction to JWT (JSON Web Tokens)

What is JWT?

JWT (JSON Web Token) is a compact, URL-safe means of representing claims to be transferred between two parties. It is widely used for authentication and authorization in web applications.

Key Components of JWT

  1. Header: Contains metadata about the token, including the type and signing algorithm.
  2. Payload: Contains the claims or statements about the user or entity.
  3. Signature: Ensures the token's integrity by verifying that it hasn't been altered.

Advantages of Using JWT

  • Stateless Authentication: Eliminates the need for server-side sessions.
  • Scalability: Suitable for distributed systems and microservices.
  • Security: Encodes user information securely with an expiration time.

Setting Up Swagger in Spring Boot

Prerequisites

  • Basic knowledge of Spring Boot.
  • Java Development Kit (JDK) installed.
  • Maven or Gradle build tool.

Step-by-Step Integration

  1. Add Swagger Dependencies
// pom.xml

    
        io.springfox
        springfox-boot-starter
        3.0.0
    

  • Configure Swagger
  • // SwaggerConfig.java
    package com.example.demo.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    
    @Configuration
    public class SwaggerConfig {
        @Bean
        public Docket api() {
            return new Docket(DocumentationType.OAS_30)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.example.demo"))
                    .paths(PathSelectors.any())
                    .build();
        }
    }
    
  • Access Swagger UI
  • Start the Spring Boot application and navigate to http://localhost:8080/swagger-ui/ to view the interactive API documentation.


    Implementing JWT Authentication

    Why Use JWT?

    JWT provides a secure and efficient way to handle authentication and authorization in stateless applications. It ensures that each request is authenticated without maintaining server-side sessions.

    Setting Up JWT in Spring Boot

    1. Add JWT Dependencies
    // pom.xml
    
        
            io.jsonwebtoken
            jjwt
            0.9.1
        
    
    
  • Create JWT Utility Class
  • // JwtUtil.java
    package com.example.demo.util;
    
    import io.jsonwebtoken.Claims;
    import io.jsonwebtoken.Jwts;
    import io.jsonwebtoken.SignatureAlgorithm;
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.function.Function;
    
    @Component
    public class JwtUtil {
        private String SECRET_KEY = "secret";
    
        public String extractUsername(String token) {
            return extractClaim(token, Claims::getSubject);
        }
    
        public Date extractExpiration(String token) {
            return extractClaim(token, Claims::getExpiration);
        }
    
        public  T extractClaim(String token, Function claimsResolver) {
            final Claims claims = extractAllClaims(token);
            return claimsResolver.apply(claims);
        }
    
        private Claims extractAllClaims(String token) {
            return Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token).getBody();
        }
    
        private Boolean isTokenExpired(String token) {
            return extractExpiration(token).before(new Date());
        }
    
        public String generateToken(String username) {
            Map claims = new HashMap<>();
            return createToken(claims, username);
        }
    
        private String createToken(Map claims, String subject) {
            return Jwts.builder().setClaims(claims).setSubject(subject).setIssuedAt(new Date(System.currentTimeMillis()))
                    .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 10))
                    .signWith(SignatureAlgorithm.HS256, SECRET_KEY).compact();
        }
    
        public Boolean validateToken(String token, String username) {
            final String extractedUsername = extractUsername(token);
            return (extractedUsername.equals(username) && !isTokenExpired(token));
        }
    }
    
  • Create Authentication Controller
  • // AuthController.java
    package com.example.demo.controller;
    
    import com.example.demo.util.JwtUtil;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.*;
    
    @RestController
    public class AuthController {
    
        @Autowired
        private JwtUtil jwtUtil;
    
        @PostMapping("/token")
        public ResponseEntity generateToken(@RequestBody AuthRequest authRequest) {
            // Authenticate user
            String token = jwtUtil.generateToken(authRequest.getUsername());
            return ResponseEntity.ok(new AuthResponse(token));
        }
    }
    
    // AuthRequest.java
    package com.example.demo.controller;
    
    public class AuthRequest {
        private String username;
        private String password;
    
        // Getters and Setters
    }
    
    // AuthResponse.java
    package com.example.demo.controller;
    
    public class AuthResponse {
        private final String token;
    
        public AuthResponse(String token) {
            this.token = token;
        }
    
        public String getToken() {
            return token;
        }
    }
    

    Customizing Swagger UI

    Updating Application Information

    You can customize the Swagger UI with application-specific details such as name, version, and description.

    // SwaggerConfig.java
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.OAS_30)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }
    
    private ApiInfo apiInfo() {
        return new ApiInfo(
                "User API",
                "Spring Boot RESTful API Demo",
                "1.0",
                "Terms of service",
                new Contact("Your Name", "www.example.com", "[email protected]"),
                "License of API",
                "API license URL",
                Collections.emptyList());
    }
    

    Adding Authorization to Swagger

    Swagger UI allows adding authorization headers to API requests, enabling authenticated testing.

    1. Configure Security Schemes
    // SwaggerConfig.java
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.OAS_30)
                .securitySchemes(Arrays.asList(apiKey()))
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo"))
                .paths(PathSelectors.any())
                .build();
    }
    
    private ApiKey apiKey() {
        return new ApiKey("JWT", "Authorization", "header");
    }
    
  • Authorize Using JWT
    • Click the "Authorize" button in Swagger UI.
    • Enter the JWT token prefixed with Bearer (e.g., Bearer your_jwt_token).
    • Once authorized, Swagger will include the token in subsequent API requests.


    Testing APIs with Swagger

    Making Authenticated Requests

    1. Generate JWT Token
      • Use the /token endpoint with valid credentials to receive a JWT.
    2. Authorize Swagger UI
      • Click "Authorize" and enter the token as described earlier.
    3. Access Protected Endpoints
      • With authorization set, you can now access secured APIs directly from Swagger UI.

    Sample Program Code

    // SampleController.java
    package com.example.demo.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class SampleController {
    
        @GetMapping("/homepage")
        public String homepage() {
            return "homepage";
        }
    }
    

    Explanation

    • Endpoint: /homepage
    • Method: GET
    • Description: Returns a simple greeting.

    Program Output

    When accessing http://localhost:8080/homepage, the response will be:

    homepage
    

    Best Practices and Security Considerations

    Secure Storage of JWT Secrets

    Ensure that the secret key used for signing JWTs is stored securely and not exposed in the codebase. Use environment variables or secure vaults.

    Implement Token Expiry

    Always set an expiration time for JWTs to minimize the risk of token theft and misuse.

    Use HTTPS

    Ensure all API communications occur over HTTPS to protect data in transit.

    Validate Token Claims

    Always validate the claims within JWTs, such as issuer, audience, and expiration, to ensure token integrity.

    Regularly Update Dependencies

    Keep Swagger, JWT libraries, and other dependencies up-to-date to patch known vulnerabilities.


    Conclusion

    Integrating Swagger and JWT into Spring Boot RESTful APIs significantly enhances the development process by providing robust documentation and secure authentication mechanisms. Swagger simplifies API exploration and testing, while JWT ensures secure, stateless authentication suitable for scalable applications. By following the guidelines and best practices outlined in this eBook, developers can build secure, well-documented APIs that cater to both current and future needs.

    Keywords: Swagger, JWT, Spring Boot, RESTful API, API Documentation, JSON Web Token, Authentication, Authorization, Springfox, API Security, OpenAPI, Microservices, Stateless Authentication, Secure APIs

    Note: This article is AI generated.

    Share Article

    Back to Blog

    Single logo

    Empowering Education, One Course at a Time

    About
    About usContact usFAQs
    Legal
    Terms of ServicePrivacy Policy

    © 2026 StudyEasy Organization. All rights reserved.