Provide redirect support for UI auth, Qute template redirection #21376
Description
Description
I am using Qute to implement a web ui in the following way:
@Slf4j
@Path("/")
@Tags({@Tag(name = "UI")})
@RequestScoped
@Produces(MediaType.TEXT_HTML)
public class Index extends UiProvider {
@Inject
@Location("webui/pages/index")
Template index;
@Inject
@Location("webui/pages/overview")
Template overview;
@Inject
UserService userService;
@Inject
JsonWebToken jwt;
@GET
@PermitAll
@Produces(MediaType.TEXT_HTML)
public TemplateInstance index(
@Context SecurityContext securityContext
) {
logRequestContext(jwt, securityContext);
return index.instance();
}
@GET
@Path("overview")
@RolesAllowed("user")
@Produces(MediaType.TEXT_HTML)
public TemplateInstance overview(
@Context SecurityContext securityContext
) {
logRequestContext(jwt, securityContext);
return overview.instance();
}
}
I am getting jwt authentication through a cookie and restricting access to web pages via @RolesAllowed
. This works as intended, except for a user whose cookie or token expires and tries to go to /overview
, the page simply doesn't load at all. Ideally there would be a way to specify "if no auth, redirect to this URL", so the user would be able to login. I know I could rework how I am using the JWT's to implement by hand, but I feel like this shouldn't be necessary.
Additionally, on a similar note, it is hard to do redirects when using Qute templates. With a normal endpoint, returning a Response
, I could return the applicable response. When returning a straight TemplateBuilder
though, that makes it hard. I could probably throw a custom exception and handle it and provide the redirect there, but that feels annoying and might want an easier way to do it.
Implementation ideas
No-Auth Redirection idea:
@GET
@Path("overview")
@RolesAllowed("user")
@UnauthRedirect("/login") // suggested annotation, provides a url/ endpoint to go to to when not authrorized (no jwt/ no role/ expired, etc)
@Produces(MediaType.TEXT_HTML)
public TemplateInstance overview(
@Context SecurityContext securityContext
) {
logRequestContext(jwt, securityContext);
return overview.instance();
}
Redirect for Qute idea:
Create an exception that is automatically handled to redirect the user of the webui to a different endpoint
@GET
@Path("overview")
@RolesAllowed("user")
@Produces(MediaType.TEXT_HTML)
public TemplateInstance overview(
@Context SecurityContext securityContext,
String someParameter
) {
logRequestContext(jwt, securityContext);
if(someParameter.equals("BAD")){
throw new UiRedirect("/path/or/url");
}
return overview.instance();
}