You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache Wicket 9 authentication-2示例中WicketExamplePage找不到问题求助

解决Apache Wicket认证示例中WicketExamplePage无法找到的问题

一、为什么找不到WicketExamplePage?

WicketExamplePage是wicket-examples包中的示例专用类,不属于Wicket核心API,且该类仅存在于示例项目的源码中,编译后的依赖jar包并不包含它——Maven能搜到包不代表jar里有可引用的类文件,很多示例包只提供可运行的war包,而非可依赖的类资源。

二、不依赖WicketExamplePage实现认证功能的方案

你可以完全用自定义页面替代WicketExamplePage,核心是实现Wicket的认证机制,步骤如下:

1. 自定义首页(替代WicketExamplePage)

创建普通Wicket页面作为登录后的跳转目标,比如HomePage:

public class HomePage extends WebPage {
    public HomePage() {
        add(new Label("welcome", "登录成功!欢迎访问系统首页"));
    }
}

2. 实现认证核心逻辑

参考authentication-2示例的流程,替换页面引用即可:

  • 自定义AuthenticatedWebSession处理登录验证:
public class CustomAuthenticatedSession extends AuthenticatedWebSession {
    public CustomAuthenticatedSession(Request request) {
        super(request);
    }

    @Override
    protected boolean authenticate(String username, String password) {
        // 替换为你的实际验证逻辑,比如查询数据库校验
        return "admin".equals(username) && "admin123".equals(password);
    }

    @Override
    public Roles getRoles() {
        if (isSignedIn()) {
            return new Roles(Roles.ADMIN);
        }
        return null;
    }
}
  • 配置AuthenticatedWebApplication,指定登录页和首页:
public class WicketAuthApplication extends AuthenticatedWebApplication {
    @Override
    public Class<? extends WebPage> getHomePage() {
        return HomePage.class; // 替换原示例的WicketExamplePage
    }

    @Override
    protected Class<? extends WebPage> getSignInPageClass() {
        return SignInPage.class; // 自定义登录页
    }

    @Override
    public Session newSession(Request request, Response response) {
        return new CustomAuthenticatedSession(request);
    }
}

3. 自定义登录页SignInPage

创建包含用户名、密码输入框的登录页面,提交时调用session登录方法:

public class SignInPage extends WebPage {
    public SignInPage() {
        Form<Void> signInForm = new Form<>("signInForm") {
            @Override
            protected void onSubmit() {
                String username = get("username").getInput();
                String password = get("password").getInput();
                CustomAuthenticatedSession session = CustomAuthenticatedSession.get();
                
                if (session.signIn(username, password)) {
                    continueToOriginalDestination();
                    setResponsePage(getApplication().getHomePage());
                } else {
                    error("用户名或密码错误");
                }
            }
        };
        
        signInForm.add(new TextField<String>("username").setRequired(true));
        signInForm.add(new PasswordTextField("password").setRequired(true));
        add(signInForm);
    }
}

三、依赖注意事项

如果不需要运行官方示例,建议移除wicket-examples依赖,仅保留Wicket核心依赖即可:

<dependency>
    <groupId>org.apache.wicket</groupId>
    <artifactId>wicket-core</artifactId>
    <version>9.x.x</version> <!-- 使用你项目对应的版本号 -->
</dependency>

内容的提问来源于stack exchange,提问作者James Pollard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 20:50:30