Authentication and authorization
|
The sample code snippets shown in this section come from the Client and server integration test and OIDC integration test in the source tree of Quarkus CXF. You may want to use them as runnable examples. |
Client HTTP basic authentication
Use the following client configuration options provided by
quarkus-cxf extension
to pass the username and password for HTTP basic authentication:
Here is an example:
quarkus.cxf.client.basicAuth.wsdl = http://localhost:${quarkus.http.test-port}/soap/basicAuth?wsdl
quarkus.cxf.client.basicAuth.client-endpoint-url = http://localhost:${quarkus.http.test-port}/soap/basicAuth
quarkus.cxf.client.basicAuth.auth.username = bob
quarkus.cxf.client.basicAuth.auth.password = bob234
Accessing WSDL protected by basic authentication
By default, the clients created by Quarkus CXF do not send the Authorization header,
unless you set the quarkus.cxf.client."client-name".secure-wsdl-access to true:
quarkus.cxf.client.basicAuthSecureWsdl.wsdl = http://localhost:${quarkus.http.test-port}/soap/basicAuth?wsdl
quarkus.cxf.client.basicAuthSecureWsdl.client-endpoint-url = http://localhost:${quarkus.http.test-port}/soap/basicAuthSecureWsdl
quarkus.cxf.client.basicAuthSecureWsdl.auth.username = bob
quarkus.cxf.client.basicAuthSecureWsdl.auth.password = ${client-server.bob.password}
quarkus.cxf.client.basicAuthSecureWsdl.secure-wsdl-access = true
Mutual TLS (mTLS) authentication
See the Mutual TLS (mTLS) authentication section in SSL, TLS and HTTPS guide.
Securing service endpoints
The server-side authentication and authorization is driven by Quarkus Security, especially when it comes to
HTTP basic authentication and role-based access control
There is a basic example in our Client and server integration test. Its key parts are:
-
io.quarkus:quarkus-elytron-security-properties-filedependency as an Identity provider -
Basic authentication enabled and users with their roles configured in
application.properties:application.propertiesquarkus.http.auth.basic = true quarkus.security.users.embedded.enabled = true quarkus.security.users.embedded.plain-text = true quarkus.security.users.embedded.users.alice = alice123 quarkus.security.users.embedded.roles.alice = admin quarkus.security.users.embedded.users.bob = bob234 quarkus.security.users.embedded.roles.bob = app-user -
Role-based access control enfoced via
@RolesAllowedannotation:
package io.quarkiverse.cxf.it.auth.basic;
import jakarta.annotation.security.RolesAllowed;
import jakarta.jws.WebService;
import io.quarkiverse.cxf.it.HelloService;
@WebService(serviceName = "HelloService", targetNamespace = HelloService.NS)
@RolesAllowed("app-user")
public class BasicAuthHelloServiceImpl implements HelloService {
@Override
public String hello(String person) {
return "Hello " + person + "!";
}
}
Securing services with OpenID Connect (OIDC)
Add the io.quarkus:quarkus-oidc dependency to authenticate SOAP requests using OpenID Connect (OIDC) bearer tokens.
The OIDC integration test
configures a service application and publishes a SOAP endpoint at /services/hello:
quarkus.oidc.application-type = service
quarkus.oidc.client-id = quarkus-cxf-oidc
quarkus.oidc.credentials.secret = secret
# Require Keycloak Dev Services for these tests.
quarkus.oidc.devservices.enabled = false
quarkus.keycloak.devservices.users.alice = alice
quarkus.keycloak.devservices.roles.alice = italian
quarkus.keycloak.devservices.users.bob = bob
quarkus.keycloak.devservices.roles.bob = user
quarkus.cxf.endpoint."/hello".implementor = io.quarkiverse.cxf.it.auth.oidc.HelloServiceImpl
The example uses Keycloak Dev Services, which requires Docker or Podman.
It creates alice with the italian role and bob with the user role.
For a production service, set quarkus.oidc.auth-server-url to your provider’s realm URL
and configure your own client ID and credentials.
Apply security annotations to the service implementation methods:
package io.quarkiverse.cxf.it.auth.oidc;
import jakarta.annotation.security.PermitAll;
import jakarta.annotation.security.RolesAllowed;
import jakarta.jws.WebService;
import io.quarkus.security.Authenticated;
@WebService(serviceName = "HelloService", targetNamespace = HelloService.NS)
public class HelloServiceImpl implements HelloService {
@Override
public String hello(String name) {
return "Hello " + name + "!";
}
@Override
@PermitAll
public String hallo(String name) {
return "Hallo " + name + "!";
}
@Override
@RolesAllowed("italian")
public String ciao(String name) {
return "Ciao " + name + "!";
}
@Override
@Authenticated
public String hoi(String name) {
return "Hoi " + name + "!";
}
}
-
hello: no security annotation; allows anonymous access in this configuration. -
hallo:@PermitAllallows anonymous access. -
ciao:@RolesAllowed("italian")requires an authenticated user with theitalianrole. -
hoi:@Authenticatedrequires authentication, regardless of the user’s roles.
Clients send access tokens in the HTTP Authorization: Bearer <token> header.
Calls requiring authentication return HTTP 401 when no valid token is supplied;
authenticated callers lacking the required role receive HTTP 403.