Skip to content

harman-04/spring-boot-client-server-swagger-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Spring Boot: API Security Config & Inter-Service Client

This module demonstrates how to configure professional-grade documentation for secured APIs and how one Spring Boot application can "talk" to another.


Technical Concepts

1. Bearer Auth Documentation (@SecurityScheme)

In SwaggerConfig.java, we define a bearerAuth security scheme.

  • The "Authorize" Button: This tells Swagger to display a padlock icon in the UI.
  • Documentation Only: Note that this doesn't implement JWT security logic (which requires Spring Security), but it documents it so that users know they need to provide a token in the Authorization: Bearer <token> format.

2. The RestTemplate (The "Phone" of Spring)

RestTemplate is a synchronous HTTP client used to consume external REST APIs.

  • Config: We define it as a @Bean in AppConfig.java to make it injectable anywhere in the app.
  • Usage: In SpringClientApplication, we use it to reach out to the "Server" service and pull data.

3. CommandLineRunner

This is a functional interface used to run a piece of code after the Spring application context has fully started. It's perfect for testing clients or seeding data immediately on startup.


Project Structure: The Duo

Part A: spring-swagger-config (The Provider)

  • Port: 8080 (Default)
  • Goal: Provides a /hello endpoint and a Swagger UI that shows how to use Bearer tokens.
  • Key Class: SwaggerConfig.java – Uses the modern OpenAPI bean approach to customize API metadata.

Part B: spring-client (The Consumer)

  • Port: 8081 (Configured in application.properties)
  • Goal: Calls the Provider and prints the message to the console.
  • Key Class: HelloWorldClient.java (Logic to call 8080/api/hello) and SpringClientApplication.

Execution Flow: Microservices in Action

  1. Start the Provider: Run SpringSwaggerConfigApplication.
    • Visit http://localhost:8080/swagger-ui/index.html to see the "Sample Swagger API Documentation" and the Authorize button.
  2. Start the Consumer: Run SpringClientApplication.
  3. Observation: Check the console of the Consumer. You will see: Response from API: Hello World This confirms the two services successfully communicated over the network.

Key Learning Points

Component Role Why use it?
@SecurityScheme Metadata Tells users the API is protected by JWT.
RestTemplate HTTP Client Allows Java apps to fetch data from other web services.
server.port=8081 Port Config Prevents port conflicts when running multiple apps locally.
@Bean Dependency Decouples the creation of objects (like RestTemplate) from their usage.

Pro-Tip: The Future of Clients

While RestTemplate is the classic choice, Spring now recommends the RestClient (introduced in Spring Boot 3.2/4.x) for synchronous calls or WebClient for reactive, non-blocking calls. Your knowledge of RestTemplate is a perfect foundation for these newer tools!


Output in Client Application


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v4.0.1)

2026-01-21T17:07:07.468+05:30  INFO 5836 --- [spring-client] [  restartedMain] c.e.s.SpringClientApplication            : Starting SpringClientApplication using Java 25.0.1 with PID 5836 (C:\Users\Hp\IdeaProjects\spring-boot-client-server-swagger-auth\spring-client\target\classes started by Hp in C:\Users\Hp\IdeaProjects\spring-boot-client-server-swagger-auth\spring-client)
2026-01-21T17:07:07.496+05:30  INFO 5836 --- [spring-client] [  restartedMain] c.e.s.SpringClientApplication            : No active profile set, falling back to 1 default profile: "default"
2026-01-21T17:07:07.630+05:30  INFO 5836 --- [spring-client] [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2026-01-21T17:07:07.630+05:30  INFO 5836 --- [spring-client] [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2026-01-21T17:07:09.515+05:30  INFO 5836 --- [spring-client] [  restartedMain] o.s.boot.tomcat.TomcatWebServer          : Tomcat initialized with port 8081 (http)
2026-01-21T17:07:09.751+05:30  INFO 5836 --- [spring-client] [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2026-01-21T17:07:09.753+05:30  INFO 5836 --- [spring-client] [  restartedMain] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/11.0.15]
2026-01-21T17:07:10.500+05:30  INFO 5836 --- [spring-client] [  restartedMain] b.w.c.s.WebApplicationContextInitializer : Root WebApplicationContext: initialization completed in 2868 ms
2026-01-21T17:07:11.358+05:30  INFO 5836 --- [spring-client] [  restartedMain] o.s.boot.tomcat.TomcatWebServer          : Tomcat started on port 8081 (http) with context path '/'
2026-01-21T17:07:11.367+05:30  INFO 5836 --- [spring-client] [  restartedMain] c.e.s.SpringClientApplication            : Started SpringClientApplication in 4.709 seconds (process running for 5.689)
Response from API: Hello World! 👈 **see here**

About

A dual-service project demonstrating Bearer (JWT) security metadata in Swagger UI and synchronous inter-service communication using RestTemplate.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages