Keycloak Security using the application properties
This functionality works only for Ontimize Boot version 3.15.0 or above. Actual release version:
Introduction
Ontimize allows you to provide a list of tenants in the application properties. This is useful when the application will manage a fixed list of tenants.
Prerequisites
There are 2 options to follow this tutorial, clone the repository with the initial state and follow the tutorial step by step, or download the final example and see which files are new and which have been updated.
Initial project
/$ git clone https://github.com/ontimize/ontimize-examples
/ontimize-examples$ cd ontimize-examples
/ontimize-examples$ git checkout boot-keycloak-login-multitenant-by-properties-initial
Final example
/$ git clone https://github.com/ontimize/ontimize-examples
/ontimize-examples$ cd ontimize-examples
/ontimize-examples$ git checkout boot-keycloak-login-multitenant-by-properties
Keycloak
We need a Keycloak instance, then we must configure two realms with their own clients, roles and users. For the examples we will use the next settings:
Tenant 1:
Element | Value | Meaning |
---|---|---|
Url | http://localhost:8082 | The URL of the host for keycloak security |
Realm | demo1 | The realm name |
Client | projectwiki1 | The client name (Enable the implicit flow check to allow testing with Swagger UI) |
Role | admin | The role name |
User | demo1 | The username |
Password | demouser | The user password |
Tenant 2:
Element | Value | Meaning |
---|---|---|
Url | http://localhost:8082 | The URL of the host for keycloak security |
Realm | demo2 | The realm name |
Client | projectwiki2 | The client name (Enable the implicit flow check to allow testing with Swagger UI) |
Role | admin | The role name (For the examples we will use client roles) |
User | demo2 | The username |
Password | demouser | The user password |
To simplify the code being written, three dots (…) may appear in some parts of the code. This indicates that there may be previous code before and after those dots.
Steps
Configure the role
If the role does not exist in the database, we can create it with the following command:
Add the Keycloak dependency to the Boot module
The Keycloak system is integrated in the Ontimize Core module, so we need to declare it as a project dependency.
pom.xml
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- IUserService.java
- service
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- public
- index.html
- application.yml
- public
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- UserDao.java
- UserRoleDao.java
- service
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- placeholders.properties
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-openapi
- src
- main
- ontimize
- marker-ws-ontimize-openapi-generator
- resources
- public
- restapi
- api
- Test.yml
- User.yml
- base
- AdvancedEntityResult.yml
- AdvancedQueryParameter.yml
- ColumnsParameter.yml
- DeleteParameter.yml
- DocumentIdentifier.yml
- EntityResult.yml
- ExportParameter.yml
- FileListParameter.yml
- FilterParameter.yml
- InsertParameter.yml
- MultipartFile.yml
- MultipartFiles.yml
- Number.yml
- Object.yml
- OFile.yml
- OFiles.yml
- Operator.yml
- QueryParameter.yml
- Responses.yml
- SQLOrder.yml
- String.yml
- UpdateFileParameter.yml
- UpdateParameter.yml
- Void.yml
- openapi-rest.yml
- api
- restapi
- public
- ontimize
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
Enable the Keycloak security
In the application.yml we need to change the ontimize security mode to keycloak and add the following properties (More information in this link):
application.yml
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- IUserService.java
- service
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- public
- index.html
- application.yml
- public
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- UserDao.java
- UserRoleDao.java
- service
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- placeholders.properties
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-openapi
- src
- main
- ontimize
- marker-ws-ontimize-openapi-generator
- resources
- public
- restapi
- api
- Test.yml
- User.yml
- base
- AdvancedEntityResult.yml
- AdvancedQueryParameter.yml
- ColumnsParameter.yml
- DeleteParameter.yml
- DocumentIdentifier.yml
- EntityResult.yml
- ExportParameter.yml
- FileListParameter.yml
- FilterParameter.yml
- InsertParameter.yml
- MultipartFile.yml
- MultipartFiles.yml
- Number.yml
- Object.yml
- OFile.yml
- OFiles.yml
- Operator.yml
- QueryParameter.yml
- Responses.yml
- SQLOrder.yml
- String.yml
- UpdateFileParameter.yml
- UpdateParameter.yml
- Void.yml
- openapi-rest.yml
- api
- restapi
- public
- ontimize
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
Removing the configurations and classes that are no longer needed
Once the Keycloak authentication had been implemented, the configurations and the data access objects for Ontimize authentication can be removed.
OpenAPI declaration files
The CRUD endpoints for the users management are no longer used, then we can remove tem or comment out in the openapi-rest.yml and user.yml files.
openapi-rest.yml
User.yml
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- IUserService.java
- service
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- public
- index.html
- application.yml
- public
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- UserDao.java
- UserRoleDao.java
- service
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- placeholders.properties
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-openapi
- src
- main
- ontimize
- marker-ws-ontimize-openapi-generator
- resources
- public
- restapi
- api
- Test.yml
- User.yml
- base
- AdvancedEntityResult.yml
- AdvancedQueryParameter.yml
- ColumnsParameter.yml
- DeleteParameter.yml
- DocumentIdentifier.yml
- EntityResult.yml
- ExportParameter.yml
- FileListParameter.yml
- FilterParameter.yml
- InsertParameter.yml
- MultipartFile.yml
- MultipartFiles.yml
- Number.yml
- Object.yml
- OFile.yml
- OFiles.yml
- Operator.yml
- QueryParameter.yml
- Responses.yml
- SQLOrder.yml
- String.yml
- UpdateFileParameter.yml
- UpdateParameter.yml
- Void.yml
- openapi-rest.yml
- api
- restapi
- public
- ontimize
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
Configurations for user information in application.yml
The user-information-service and user-role-information-service configurations under ontimize.security are no longer used and we can remove them.
application.yml
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- IUserService.java
- service
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- public
- index.html
- application.yml
- public
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- UserDao.java
- UserRoleDao.java
- service
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- placeholders.properties
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-openapi
- src
- main
- ontimize
- marker-ws-ontimize-openapi-generator
- resources
- public
- restapi
- api
- Test.yml
- User.yml
- base
- AdvancedEntityResult.yml
- AdvancedQueryParameter.yml
- ColumnsParameter.yml
- DeleteParameter.yml
- DocumentIdentifier.yml
- EntityResult.yml
- ExportParameter.yml
- FileListParameter.yml
- FilterParameter.yml
- InsertParameter.yml
- MultipartFile.yml
- MultipartFiles.yml
- Number.yml
- Object.yml
- OFile.yml
- OFiles.yml
- Operator.yml
- QueryParameter.yml
- Responses.yml
- SQLOrder.yml
- String.yml
- UpdateFileParameter.yml
- UpdateParameter.yml
- Void.yml
- openapi-rest.yml
- api
- restapi
- public
- ontimize
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
User CRUD API
The CRUD endpoints for the users management are no longer used, then we can remove them or comment out the inheritance of the ORestController class in the UserRestController.
UserRestController.java
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- IUserService.java
- service
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- public
- index.html
- application.yml
- public
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- UserDao.java
- UserRoleDao.java
- service
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- placeholders.properties
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-openapi
- src
- main
- ontimize
- marker-ws-ontimize-openapi-generator
- resources
- public
- restapi
- api
- Test.yml
- User.yml
- base
- AdvancedEntityResult.yml
- AdvancedQueryParameter.yml
- ColumnsParameter.yml
- DeleteParameter.yml
- DocumentIdentifier.yml
- EntityResult.yml
- ExportParameter.yml
- FileListParameter.yml
- FilterParameter.yml
- InsertParameter.yml
- MultipartFile.yml
- MultipartFiles.yml
- Number.yml
- Object.yml
- OFile.yml
- OFiles.yml
- Operator.yml
- QueryParameter.yml
- Responses.yml
- SQLOrder.yml
- String.yml
- UpdateFileParameter.yml
- UpdateParameter.yml
- Void.yml
- openapi-rest.yml
- api
- restapi
- public
- ontimize
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
Services and data access objects for user information
The user services and DAOs are no longer needed and we can remove them.
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- IUserService.java
- service
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- public
- index.html
- application.yml
- public
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- UserDao.java
- UserRoleDao.java
- service
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- placeholders.properties
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-openapi
- src
- main
- ontimize
- marker-ws-ontimize-openapi-generator
- resources
- public
- restapi
- api
- Test.yml
- User.yml
- base
- AdvancedEntityResult.yml
- AdvancedQueryParameter.yml
- ColumnsParameter.yml
- DeleteParameter.yml
- DocumentIdentifier.yml
- EntityResult.yml
- ExportParameter.yml
- FileListParameter.yml
- FilterParameter.yml
- InsertParameter.yml
- MultipartFile.yml
- MultipartFiles.yml
- Number.yml
- Object.yml
- OFile.yml
- OFiles.yml
- Operator.yml
- QueryParameter.yml
- Responses.yml
- SQLOrder.yml
- String.yml
- UpdateFileParameter.yml
- UpdateParameter.yml
- Void.yml
- openapi-rest.yml
- api
- restapi
- public
- ontimize
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
Tables for user information
With the above configurations and classes removed, we can remove the user tables from the database with the following commands:
templateDB.txt
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- IUserService.java
- service
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- public
- index.html
- application.yml
- public
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- UserDao.java
- UserRoleDao.java
- service
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- placeholders.properties
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-openapi
- src
- main
- ontimize
- marker-ws-ontimize-openapi-generator
- resources
- public
- restapi
- api
- Test.yml
- User.yml
- base
- AdvancedEntityResult.yml
- AdvancedQueryParameter.yml
- ColumnsParameter.yml
- DeleteParameter.yml
- DocumentIdentifier.yml
- EntityResult.yml
- ExportParameter.yml
- FileListParameter.yml
- FilterParameter.yml
- InsertParameter.yml
- MultipartFile.yml
- MultipartFiles.yml
- Number.yml
- Object.yml
- OFile.yml
- OFiles.yml
- Operator.yml
- QueryParameter.yml
- Responses.yml
- SQLOrder.yml
- String.yml
- UpdateFileParameter.yml
- UpdateParameter.yml
- Void.yml
- openapi-rest.yml
- api
- restapi
- public
- ontimize
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
Testing
In order to call the REST API of the application, we must first log in using the Keycloak authentication user interface, capture the returned token, and then provide it in the authentication header of the http request.
To test this functionality we will use the Swagger tool deployed with the application, but we need to change the authentication settings on the OpenAPI declaration file.
Modify openapi-rest.yml
In the openapi-rest.yml we need to change the security settings to use OAuth2, replacing the previous BasicAuth properties (More information in this link and in this link).
Also we need to add a new parameter to provide the tenant and in the User.yml file we need to add a reference to this parameter on each method:
openapi-rest.yml
User.yml
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- IUserService.java
- service
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- public
- index.html
- application.yml
- public
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- UserDao.java
- UserRoleDao.java
- service
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- placeholders.properties
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-openapi
- src
- main
- ontimize
- marker-ws-ontimize-openapi-generator
- resources
- public
- restapi
- api
- Test.yml
- User.yml
- base
- AdvancedEntityResult.yml
- AdvancedQueryParameter.yml
- ColumnsParameter.yml
- DeleteParameter.yml
- DocumentIdentifier.yml
- EntityResult.yml
- ExportParameter.yml
- FileListParameter.yml
- FilterParameter.yml
- InsertParameter.yml
- MultipartFile.yml
- MultipartFiles.yml
- Number.yml
- Object.yml
- OFile.yml
- OFiles.yml
- Operator.yml
- QueryParameter.yml
- Responses.yml
- SQLOrder.yml
- String.yml
- UpdateFileParameter.yml
- UpdateParameter.yml
- Void.yml
- openapi-rest.yml
- api
- restapi
- public
- ontimize
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
Testing using Tenant 1
Once we have built and launched the project, we can access to the application opening a web browser and going to http://localhost:33333):
We must open the authorization dialog by clicking on the Authorize button, provide the client and click on the Authorize button.
If there is not exists a previous Keycloak session, a new tab will be opened requesting then credentials to login.
Once the login process has been completed, it will return to the authorization dialog, showing the result of the authorization process.
Now, we can close the authorization dialog and test the API selecting the tenant 1.
Testing using Tenant 2
To test using the second tenant, we must open the authorization dialog again, provide the client and click on the Authorize button.
If there is not exists a previous Keycloak session, a new tab will be opened requesting then credentials to login.
Once the login process has been completed, it will return to the authorization dialog, showing the result of the authorization process.
Now, we can close the authorization dialog and test the API selecting the tenant 2.