Skip to content

IndustryApps/Sample-App-Springboot

Repository files navigation

Springboot sample app

When developing an app for IndustryApps appstore, we should take care of two things. Service Discovery (Eureka) registration and Authorization on AAS requests.

Service Discovery Registration

Adding dependencies

  1. Spring cloud dependency
ext {
    springCloudVersion = '2020.0.4'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

dependencies {
    implementation "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
  1. Eureka dependency
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:3.0.4'

Configuring Eureka Properties

  1. Public hosting the app and launching though iApps
eureka:
  instance:
    hostname: <app's public domain name>
    securePort: 443
    securePortEnabled: true
  server:
    url: https://servicediscovery.uat.industryapps.net
  client:
    serviceUrl:
      defaultZone: ${eureka.server.url}/eureka/
  1. Hosting the app as a container in IndustryApps ecosystem
eureka:
  instance:
    preferIpAddress: true
    securePortEnabled: false
    homePageUrl: http://${eureka.instance.hostname}:${eureka.instance.port}
  server:
    url: https://servicediscovery.uat.industryapps.net
  client:
    serviceUrl:
      defaultZone: ${eureka.server.url}/eureka/

Eureka server URL for UAT is https://servicediscovery.uat.industryapps.net. For production, it will be https://servicediscovery.industryapps.net

Set Application name as AppCode

spring:
  application:
    name: <AppCode>

You can find the AppCode in App Information page of publish section.

Set the application running port to 80

server:
  port: 80

Add @EnableEurekaClient annotation to the Application Class

@SpringBootApplication
@EnableEurekaClient
public class OeeAppApplication {

    public static void main(String[] args) {
        SpringApplication.run(OeeAppApplication.class, args);
    }

}

Accessing data through AAS Gateway

App authentication token should be generated and sent on all request headers as described here .

In this sample app, we used FeignClient for rest communication and for token management, we used Springboot OAuth2 Client

Adding dependencies

implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:3.0.5'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client:2.5.6'

OAuth2 configuration

spring.security.oauth2.client:
  registration:
    demo-app:
      client-id: <App ClientId>
      client-secret: <App ClientSecret>
      authorization-grant-type: client_credentials
      client-authentication-method: post
  provider:
    demo-app:
      token-uri: https://auth.uat.industryapps.net/auth/realms/IndustryApps/protocol/openid-connect/token

Auth Token URL in UAT is https://auth.uat.industryapps.net/auth/realms/IndustryApps/protocol/openid-connect/token. For production, it will be https://auth.industryapps.net/auth/realms/IndustryApps/protocol/openid-connect/token

In Feign Client configuration class we added an interceptor to add the Authorization header with the Bearer token (App token) in all requests.

@Bean
public RequestInterceptor requestInterceptor() {
    return requestTemplate -> {
        requestTemplate.header(HttpHeaders.AUTHORIZATION, oauth2Provider.getAuthenticationToken(AUTH_SERVER_NAME));
    };
}

Containerizing the app

You can use some frameworks like jib, or you can build the jar and containerize as below.

FROM openjdk:11.0.12-jdk
MAINTAINER baeldung.com
COPY build/libs/OEEApp-0.0.1-SNAPSHOT.jar OEEApp-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","/OEEApp-0.0.1-SNAPSHOT.jar"]