Security System
Introduction
Ontimize security allows you to set permissions to each role that you assign to a specific user, so that different users can access (or not) the project services and methods.
Prerequisites
You can follow this tutorial using your own application, although for this example we will use an application created using the archetype that can be found on this page and with a REST service.
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-security-initial
Final example
/$ git clone https://github.com/ontimize/ontimize-examples
/ontimize-examples$ cd ontimize-examples
/ontimize-examples$ git checkout boot-security
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
Add new profile and user
We will add the following elements to the database:
- New user
- New user role
- Relationship between the new user and the new role
Modify the service to add security
In the projectwiki-boot module we will add the annotation @EnableAspectJAutoProxy(proxyTargetClass = false)
to the ServerApplication.java class and to each service method the @Secured({ PermissionsProviderSecured.SECURED })
annotation.
ServerApplication.java
CustomerService.java
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- ICandidateService.java
- 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
- application.yml
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.properties
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- CandidateDao.java
- UserDao.java
- UserRoleDao.java
- service
- CandidateService.java
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- CandidateDao.xml
- placeholders.properties
- RoleDao.xml
- RoleServerPermissionDao.xml
- ServerPermissionDao.xml
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- CandidateRestController.java
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- pom.xml
- README.md
- projectwiki-api
Add permissions for methods
Once the methods have been noted, it is necessary to add the interface of the service and the method that we have noted to the table of the DB that stores those elements and to the table that indicates the methods for which the role has permission.
If we try to make requests through Postman to the service of CandidateService, changing the authentication by the user candidate and password candidate, it will give an error, since the user candidate does not belong to the role admin which is the only role that has permissions to make these queries.