You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: java/security.md
+19-17Lines changed: 19 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,15 +42,15 @@ Without security configured, CDS services are exposed to public. Proper configur
42
42
43
43
## Authentication { #authentication}
44
44
45
-
Authentication rejects user requests with invalid authentication and limits the possible resource impact.
45
+
Authentication rejects user requests with invalid authentication and limits the possible resource impact.
46
46
47
47
Rejecting them as soon as possible is one of the reasons why it's not an integral part of the CAP runtime and needs to be configured on the application framework level. In addition, CAP Java is based on a [modular architecture](./developing-applications/building#modular_architecture) and allows flexible configuration of any authentication method.
48
48
By default, it supports the standard BTP platform identity services [out of the box](#xsuaa-ias):
-[SAP Authorization and Trust Management Service (XSUAA)](https://help.sap.com/docs/authorization-and-trust-management-service) - previous offering scoped to a BTP landscape
52
52
53
-
Which are highly recommended for production usage. For specific use cases, [custom authentication](#custom-authentication) can be configured as well.
53
+
Which are highly recommended for production usage. For specific use cases, [custom authentication](#custom-authentication) can be configured as well.
54
54
Local development and testing can be done easily with built-in [mock user](#mock-users) support.
55
55
56
56
### Configure XSUAA and IAS Authentication { #xsuaa-ias}
@@ -164,7 +164,10 @@ Please note that the authentication mode has no impact on the *authorization* be
164
164
165
165
#### Customizing Spring Boot Security Configuration { #custom-spring-security-config}
166
166
167
-
If you want to explicitly change the automatic security configuration, you can add an _additional_ Spring security configuration on top that overrides the default configuration by CAP. This can be useful, for instance, if an alternative authentication method is required for *specific endpoints* of your application.
167
+
If you want to explicitly change the automatic security configuration, you can add an _additional_ Spring security configuration on top that overrides the default configuration by CAP.
168
+
This can be useful, for instance, if an alternative authentication method is required for *specific endpoints* of your application.
169
+
170
+
As the default security configurations provided by CAP act as the last line of defense and handle any request by default, you need to ensure that your custom security configurations have higher precedence. At the `SecurityFilterChain` bean method, set the `@Order` annotation with a lower numeric value, for example `1`:
168
171
169
172
```java
170
173
@Configuration
@@ -184,9 +187,6 @@ public class AppSecurityConfig {
184
187
}
185
188
```
186
189
Due to the custom configuration, all URLs matching `/public/**` are opened for public access.
187
-
::: tip
188
-
The Spring `SecurityFilterChain` requires CAP Java SDK [1.27.x](../releases/archive/2022/aug22#minimum-spring-boot-version-2-7-x) or later. Older versions need to use the deprecated `WebSecurityConfigurerAdapter`.
189
-
:::
190
190
191
191
::: warning _❗ Warning_<!---->
192
192
Be cautious with the configuration of the `HttpSecurity` instance in your custom configuration. Make sure that only the intended endpoints are affected.
@@ -201,7 +201,7 @@ public class ActuatorSecurityConfig {
@@ -214,6 +214,8 @@ public class ActuatorSecurityConfig {
214
214
}
215
215
```
216
216
217
+
In case you want to write your own custom security configuration that acts as a last line of defense and handles any request you need to disable the CAP security configurations by setting <Configjava>cds.security.authentication.authConfig.enabled: false</Config>, as Spring Security forbids registering multiple security configurations with an any request security matcher.
You're free to configure any authentication method according to your needs. CAP isn't bound to any specific authentication method or user representation such as introduced with XSUAA, it rather runs the requests based on a [user abstraction](../guides/security/authorization#user-claims). The CAP user of a request is represented by a [UserInfo](https://www.javadoc.io/doc/com.sap.cds/cds-services-api/latest/com/sap/cds/services/request/UserInfo.html) object that can be retrieved from the [RequestContext](https://www.javadoc.io/doc/com.sap.cds/cds-services-api/latest/com/sap/cds/services/request/RequestContext.html) as explained in [Enforcement API & Custom Handlers](#enforcement-api).
@@ -238,7 +240,7 @@ public class CustomUserInfoProvider implements UserInfoProvider {
238
240
}
239
241
}
240
242
if (userInfo !=null) {
241
-
/* any modification of the resolved user goes here: */
243
+
/* any modification of the resolved user goes here: */
@@ -625,7 +627,7 @@ In addition to standard authorization, CAP Java provides additional out of the b
625
627
626
628
### Deep Authorization { #deep-auth}
627
629
628
-
Queries to Application Services are not only authorized by the target entity which has a `@restrict` or `@requires` annotation, but also for all __associated entities__ that are used in the statement.
630
+
Queries to Application Services are not only authorized by the target entity which has a `@restrict` or `@requires` annotation, but also for all __associated entities__ that are used in the statement.
629
631
__Compositions__ are neither checked nor extended with additional filters.
630
632
For instance, consider the following model:
631
633
@@ -643,15 +645,15 @@ entity Orders {
643
645
}
644
646
```
645
647
646
-
For the following OData request `GET Orders(ID='1')/items?$expand=book`, authorizations for `Orders` and for `Books` are checked.
647
-
If the entity `Books` has a `where` clause for [instance-based authorization](/java/security#instance-based-auth),
648
+
For the following OData request `GET Orders(ID='1')/items?$expand=book`, authorizations for `Orders` and for `Books` are checked.
649
+
If the entity `Books` has a `where` clause for [instance-based authorization](/java/security#instance-based-auth),
648
650
it will be added as a filter to the sub-request with the expand.
649
651
650
-
Custom CQL statements submitted to the [Application Service](/java/cqn-services/application-services) instances
652
+
Custom CQL statements submitted to the [Application Service](/java/cqn-services/application-services) instances
651
653
are also authorized by the same rules including the path expressions and subqueries used in them.
652
654
653
-
For example, the following statement checks role-based authorizations for both `Orders` and `Books`,
654
-
because the association to `Books` is used in the select list.
655
+
For example, the following statement checks role-based authorizations for both `Orders` and `Books`,
656
+
because the association to `Books` is used in the select list.
655
657
656
658
```java
657
659
Select.from(Orders_.class,
@@ -675,15 +677,15 @@ Be careful when you modify or extend the statements in custom handlers.
675
677
Make sure you keep the filters for authorization.
676
678
:::
677
679
678
-
Starting with CAP Java `4.0`, deep authorization is on by default.
680
+
Starting with CAP Java `4.0`, deep authorization is on by default.
679
681
It can be disabled by setting <Config java>cds.security.authorization.deep.enabled: false</Config>.
680
682
681
683
[Learn more about `@restrict.where` in the instance-based authorization guide.](/guides/security/authorization#instance-based-auth){.learn-more}
682
684
683
685
### Forbidden on Rejected Entity Selection { #reject-403 }
684
686
685
-
Entities that have an instance-based authorization condition, that is [`@restrict.where`](/guides/security/authorization#restrict-annotation),
686
-
are guarded by the CAP Java runtime by adding a filter condition to the DB query **excluding not matching instances from the result**.
687
+
Entities that have an instance-based authorization condition, that is [`@restrict.where`](/guides/security/authorization#restrict-annotation),
688
+
are guarded by the CAP Java runtime by adding a filter condition to the DB query **excluding not matching instances from the result**.
687
689
Hence, if the user isn't authorized to query an entity, requests targeting a *single* entity return *404 - Not Found* response and not *403 - Forbidden*.
688
690
689
691
To allow the UI to distinguish between *not found* and *forbidden*, CAP Java can detect this situation and rejects`PATCH` and `DELETE` requests to single entities with forbidden accordingly.
0 commit comments