java - 使登录另一个站点 javaEE 的按钮

标签 java vaadin

我正在开发一个由 VAADIN 制作的平台。如果您想查看,请访问 www.wikire.it。 在此主页中,要登录,您必须单击“Accedi”,然后将文本字段编译为出现的模式表单。

该平台还有另一个作为后台的应用程序(与前面提到的不同)。

目的是在后台创建一个按钮,使用一些凭据登录网站(如何获取它并不重要)。

我是这个平台的新手,我有一些想法,但我不知道该怎么做(作为工作流程): - 休息服务 - 网站的 Web 服务

我怎样才能实现我的目标?

抱歉,这是一个通用问题,但我的工作需要它,但我不知道首先要做什么。

最佳答案

无需对现有网站进行太多更改即可实现此目的的一种方法是:

  1. 使用 Apache 的 HttpClient要启动的库,由某些用户操作(例如后台应用中的按钮或链接点击、带有必要参数的 (POST) 请求触发) (用户名、密码、最终隐藏字段)到您网站的登录地址(我相信您的情况是 http://www.wikire.it/logon )

  2. 成功登录后,站点将(可能)向您的 HttpClient 实例发送至少一个 cookie 用于身份验证 - 获取它:)(在我提供的示例中 - 见下文 - 我假设此 cookie 名为 JSESSION,通常情况下对于创建用户 session 的 Java 应用程序;如果您的网站使用不同的技术(例如 PHP 等)完成,请确保您了解该技术的 session /身份验证 cookie 的样子)

  3. 在您对已在后台(请记住:A.您从站点收到的身份验证 cookie 是为 HttpClient 实例设置/存在的,而不是为实际客户端设置/存在的,这是您的browser! 和 B. 在监听器中处理 Vaadin 事件,最终意味着将发送回您的浏览器的响应

  4. 要完成对用户点击的处理,请要求 Vaadin 页面执行 window.open('http://www.wikire.it/') JavaScript 调用(即传递您的目标站点的地址,也许还有“_blank”作为第二个参数,以强制在新窗口/选项卡中打开页面;这可能会被不允许打开弹出窗口的浏览器阻止,所以......小心)

应该可以了。请注意,登录站点完全独立于 Vaadin ——您只需找到一种方法来为浏览器设置 cookie 并使其执行最后一次 JavaScript 调用。


我在这里创建了 2 个示例项目:https://github.com/octavian-nita/so/tree/master/so-42927030-vaadin-login-to-site-buttonsite是一个非常基本的java/jsp应用程序,由登录页面(您可以用来登录的用户名是Johnny,密码无关紧要)保护,作为登录的目标站点。 backoffice是一个小型 Vaadin 应用程序,带有一个按钮,您可以单击该按钮来登录网站

为了您的方便,我在下面突出显示了相关的代码段。

HttpClient 上添加 Maven 依赖项

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.5.3</version>
</dependency>

创建登录站点并返回身份验证 Cookie 的例程

这意味着您知道用于登录操作的路径(通常是登录表单中指定的路径)。

private Cookie login(URI targetUri, String loginPath, Map<String, String> params) throws IOException {
    requireNonNull(targetUri);
    requireNonNull(loginPath);

    // Keep track of cookies we might receive in an HttpClient:
    final CookieStore cookies = new BasicCookieStore();

    // Build and work with an (AutoCloseable) HttpClient that uses the cookie store:
    try (CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(cookies).build()) {

        // Prepare (login) request parameters:
        List<NameValuePair> reqParams = new ArrayList<>();
        if (params != null) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                reqParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
        }

        // Execute the login (POST) request with the given parameters:
        HttpPost post = new HttpPost(targetUri + loginPath);
        post.setEntity(new UrlEncodedFormEntity(reqParams));

        CloseableHttpResponse response = client.execute(post);
        // Eventually, check the response to see if successful
        response.close();

        // Look for a JSESSIONID-named cookie stored in the HttpClient and return it to be used by calling code:
        for (org.apache.http.cookie.Cookie cookie : cookies.getCookies()) {
            if ("JSESSIONID".equalsIgnoreCase(cookie.getName())) {

                String domain = targetUri.getHost();
                if (domain.startsWith("www.")) {
                    domain = domain.substring(4);
                }

                Cookie authCookie = new Cookie(cookie.getName(), cookie.getValue());
                authCookie.setDomain(domain);
                authCookie.setPath("/");
                // Eventually, set expiry (to allow longer login) and other things...

                return authCookie;
            }
        }

        return null; // some sort of error?
    }
}

在浏览器中设置身份验证 Cookie 并在 Vaadin 中打开网站

@Title("Backoffice for SO Question #42927030")
public class MainUI extends UI {

    private Cookie login(URI targetUri, String loginPath, Map<String, String> params) throws IOException {
        // ...
    }

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        setContent(new VerticalLayout(new Button("Log into site...", event -> {

            try {
                URI targetUri = new URI("http://localhost:8080");

                Map<String, String> params = new HashMap<>();
                params.put("username", "Johnny");
                params.put("password", "incorrect :)");
                // Eventual hidden fields, etc.
                // params.put("...", "...");

                Cookie targetAuthCookie = login(targetUri, "/log-me-in", params);

                // We're not ready just yet: we still need to 'transfer' the cookie
                // the HTTP client received to the current browser:
                VaadinService.getCurrentResponse().addCookie(targetAuthCookie);

                // Upon responding to the Vaadin 'click' request, open the target URL (eventually in a new page / tab):
                Page.getCurrent().getJavaScript().execute("window.open('" + targetUri + "');");

            } catch (Exception ex) {
                ex.printStackTrace();
            }

        })));
    }

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

让我们知道您的情况如何...

关于java - 使登录另一个站点 javaEE 的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42927030/

相关文章:

java - RSA 加密(模数/指数)给出 "Message is larger than modulus"

java - 向现有的 java POJO 添加一个新的成员变量

java - org/apache/poi/ss/usermodel/Sheet 的 NoClassDefFoundError Jar 错误

css - 使用更多 CSS 样式化或删除已使用 CSS 添加的元素

javascript - 如何在 Swing-GUI 和 Web 应用程序(如 Vaadin 或 native JS)之间同步倒计时

java - Ignite 扫描查询抛出类 org.apache.ignite.binary.BinaryInvalidTypeException

java - 在 Java 中,为什么要将括号放在数组名称的前面?

java - Vaadin 7 迁移到 Vaadin 8 后出现小部件集错误

java - 垂直布局 Vaadin 中的按钮不可点击?

java - 为什么 textchangelistener 很慢?