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
If an endpoint is annotated with `@RequestScoped`, or with a security annotation (such as `@RolesAllowed`), or depends directly or indirectly on a `@RequestScoped` bean, or on a bean annotated with a security annotation, then each WebSocket endpoint callback method execution is associated with a new _request context_.
189
-
The request context is active during endpoint callback invocation.
188
+
Each WebSocket endpoint callback method execution is associated with a new _request context_ (activate CDI request context) if the endpoint is:
189
+
190
+
* Annotated with the `@RequestScoped` annotation.
191
+
* Has a method annotated with a security annotation such as `@RolesAllowed`.
192
+
* Depends directly or indirectly on a `@RequestScoped` bean.
193
+
* Depends directly or indirectly on a CDI beans secured with a standard security annotation.
190
194
191
195
TIP: It is also possible to set the `quarkus.websockets-next.server.activate-request-context` config property to `always`. In this case, the request context is always activated when an endpoint callback is invoked.
192
196
@@ -783,6 +787,67 @@ class MyBean {
783
787
[[websocket-next-security]]
784
788
=== Security
785
789
790
+
Security capabilities are provided by the Quarkus Security extension.
791
+
Any xref:security-identity-providers.adoc[Identity provider] can be used to convert authentication credentials attached to a secure HTTP upgrade into a `SecurityIdentity` instance.
792
+
The `SecurityIdentity` is then associated with the websocket connection.
793
+
Authorization options are demonstrated in following sections.
794
+
795
+
NOTE: When OpenID Connect extension is used and token expires, Quarkus automatically closes connection.
796
+
797
+
[[secure-http-upgrade]]
798
+
==== Secure HTTP upgrade
799
+
800
+
An HTTP upgrade is secured when standard security annotation is placed on an endpoint class or an HTTP Security policy is defined.
801
+
The advantage of securing HTTP upgrade is less processing, the authorization is performed early and only once.
802
+
You should always prefer HTTP upgrade security unless, like in the example above, you need to perform an action on error or a security check based on the payload.
803
+
804
+
.Use standard security annotation to secure an HTTP upgrade
<1> Initial HTTP handshake ends with the 401 status for anonymous users.
836
+
You can also redirect the handshake request on authorization failure with the `quarkus.websockets-next.server.security.auth-failure-redirect-url` configuration property.
837
+
838
+
IMPORTANT: HTTP upgrade is only secured when a security annotation is declared on an endpoint class next to the `@WebSocket` annotation.
839
+
Placing a security annotation on an endpoint bean will not secure bean methods, only the HTTP upgrade.
840
+
You must always verify that your endpoint is secured as intended.
841
+
842
+
.Use HTTP Security policy to secure an HTTP upgrade
WebSocket endpoint callback methods can be secured with security annotations such as `io.quarkus.security.Authenticated`,
787
852
`jakarta.annotation.security.RolesAllowed` and other annotations listed in the xref:security-authorize-web-endpoints-reference.adoc#standard-security-annotations[Supported security annotations] documentation.
788
853
@@ -828,60 +893,108 @@ public class Endpoint {
828
893
<1> The echo callback method can only be invoked if the current security identity has an `admin` role.
829
894
<2> The error handler is invoked in case of the authorization failure.
830
895
831
-
`SecurityIdentity` is initially created during a secure HTTP upgrade and associated with the websocket connection.
896
+
==== Secure server endpoints with permission checkers
832
897
833
-
NOTE: When OpenID Connect extension is used and token expires, Quarkus automatically closes connection.
898
+
WebSocket endpoints can be secured with the xref:security-authorize-web-endpoints-reference.adoc#permission-checker[permission checkers].
899
+
We recommend to <<secure-http-upgrade>> rather than individual endpoint methods. For example:
834
900
835
-
=== Secure HTTP upgrade
901
+
.Example of a WebSocket endpoint with secured HTTP upgrade
902
+
[source, java]
903
+
----
904
+
package io.quarkus.websockets.next.test.security;
836
905
837
-
An HTTP upgrade is secured when standard security annotation is placed on an endpoint class or an HTTP Security policy is defined.
838
-
The advantage of securing HTTP upgrade is less processing, the authorization is performed early and only once.
839
-
You should always prefer HTTP upgrade security unless, like in th example above, you need to perform action on error.
906
+
import io.quarkus.security.PermissionsAllowed;
907
+
import io.quarkus.websockets.next.OnTextMessage;
908
+
import io.quarkus.websockets.next.WebSocket;
840
909
841
-
.Use standard security annotation to secure an HTTP upgrade
910
+
@PermissionsAllowed("product:premium")
911
+
@WebSocket(path = "/product/premium")
912
+
public class PremiumProductEndpoint {
913
+
914
+
@OnTextMessage
915
+
PremiumProduct getPremiumProduct(int productId) {
916
+
return new PremiumProduct(productId);
917
+
}
918
+
919
+
}
920
+
----
921
+
922
+
.Example of a permission checker authorizing the HTTP upgrade
return new Product(productId, "Product " + productId);
869
982
}
870
-
}
871
-
----
872
-
<1> Initial HTTP handshake ends with the 401 status for anonymous users.
873
-
You can also redirect the handshake request on authorization failure with the `quarkus.websockets-next.server.security.auth-failure-redirect-url` configuration property.
874
983
875
-
IMPORTANT: HTTP upgrade is only secured when a security annotation is declared on an endpoint class next to the `@WebSocket` annotation.
876
-
Placing a security annotation on an endpoint bean will not secure bean methods, only the HTTP upgrade.
877
-
You must always verify that your endpoint is secured as intended.
<1> The `getProduct` callback method can only be invoked if the current security identity has an `admin` role or the user is allowed to get the product detail.
997
+
<2> The error handler is invoked in case of the authorization failure.
Copy file name to clipboardExpand all lines: extensions/security/runtime/src/main/java/io/quarkus/security/runtime/QuarkusPermissionSecurityIdentityAugmentor.java
+29-17Lines changed: 29 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
1
packageio.quarkus.security.runtime;
2
2
3
3
importjava.security.Permission;
4
+
importjava.util.Map;
5
+
importjava.util.function.BiFunction;
4
6
importjava.util.function.Function;
5
7
importjava.util.function.Predicate;
6
8
@@ -33,33 +35,43 @@ public Throwable apply(Throwable throwable) {
Copy file name to clipboardExpand all lines: extensions/security/spi/src/main/java/io/quarkus/security/spi/PermissionsAllowedMetaAnnotationBuildItem.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -49,7 +49,7 @@ public List<AnnotationInstance> getTransitiveInstances() {
0 commit comments