在没有HTML登录页面的情况下在Vaadin中设置JDBC身份验证

时间:2015-06-30 20:55:07

标签: java authentication jdbc vaadin

我想在我的Vaadin应用程序上设置用户身份验证,但我不想使用HTML或JSP页面进行身份验证。我想使用Vaadin API编写代码来捕获用户名和密码。密码并使用更多Java代码将其提供给Glassfish。到目前为止,我已成功使用此(基本)代码从用户检索用户名和密码:

@Title("test")
@Theme("valo")
public class MyVaadinApplication extends UI {
    private String user;
    private String pwd;

    @WebServlet(value = "/*", asyncSupported = false)
    @VaadinServletConfiguration(productionMode = false, ui = MyVaadinApplication.class)
    public static class Servlet extends VaadinServlet {
    }

    @Override
    protected void init(VaadinRequest request) {
        FormLayout form = new FormLayout();
        setContent(form);

        TextField nameField = new TextField("Name:");
        PasswordField passField = new PasswordField("Password:");
        Button button = new Button("Login");

        form.addComponent(nameField);
        form.addComponent(passField);
        form.addComponent(button);

        button.addClickListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                user = nameField.getValue();
                pwd = passField.getValue();
                Notification.show("Name=" + user +  ", PWD=" + pwd, Notification.Type.TRAY_NOTIFICATION);
            }
        });

    }
}

现在我正在尝试使用用户/传递字符串登录,但我似乎无法找到这样做的方法。我找到了这个令人惊讶的文档,它完全概述了我的需求(在这里找到:https://blogs.oracle.com/bobby/entry/authentication_without_the_form),但不幸的是它已经过时了,代码已经不存在了。我试着搜索" getRequest()。login()"使用但无法找到任何支持它的Vaadin类的方法。有没有人曾经这样做过?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:2)

我假设您的登录操作会将用户重定向到其他页面/视图。

您始终可以使用会话的setAttribute功能

在视图之间传递属性
getUI().getSession().setAttribute("username", user);

然后,您可以在下一个视图中检索此属性,以便确定用户是否已登录。

getUI().getSession().getAttribute("username");

我不知道您在哪里存储帐户,但无论您使用什么(数据库,文本文件等),这种方式都应该有效。

我建议你研究VaadinSession,因为会话实际上存储了你需要的所有数据,直到它过期。

以下是一些可能有用的链接:

VaadinSession documentation

Setting and reading session attributes

尝试这些,如果需要,我可以提供更多帮助。

相关问题