Thursday, 21 August 2025

Multi-Module Spring Boot Microservices Project with JWT

Multi-Module Spring Boot Microservices Project with JWT (POC, no DB) in IntelliJ step by step.

you’ll have one root Maven project and inside it multiple submodules (auth-service, api-gateway, order-service, product-service)


---


🛠 Step 1: Create Parent Project


1. In IntelliJ → New Project → Choose Maven.



2. Name it jwt-microservices-poc.



3. Select Packaging: pom.



4. This parent project will not have code, only manage dependencies.




pom.xml (Parent)


<project xmlns="http://maven.apache.org/POM/4.0.0"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>


    <groupId>com.neeraj</groupId>

    <artifactId>jwt-microservices-poc</artifactId>

    <version>1.0-SNAPSHOT</version>

    <packaging>pom</packaging>


    <modules>

        <module>auth-service</module>

        <module>api-gateway</module>

        <module>order-service</module>

        <module>product-service</module>

    </modules>


    <properties>

        <java.version>11</java.version>

        <spring.boot.version>2.7.18</spring.boot.version>

        <spring.cloud.version>2021.0.8</spring.cloud.version>

    </properties>


    <dependencyManagement>

        <dependencies>

            <dependency>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-dependencies</artifactId>

                <version>${spring.boot.version}</version>

                <type>pom</type>

                <scope>import</scope>

            </dependency>

            <dependency>

                <groupId>org.springframework.cloud</groupId>

                <artifactId>spring-cloud-dependencies</artifactId>

                <version>${spring.cloud.version}</version>

                <type>pom</type>

                <scope>import</scope>

            </dependency>

        </dependencies>

    </dependencyManagement>

</project>



---


🛠 Step 2: Create Submodules


Inside jwt-microservices-poc, create 4 Maven Modules:


auth-service


api-gateway


order-service


product-service



Each submodule will have its own pom.xml.



---


🛠 Step 3: Auth Service (JWT Issuer)


📁 auth-service/pom.xml


<project>

    <parent>

        <groupId>com.neeraj</groupId>

        <artifactId>jwt-microservices-poc</artifactId>

        <version>1.0-SNAPSHOT</version>

    </parent>

    <artifactId>auth-service</artifactId>


    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-security</artifactId>

        </dependency>

        <dependency>

            <groupId>io.jsonwebtoken</groupId>

            <artifactId>jjwt-api</artifactId>

            <version>0.11.5</version>

        </dependency>

        <dependency>

            <groupId>io.jsonwebtoken</groupId>

            <artifactId>jjwt-impl</artifactId>

            <version>0.11.5</version>

            <scope>runtime</scope>

        </dependency>

        <dependency>

            <groupId>io.jsonwebtoken</groupId>

            <artifactId>jjwt-jackson</artifactId>

            <version>0.11.5</version>

            <scope>runtime</scope>

        </dependency>

    </dependencies>

</project>


👉 Same SecurityConfig, JwtUtil, and AuthController as I gave you earlier.

Set server.port=8081 in application.properties.



---


🛠 Step 4: API Gateway


📁 api-gateway/pom.xml


<project>

    <parent>

        <groupId>com.neeraj</groupId>

        <artifactId>jwt-microservices-poc</artifactId>

        <version>1.0-SNAPSHOT</version>

    </parent>

    <artifactId>api-gateway</artifactId>


    <dependencies>

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-gateway</artifactId>

        </dependency>

        <dependency>

            <groupId>io.jsonwebtoken</groupId>

            <artifactId>jjwt-api</artifactId>

            <version>0.11.5</version>

        </dependency>

    </dependencies>

</project>


📁 application.yml


server:

  port: 8080


spring:

  cloud:

    gateway:

      routes:

        - id: order-service

          uri: http://localhost:8082/

          predicates:

            - Path=/order/**

        - id: product-service

          uri: http://localhost:8083/

          predicates:

            - Path=/product/**


📁 Add JwtAuthFilter as shown before.



---


🛠 Step 5: Order Service


📁 order-service/pom.xml


<project>

    <parent>

        <groupId>com.neeraj</groupId>

        <artifactId>jwt-microservices-poc</artifactId>

        <version>1.0-SNAPSHOT</version>

    </parent>

    <artifactId>order-service</artifactId>


    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

    </dependencies>

</project>


📁 OrderController.java


@RestController

@RequestMapping("/order")

public class OrderController {

    @GetMapping("/details")

    public String getOrder() {

        return "Order details fetched successfully!";

    }

}


📁 application.properties


server.port=8082



---


🛠 Step 6: Product Service


📁 product-service/pom.xml


<project>

    <parent>

        <groupId>com.neeraj</groupId>

        <artifactId>jwt-microservices-poc</artifactId>

        <version>1.0-SNAPSHOT</version>

    </parent>

    <artifactId>product-service</artifactId>


    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

    </dependencies>

</project>


📁 ProductController.java


@RestController

@RequestMapping("/product")

public class ProductController {

    @GetMapping("/list")

    public String getProducts() {

        return "Product list fetched successfully!";

    }

}


📁 application.properties


server.port=8083



---


🛠 Step 7: Run & Test (Stepwise in IntelliJ)


1. Right-click Parent Project → Run ‘mvn clean install’ (this builds all modules).



2. Run AuthServiceApplication → port 8081.



3. Run ApiGatewayApplication → port 8080.



4. Run OrderServiceApplication → port 8082.



5. Run ProductServiceApplication → port 8083.





---


🛠 Step 8: Verify


👉 Get Token:


curl -X POST http://localhost:8081/auth/login \

 -H "Content-Type: application/json" \

 -d '{"username":"neeraj", "password":"password"}'


👉 Call Order Service via Gateway:


curl -X GET http://localhost:8080/order/details \

 -H "Authorization: Bearer <TOKEN>"


👉 Call Product Service via Gateway:


curl -X GET http://localhost:8080/product/list \

 -H "Authorization: Bearer <TOKEN>"


✅ Both return data when token is valid.





No comments:

Post a Comment