Vaadin 前端和 Token/Cookie

Vaadin Front-end and Token/Cookie

我在后端环境中使用nodeJS,大部分API都准备好了。我想在前端使用Vaading,因为Java Spreadsheet component。我查看了 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,如何在Javaweb中实现Cookie?)每次请求时

您使用哪个版本的 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);
    }
}

结果如下:

(按钮被点击了 3 次)

Update:同样可以在 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 {
    }
}