Skip to content

Commit

Permalink
Improves documentation on SecurityIdentityAugmentor with Hibernate ex…
Browse files Browse the repository at this point in the history
…ample
  • Loading branch information
vanny96 committed Jan 23, 2023
1 parent e634fe0 commit 143c5c5
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions docs/src/main/asciidoc/security-customization.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -241,37 +241,63 @@ The solution is to activate the request context, the following example shows how
import io.quarkus.security.identity.AuthenticationRequestContext;
import io.quarkus.security.identity.SecurityIdentity;
import io.quarkus.security.identity.SecurityIdentityAugmentor;
import io.quarkus.security.runtime.QuarkusSecurityIdentity;
import io.smallrye.mutiny.Uni;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.control.ActivateRequestContext;
import java.util.function.Supplier;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
@ApplicationScoped
public class RolesAugmentor implements SecurityIdentityAugmentor {
@Inject
Instance<SecurityIdentitySupplier> identitySupplierInstance;
@Override
public Uni<SecurityIdentity> augment(SecurityIdentity identity, AuthenticationRequestContext context) {
if(identity.isAnonymous()) {
return Uni.createFrom().item(identity);
}
// Hibernate ORM is blocking
return context.runBlocking(build(identity));
// Hibernate ORM is
SecurityIdentitySupplier identitySupplier = identitySupplierInstance.get();
identitySupplier.setIdentity(identity);
return context.runBlocking(identitySupplier);
}
}
----
[source,java]
----
import io.quarkus.security.identity.SecurityIdentity;
import io.quarkus.security.runtime.QuarkusSecurityIdentity;
@ActivateRequestContext // Will activate the request context
Supplier<SecurityIdentity> build(SecurityIdentity identity) {
import javax.enterprise.context.Dependent;
import javax.enterprise.context.control.ActivateRequestContext;
import java.util.function.Supplier;
@Dependent
class SecurityIdentitySupplier implements Supplier<SecurityIdentity> {
private SecurityIdentity identity;
@Override
@ActivateRequestContext
public SecurityIdentity get() {
QuarkusSecurityIdentity.Builder builder = QuarkusSecurityIdentity.builder(identity);
String user = identity.getPrincipal().getName();
UserRoleEntity.<userRoleEntity>streamAll()
.filter(role -> user.equals(role.user))
.forEach(role -> builder.addRole(role.role));
return builder::build;
return builder.build();
}
public void setIdentity(SecurityIdentity identity) {
this.identity = identity;
}
}
----

[[jaxrs-security-context]]
Expand Down

0 comments on commit 143c5c5

Please sign in to comment.