# 5.3 Securing a Spring Boot Application

ZTrust integrates with **Spring Boot** using **OpenID Connect (OIDC)** to handle authentication and authorization. This setup enables secure login and role-based access for your applications.

#### Prerequisites

Before integrating your Spring Boot application with ZTrust, ensure the following are in place:

* **ZTrust SSO** – A running ZTrust instance that will act as the identity and access management provider.
* **Spring Boot Application** – Your application should be set up with **Spring Security**, which provides the foundation for authentication and authorization.
* **Configured Realm and Client** – A realm and client must already be created in ZTrust, with the client configured for OIDC. These settings will be used by your Spring Boot app to authenticate users and validate tokens.

With these prerequisites, your application will be ready to establish a secure connection with ZTrust using OpenID Connect.

### Step 1: Set up ZTrust

1. Access ZTrust Admin Console:

   <figure><img src="https://1778922777-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3EUK5AUZv0UVaI5S0CTM%2Fuploads%2F3CFYuMdQrGvZPh4mvkcp%2Fimage.png?alt=media&#x26;token=9079e2dd-a3bb-4d08-9098-253df7399217" alt=""><figcaption><p>Fig 5.3.a: Master realm welcome page</p></figcaption></figure>

2. Click on **Manage Realms** in the sidebar to view the list of realms available in your **ZTrust**.

   <figure><img src="https://1778922777-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3EUK5AUZv0UVaI5S0CTM%2Fuploads%2FAeVUxDaUMgQmnQCTpMUr%2Fimage.png?alt=media&#x26;token=f7f3b7ea-5ec8-4e54-a154-7c63d41acf11" alt=""><figcaption><p>Fig 5.3.b: List of available realms under manage realms</p></figcaption></figure>

3. From the list of realms, select the **realm** where you want to configure **Geo fencing**.\\

   <figure><img src="https://1778922777-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3EUK5AUZv0UVaI5S0CTM%2Fuploads%2FbXaN7CWO4K9CysxuHMZS%2Fimage.png?alt=media&#x26;token=fad61d41-6047-4f93-8f26-5a765d1f1fe6" alt=""><figcaption><p>Fig 5.3.c: Welcome to Demo realm</p></figcaption></figure>

4. From the left **sidebar**, navigate to the **Clients** section.

   <figure><img src="https://1778922777-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3EUK5AUZv0UVaI5S0CTM%2Fuploads%2FzLIQGWWP8e1pOtG3ysBb%2Fimage.png?alt=media&#x26;token=55f3b23d-9037-4b21-81ae-fce5663d011c" alt="" width="96"><figcaption><p>Fig 5.3.d: Navigating to clients section in side bar</p></figcaption></figure>

5. You will see a list of **clients** (applications). Choose/ Create the **client** for which you want to secure Spring Boot Application.

   <figure><img src="https://1778922777-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3EUK5AUZv0UVaI5S0CTM%2Fuploads%2FBlLEb9sJI1t7srGaTDzz%2Fimage.png?alt=media&#x26;token=6571a5f5-6d1b-4101-af37-04629fa96c7c" alt=""><figcaption><p>Fig 5.3.e: List of avalible clients</p></figcaption></figure>

6. After select your client, it will take you to the settings page

   <figure><img src="https://1778922777-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3EUK5AUZv0UVaI5S0CTM%2Fuploads%2FqQT0Kisuh0FTGt3WrxGT%2Fimage.png?alt=media&#x26;token=f1bc4fd2-5204-4414-9af7-1928a134c221" alt=""><figcaption><p>Fig 5.3.f: Client settings page</p></figcaption></figure>

7. Enter your application’s **redirect URL** in the **Valid Redirect URIs** field.

   <figure><img src="https://1778922777-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3EUK5AUZv0UVaI5S0CTM%2Fuploads%2FsaKFca9umI7oth9lsqvR%2Fimage.png?alt=media&#x26;token=34c7e4f1-5338-4d74-980f-a6210d9d8614" alt=""><figcaption><p>Fig 5.3.g: In client settings tab general settings fields</p></figcaption></figure>

8. Then under **Capability** config turn on **Client authentication** and save

   <figure><img src="https://1778922777-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3EUK5AUZv0UVaI5S0CTM%2Fuploads%2FtmgLB1TxpKk5cHyNVjJj%2Fimage.png?alt=media&#x26;token=73c4ece9-4b12-45a3-b2da-7de89e2d5919" alt="" width="563"><figcaption><p>Fig 5.3.h: In client settings tab capability config options</p></figcaption></figure>

9. You will now see a new tab enabled, called **Credentials**.\\

   <figure><img src="https://1778922777-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3EUK5AUZv0UVaI5S0CTM%2Fuploads%2F6uZMB1De40PWuxsaxxw5%2Fimage.png?alt=media&#x26;token=5e67344e-b3f3-48af-add1-4edb07c1cc88" alt="" width="563"><figcaption><p>Fig 5.3.i: Client settings tab</p></figcaption></figure>

10. Navigate to the **Credentials** tab, where you can view and copy the **Client Secret**.

    <figure><img src="https://1778922777-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3EUK5AUZv0UVaI5S0CTM%2Fuploads%2FFgEuJ4KGD0TOUQ215oYs%2Fimage.png?alt=media&#x26;token=779ac9cd-ecb8-48dd-b95f-89d904272dcf" alt="" width="563"><figcaption><p>Fig 5.3.j: Navigating to Credentials tab</p></figcaption></figure>

11. With the ZTrust configuration complete, we can now move on to the Spring Boot side of the setup.

### Step 2: Set up Spring Boot

1. **Add Dependencies to Your Spring Boot App**\
   \
   If you're using Spring Boot 3.x:

   ```
   <!-- pom.xml -->
   <dependencies>
     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
     </dependency>
     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-security</artifactId>
     </dependency>
     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
   </dependencies>
   ```

   If you want to enable OAuth2 Login, also add:

   ```
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-oauth2-client</artifactId>
   </dependency>

   ```

2. **Configure application.yml or application.properties**\\

   #### application.yml example:

   ```
   server:
     port: 8081
   spring:
     security:
       oauth2:
         resourceserver:
           jwt:
             issuer-uri: https://auth.example.com/realms/organisation-realm
         client:
           registration:
             ztrust:
               client-id: application-client
               client-secret: YOUR_CLIENT_SECRET
               authorization-grant-type: authorization_code
               redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
               scope: openid,profile,email
           provider:
             ztrust:
               issuer-uri: https://auth.example.com/realms/organisation-realm
   ```

3. **Secure Endpoints**\\

   Create a basic SecurityConfig:

   ```
   @EnableWebSecurity
   public class SecurityConfig {
       @Bean
       public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
           http
               .authorizeHttpRequests(authz -> authz
                   .requestMatchers("/public/**").permitAll()
                   .anyRequest().authenticated()
               )
               .oauth2Login(Customizer.withDefaults())
               .oauth2ResourceServer(oauth2 -> oauth2.jwt());
           return http.build();
       }
   }
   ```
