Seeing a weird exception after a successful login #43719
-
Using the latest Quarkus on graalvm-jdk-21.0.4+8.1. Windows 11 23H2, 64-bit, with all the updates, if it matters. I'm working on a custom SecurityIdentityProvider. I'm trying to get it working with form authentication. Initially I'm just trying to get it working with a predefined username (bob) and password (bob). package com.example;
import io.quarkus.security.AuthenticationFailedException;
import io.quarkus.security.identity.AuthenticationRequestContext;
import io.quarkus.security.identity.IdentityProvider;
import io.quarkus.security.identity.SecurityIdentity;
import io.quarkus.security.identity.request.UsernamePasswordAuthenticationRequest;
import io.quarkus.security.runtime.QuarkusPrincipal;
import io.quarkus.security.runtime.QuarkusSecurityIdentity;
import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;
import java.util.Map;
@ApplicationScoped
public class ExampleIdentityProvider implements IdentityProvider<UsernamePasswordAuthenticationRequest> {
private static final Map<String, String> CREDENTIALS = Map.of("bob", "bob");
@Override
public Class<UsernamePasswordAuthenticationRequest> getRequestType() {
return UsernamePasswordAuthenticationRequest.class;
}
@Override
public Uni<SecurityIdentity> authenticate(UsernamePasswordAuthenticationRequest request,
AuthenticationRequestContext authenticationRequestContext) {
if (new String(request.getPassword().getPassword()).equals(CREDENTIALS.get(request.getUsername()))) {
return Uni.createFrom().item(QuarkusSecurityIdentity.builder()
.setPrincipal(new QuarkusPrincipal(request.getUsername()))
.addCredential(request.getPassword())
.setAnonymous(false)
.addRole("admin")
.build());
}
throw new AuthenticationFailedException("password invalid or user not found");
}
}
quarkus.package.jar.type=uber-jar
quarkus.quinoa.ui-root-path=app
quarkus.quinoa.enable-spa-routing=true
quarkus.http.auth.form.enabled=true
quarkus.http.auth.form.login-page=login
quarkus.http.auth.form.landing-page=success
quarkus.http.auth.form.error-page=error
quarkus.datasource.db-kind=mariadb
quarkus.datasource.username=db-username
quarkus.datasource.password=db-password
quarkus.datasource.jdbc.url=jdbc:mariadb://localhost/somedatabase Although I am using quinoa for part of the application, the login, success and error pages are standard Qute templates. I can log in successfully. But instead of seeing the "success" page, I get an exception
(many lines of stacktrace omitted)
@GET
@RolesAllowed("admin")
@Path("/restricted")
public String me(@Context SecurityContext securityContext) {
return securityContext.getUserPrincipal().getName();
} Any ideas? What am I doing wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 15 replies
-
Incidentally, I know the login is successful. I had a couple lines of code placed before the |
Beta Was this translation helpful? Give feedback.
-
@sberyozkin ^^^ |
Beta Was this translation helpful? Give feedback.
AFAIK you always need to implement both
IdentityProvider<UsernamePasswordAuthenticationRequest>
andIdentityProvider<TrustedAuthenticationRequest>
if using form-based authentication. At least that's whatquarkus-security-jpa
generates and I also used it successfully with my apps. TheIdentityProvider<TrustedAuthenticationRequest>
implementation is used to authenticate request from an encrypted cookie.In other words, if an
IdentityProvider<UsernamePasswordAuthenticationRequest>
succeeds the authentication information is stored in an encrypted cookie which is sent with a follow-up requests and for these requests theIdentityProvider<TrustedAuthenticationRequest>
is used to build aSecurity…