简体   繁体   中英

How to use a path parameter in wicket login page

I have a protected wicket page mounted with a path parameter, such as this:

class MyApp extends AuthenticatedWebApplication {
  @Override protected void init() {
    mountPage("/login/${site}", MyLoginPage.class);
    mountPage("/admin/${site}", MyAdminPage.class);
  }
  @Override protected Class<? extends WebPage> getSignInPageClass() {
    return MyLoginPage.class;
} }

@AuthorizeInstantiation("ADMIN")
class MyAdminPage extends WebPage { ... }

I'd like to keep this "site" parameter in the login page (I need this parameter in the login process). But apparently the default AuthenticatedWebApplication::onUnauthorizedInstantiation interceptor redirect to the login page without any parameters, so when accessing the admin page I get redirected to the login page with a NULL site...

How can I re-use the "site" parameter in the login page? I can eventually re-write my own AuthenticatedWebApplication to write a specific onUnauthorizedInstantiation interceptor, but there could be a simpler way to do that.

One requirement is to keep the login page bookmarkable, so the "site" parameter should be part of the URL (as a path parameter).

Override method restartResponseAtSignInPage to invoke your login page and make a parameter that is specific for your site. I give you an example with whole context path because I do not exactly know what you use to identify the site.

class MyApp extends AuthenticatedWebApplication {

    @Override protected void init() {
        super.init();
        mountPage("/login/${site}", MyLoginPage.class);
        mountPage("/admin/${site}", MyAdminPage.class);
    }

    @Override   
    public void restartResponseAtSignInPage() {
        // contextPath is just an example, implement here anything 
        // you use to identify the page or site
        String contextPath = RequestCycle.get().getRequest().getContextPath();
        throw new RestartResponseAtInterceptPageException(new MyLoginPage(new PageParameters().add("site", contextPath)));
    }
}

EDIT :

NOTE1: If you has to continue with any of parameters from the original request, see how to extract parameters from request cycle

IRequestParameters requestParameters = RequestCycle.get().getRequestParameters();

NOTE2: You can really override full functionality of the original AuthenticatedWebApplication that is a bit complicated because of final methods. Probably you have to modify the following code to exactly extract your site parameter that I do not know exactly.

class MyApp extends AuthenticatedWebApplication {

@Override 
protected void init() {
    super.init();
    mount("/login/${site}", MyLoginPage.class);
    mountPage("/admin/${site}", MyAdminPage.class);
    getSecuritySettings().setUnauthorizedComponentInstantiationListener(new IUnauthorizedComponentInstantiationListener() {

        @Override
        public void onUnauthorizedInstantiation(Component component) {
            MyApp.this.onMyUnauthorizedInstantiation(Component component);

        }
    });
}

public void onMyUnauthorizedInstantiation(final Component component) {
    if (component instanceof Page)
    {
        if (!AbstractAuthenticatedWebSession.get().isSignedIn())
        {
            // Redirect to intercept page to let the user sign in
            restartResponseAtSignInPage((Page)component);
        }
        else
        {
            onUnauthorizedPage((Page)component);
        }
    }
    else
    {
        // The component was not a page, so throw an exception
        throw new UnauthorizedInstantiationException(component.getClass());
    }
}


@Override   
public void restartResponseAtSignInPage(Page page) {
    // contextPath is just an example, implement here anything 
    // you use to identify the page or site
    throw new RestartResponseAtInterceptPageException(new MyLoginPage(new PageParameters().add("site", page.getPageParameters().get("site").toString("defaultValue"))));
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM