The English version of is the official project site. Translated sites are community supported on a best-effort basis.

Testes de segurança

This document describes how to test Quarkus Security.

Configuring User Information

You can use quarkus-elytron-security-properties-file for testing security. This supports both embedding user info in and standalone properties files.

For example, the following configuration will allow for configuring the users in both the production where OAuth2 is required and development modes using Configuration Profiles.

# Configure embedded authentication,WRITER

# Configure OAuth2

Test Security Extension

Quarkus provides explicit support for testing with different users, and with the security subsystem disabled. To use this you must include the quarkus-test-security dependency:


This artifact provides the annotation, that can be applied to test methods and test classes to control the security context that the test is run with. This allows you to do two things, you can disable authorization so tests can access secured endpoints without needing to be authenticated, and you can specify the identity that you want the tests to run under.

A test that runs with authorization disabled can just set the enabled property to false:

@TestSecurity(authorizationEnabled = false)
void someTestMethod() {

This will disable all access checks, which allows the test to access secured endpoints without needing to authenticate.

You can also use this to configure the current user that the test will run as:

@TestSecurity(user = "testUser", roles = {"admin", "user"})
void someTestMethod() {

This will run the test with an identity with the given username and roles. Note that these can be combined, so you can disable authorization while also providing an identity to run the test under, which can be useful if the endpoint expects an identity to be present.

See OpenID Connect Bearer Token Integration testing, OpenID Connect Authorization Code Flow Integration testing and SmallRye JWT Integration testing for more details about testing the endpoint code which depends on the injected JsonWebToken.

Additionally, you can specify attributes for the identity, perhaps custom items that were added with identity augmentation:

SecurityIdentity identity;

@TestSecurity(user = "testUser", "roles = {"admin, "user"}, attributes = {
            @SecurityAttribute(key = "answer", value = "42", type = AttributeType.LONG) }
void someTestMethod() {
    Long answer = identity.<Long>getAttribute("answer");

This will run the test with an identity with an attribute of type Long named answer.

The feature is only available for @QuarkusTest and will not work on a @QuarkusIntegrationTest.

@TestSecurity can also be used in meta-annotations, for example like so:

    @Target({ ElementType.METHOD })
    @TestSecurity(user = "testUser", roles = {"admin", "user"})
    public @interface TestSecurityMetaAnnotation {


This is particularly useful if the same set of security settings needs to be used in multiple test methods.

Mixing security tests

If it becomes necessary to test security features using both @TestSecurity and Basic Auth (which is the fallback auth mechanism when none is defined), then Basic Auth needs to be enabled explicitly, for example by setting quarkus.http.auth.basic=true or %test.quarkus.http.auth.basic=true.

Path-based authentication

@TestSecurity can also be used when authentication mechanisms must be combined. Example below shows how to select authentication mechanism when path-based authentication is enabled.

@TestSecurity(user = "testUser", roles = {"admin", "user"}, authMechanism = "basic") (1)
void basicTestMethod() {

@TestSecurity(user = "testUser", roles = {"admin", "user"}, authMechanism = "form") (2)
void formTestMethod() {
1 The 'authMechanism' attribute selects Basic authentication.
2 The 'authMechanism' attribute selects Form-based authentication.

In your Quarkus application, it is possible to use annotations to select an authentication mechanism specific to each Jakarta REST endpoint:



public class TestSecurityResource {

    @BasicAuthentication (1)
    public String basicOnly() {
        return "basic-only";

    @FormAuthentication (2)
    public String formOnly() {
        return "form-only";
1 All HTTP requests to the /basic-only path from the basicTestMethod test are authenticated successfully.
2 Same HTTP requests will fail when invoked from the formTestMethod test as Basic authentication is required.

Alternatively, it is possible to select path-specific authentication mechanism with HTTP Security Policy:

# require basic authentication for the '/basic-only' path

# require form-based authentication for the '/form-only' path

Use Wiremock for Integration Testing

You can also use Wiremock to mock the authorization OAuth2 and OIDC services: See OAuth2 Integration testing, OpenID Connect Bearer Token Integration testing, OpenID Connect Authorization Code Flow Integration testing and SmallRye JWT Integration testing for more details.

Conteúdo Relacionado