【问题标题】:Vaadin Front-end and Token/CookieVaadin 前端和令牌/Cookie
【发布时间】:2019-03-04 07:44:54
【问题描述】:

我在后端环境中使用 nodeJS,并且大多数 API 都准备好了。由于Java Spreadsheet component,我想在前端使用 Vaading。我查看了 Bakery 示例并设计了一个登录页面,该页面将用户凭据发送到我的后端 API。这是向服务器发送 POST 请求的代码。

public void submit(String username, String password) throws IOException {
        JSONObject json = new JSONObject();
        json.put("username", username);
        json.put("password", password);


        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        try {
            HttpPost request = new HttpPost(URL);
            HttpResponse response;

            StringEntity params = new StringEntity(json.toString());
            request.addHeader("content-type", "application/json");
            request.setEntity(params);
            response = httpClient.execute(request);
            if(response.getStatusLine().getStatusCode() == 200) {
                System.out.println("Okey!");
                // The response contains token. How can I store this token?
                // Also, How can I use the stored token for future Authentication:Bearer?
            }
        }
        catch(Exception ex) {
            System.out.println(ex);
        }
        finally {
            httpClient.close();
        }
    }

我想将响应的令牌存储在某个地方(我可以将它存储在 React 中的 cookie 对象中。但是,我不熟悉这种语言)并从这个存储中获取令牌(可能是 cookie,如何在Java web中实现Cookie?)每次我发出请求时。

【问题讨论】:

  • 仍然是一个有效的问题

标签: java cookies token vaadin


【解决方案1】:

您使用哪个版本的 Vaadin?如果您使用 Vaadin >= 10:如何使用会话?

您可以将令牌保存在 VaadinSession 中,并在每次请求时从那里读取它。

我创建了一个最小示例,在该示例中(单击按钮时)我检查令牌是否已保存在会话中。如果是,则打印在通知中。如果不是,则将其保存到会话中。代码如下:

@Route("")
public class MainView extends VerticalLayout {

    public MainView() {
        Button button = new Button("Click me", event -> {
            Object token = VaadinSession.getCurrent().getAttribute("token");
            if (token == null) {
                Notification.show("No token found in session. Now storing token = 123456");
                VaadinSession.getCurrent().setAttribute("token", "123456");
            } else {
                Notification.show("Found token in session: " + token.toString());
            }
        });
        add(button);
    }
}

结果如下:

(按钮被点击了三下)

更新:同样可以在 Vaadin 8 中使用,其中最小示例的代码如下所示:

@Theme("mytheme")
public class MyUI extends UI {

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        final VerticalLayout layout = new VerticalLayout();

        Button button = new Button("Click me", event -> {
            Object token = VaadinSession.getCurrent().getAttribute("token");
            if (token == null) {
                Notification.show("No token found in session. Now storing token = 123456");
                VaadinSession.getCurrent().setAttribute("token", "123456");
            } else {
                Notification.show("Found token in session: " + token.toString());
            }
        });

        layout.addComponents(button);

        setContent(layout);
    }

    @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
    public static class MyUIServlet extends VaadinServlet {
    }
}

【讨论】:

  • 谢谢。我正在使用 Vaadin8 或其他东西。我将其升级到 10.0.11。您的代码正在运行
  • 抱歉,当您说您主要使用 Vaadin 来支持电子表格组件时提到 Vaadin 10(它与 Vaadin 10+ 不兼容 - 我以前不知道)。好消息是所描述的代码也适用于 Vaadin 8(VaadinSession.getCurrent().getAttribute("token");VaadinSession.getCurrent().setAttribute("token", "123456");)。我会在这里更新我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-26
  • 2019-12-05
  • 2012-08-30
  • 2020-03-23
  • 2021-10-07
  • 1970-01-01
  • 2019-01-29
相关资源
最近更新 更多