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: _guides/cdi-reference.adoc
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -333,6 +333,7 @@ public class EagerAppBean {
333
333
334
334
NOTE: Quarkus users are encouraged to always prefer the `@Observes StartupEvent` to `@Initialized(ApplicationScoped.class)` as explained in the xref:lifecycle.adoc[Application Initialization and Termination] guide.
Copy file name to clipboardExpand all lines: _guides/security-authorize-web-endpoints-reference.adoc
+14-3Lines changed: 14 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -123,9 +123,9 @@ public class CustomNamedHttpSecPolicy implements HttpSecurityPolicy {
123
123
public Uni<CheckResult> checkPermission(RoutingContext event, Uni<SecurityIdentity> identity,
124
124
AuthorizationRequestContext requestContext) {
125
125
if (customRequestAuthorization(event)) {
126
-
return Uni.createFrom().item(CheckResult.PERMIT);
126
+
return CheckResult.permit();
127
127
}
128
-
return Uni.createFrom().item(CheckResult.DENY);
128
+
return CheckResult.deny();
129
129
}
130
130
131
131
@Override
@@ -182,6 +182,17 @@ You can also create global `HttpSecurityPolicy` invoked on every request.
182
182
Just do not implement the `io.quarkus.vertx.http.runtime.security.HttpSecurityPolicy.name` method and leave the policy nameless.
183
183
====
184
184
185
+
[[policy-active-cdi-request-context]]
186
+
=== Inject `@RequestScoped` beans into `HttpSecurityPolicy`
187
+
188
+
`@RequestScoped` beans can only be injected when the xref:cdi-reference.adoc#request-context-lifecycle[CDI request context] is active.
189
+
The context can be activated by users, for example with the `@ActivateRequestContext`, however authorization happens before Quarkus prepares some `@RequestScoped` beans.
190
+
We recommend to let Quarkus activate and prepare CDI request context for you.
191
+
For example, consider a situation where you want to inject a bean from the Jakarta REST context, such as the `jakarta.ws.rs.core.UriInfo` bean.
192
+
In this case, you must apply the `HttpSecurityPolicy` to Jakarta REST endpoints. This can be achieved in one of the following ways:
193
+
* Use the `@AuthorizationPolicy` security annotation.
194
+
* Set the `quarkus.http.auth.permission.custom1.applies-to=jaxrs` configuration property.
195
+
185
196
=== Matching on paths and methods
186
197
187
198
Permission sets can also specify paths and methods as a comma-separated list.
@@ -494,7 +505,7 @@ s| `@PermitAll` | Specifies that all security roles are allowed to invoke the sp
494
505
s| `@RolesAllowed` | Specifies the list of security roles allowed to access methods in an application.
495
506
s| `@Authenticated` | {project-name} provides the `io.quarkus.security.Authenticated` annotation that permits any authenticated user to access the resource. It's equivalent to `@RolesAllowed("**")`.
496
507
s| `@PermissionsAllowed` | Specifies the list of permissions that are allowed to invoke the specified methods.
497
-
s| `@AuthorizationPolicy` | Specifies named `io.quarkus.vertx.http.runtime.security.HttpSecurityPolicy` that should authorize access to the specified endpoints.HttpSecurityPolicy.
508
+
s| `@AuthorizationPolicy` | Specifies named `io.quarkus.vertx.http.runtime.security.HttpSecurityPolicy` that should authorize access to the specified Jakarta REST endpoints.
498
509
Named HttpSecurityPolicy can be used for general authorization checks as demonstrated by <<authorization-policy-example>>.
Copy file name to clipboardExpand all lines: _guides/security-customization.adoc
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -242,6 +242,8 @@ You can enforce the order by implementing a default `SecurityIdentityAugmentor#p
242
242
By default, the request context is not activated when augmenting the security identity, this means that if you want to use for example Hibernate
243
243
that mandates a request context, you will have a `jakarta.enterprise.context.ContextNotActiveException`.
244
244
245
+
IMPORTANT: Please also review the xref:security-proactive-authentication.adoc#cdi-request-context-activation[Activating the CDI request context] section of the "Proactive authentication" guide.
246
+
245
247
The solution is to activate the request context, the following example shows how to get the roles from an Hibernate with Panache `UserRoleEntity`.
By adding the `%prod.` profile prefix, you ensure that the data source properties are only observed by an application running in production mode.
336
+
334
337
====
335
338
+
336
339
. To initialize the database with users and roles, implement the `Startup` class, as outlined in the following code snippet:
@@ -384,7 +387,7 @@ In a production environment, do not store plain text passwords.
384
387
As a result, the `quarkus-security-jpa` defaults to using bcrypt-hashed passwords.
385
388
====
386
389
387
-
== Test your application by using Dev Services for PostgreSQL
390
+
== Test your application in dev mode by using Dev Services for PostgreSQL
388
391
389
392
Complete the integration testing of your application in JVM and native modes by using xref:dev-services.adoc#databases[Dev Services for PostgreSQL] before you run your application in production mode.
390
393
@@ -411,21 +414,8 @@ To run your application in dev mode:
411
414
include::{includes}/devtools/dev.adoc[]
412
415
413
416
414
-
The following properties configuration demonstrates how to enable PostgreSQL testing to run only in production (`prod`) mode.
415
417
In this scenario, `Dev Services for PostgreSQL` launches and configures a `PostgreSQL` test container.
If you add the `%prod.` profile prefix, data source properties are not visible to `Dev Services for PostgreSQL` and are only observed by an application running in production mode.
428
-
429
419
To write the integration test, use the following code sample:
430
420
431
421
[source,java]
@@ -503,7 +493,7 @@ While developing your application, you can add and run tests individually by usi
503
493
Dev Services for PostgreSQL supports testing while you develop by providing a separate PostgreSQL test container that does not conflict with the dev mode container.
504
494
====
505
495
506
-
== Test your application using Curl or browser
496
+
== Test your application in production mode by using Curl or browser
507
497
508
498
To test your application using Curl or the browser, you must first start a PostgreSQL server, then compile and run your application either in JVM or native mode.
509
499
@@ -513,7 +503,7 @@ To test your application using Curl or the browser, you must first start a Postg
513
503
----
514
504
docker run --rm=true --name security-getting-started -e POSTGRES_USER=quarkus \
Copy file name to clipboardExpand all lines: _guides/security-proactive-authentication.adoc
+15-1Lines changed: 15 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,6 +18,7 @@ Gain practical insights and strategies for various application scenarios.
18
18
Proactive authentication is enabled in Quarkus by default.
19
19
It ensures that all incoming requests with credentials are authenticated, even if the target page does not require authentication.
20
20
As a result, requests with invalid credentials are rejected, even if the target page is public.
21
+
Requests without credentials are not rejected, because anonymous requests are allowed.
21
22
22
23
You can turn off this default behavior if you want to authenticate only when the target page requires it.
23
24
To turn off proactive authentication so that authentication occurs only when the target page requires it, modify the `application.properties` configuration file as follows:
If you turn off proactive authentication, the authentication process runs only when an identity is requested.
31
32
An identity can be requested because of security rules that require the user to authenticate or because programmatic access to the current identity is required.
32
33
33
-
If proactive authentication is used, accessing `SecurityIdentity` is a blocking operation.
34
+
If proactive authentication is not used, accessing `SecurityIdentity` is a blocking operation.
34
35
This is because authentication might have yet to happen, and accessing `SecurityIdentity` might require calls to external systems, such as databases, that might block the operation.
35
36
For blocking applications, this is not an issue.
36
37
However, if you have disabled authentication in a reactive application, this fails because you cannot do blocking operations on the I/O thread.
@@ -96,6 +97,19 @@ public class HelloService {
96
97
}
97
98
----
98
99
100
+
[[cdi-request-context-activation]]
101
+
== Activating the CDI request context
102
+
103
+
You may need to inject `@RequestScoped` beans during authentication and authorization.
104
+
A good example of this is accessing a database during a `SecurityIdentity` augmentation,
105
+
which is described in the xref:security-customization.adoc#security-identity-customization[Security Identity Customization] section of the "Security Tips and Tricks" guide.
106
+
If authentication or authorization fails with the `jakarta.enterprise.context.ContextNotActiveException`, disabling proactive authentication is most often the best solution.
107
+
Users can also activate xref:cdi-reference.adoc#request-context-lifecycle[CDI request context], for example, by using the `@ActivateRequestContext` annotation.
108
+
However, some CDI beans may not be ready for use.
109
+
110
+
One exception to this solution is a situation when application endpoints are secured with the xref:security-authorize-web-endpoints-reference.adoc#authorization-using-configuration[Authorization using configuration].
111
+
For more information, see the xref:security-authorize-web-endpoints-reference.adoc#policy-active-cdi-request-context[Inject RequestScoped beans into HttpSecurityPolicy] section of the "Authorization of Web endpoints" guide for more information.
0 commit comments