简体   繁体   English

如何以编程方式验证 web 表单的登录凭据?

[英]How to programmatically verify login credentials for a web form?

I'm building an app to let users export data from a university system.我正在构建一个应用程序,让用户从大学系统中导出数据。 Currently, they can log in and see the data in HTML, but I would like to let people download it as CSV.目前,他们可以登录并查看 HTML 中的数据,但我想让人们将其下载为 CSV。

I have an app where users supply their username and password.我有一个应用程序,用户提供他们的用户名和密码。 I would like to log in to the university system and HTML scrape the resulting page.我想登录大学系统,然后 HTML 抓取结果页面。 How can I do this?我怎样才能做到这一点?

I'm building a GWT app.我正在构建一个 GWT 应用程序。 I could either do this in Java-transliterated-JS on the client, or Java on the server.我可以在客户端上的 Java-transliterated-JS 或服务器上的 Java 中执行此操作。

Update : Selenium might be nice, but it looks like overkill.更新: Selenium 可能不错,但看起来有点矫枉过正。

You're going to have to do this from the server unless the domains are the same.除非域相同,否则您将不得不从服务器执行此操作。 You'd need to determine what the POST transaction used by the other server for the login step looks like - parameter names etc. Then you'd perform that operation and do whatever you want with what comes back.您需要确定其他服务器用于登录步骤的 POST 事务是什么样的 - 参数名称等。然后您将执行该操作并对返回的内容做任何您想做的事情。 If you need to see multiple pages, you need to maintain the appropriate session cookie too so that the server knows you're still logged in on the subsequent HTTP requests.如果您需要查看多个页面,您还需要维护适当的 session cookie,以便服务器知道您仍然在后续的 HTTP 请求中登录。

If you have to hit another site to validate the credentials, then I'm not so sure that people should feel comfortable providing those credentials to you.如果您必须访问另一个站点来验证凭据,那么我不太确定人们是否应该愿意向您提供这些凭据。 That is, if you don't have rights to check the credentials directly, why are you trustworthy to receive them?也就是说,如果您无权直接检查凭据,为什么您值得信赖地接收它们? I know sometimes people need to integrate with a system they don't own, so this is just a question.我知道有时人们需要与他们不拥有的系统集成,所以这只是一个问题。

First, this has to be done server-side because of the limitations on client scripting due to the same origin policy .首先,由于同源策略对客户端脚本的限制,这必须在服务器端完成。

The typical way of handling the "screen scraping" you mention is to treat the web page as if it was an XML service.您提到的处理“屏幕抓取”的典型方法是将 web 页面视为 XML 服务。 First, examine the source code of the page, then using an internet/HTTP stack, craft a POST to the correct URL and read the response using a standard XML library.首先,检查页面的源代码,然后使用 Internet/HTTP 堆栈,将 POST 发送到正确的 URL 并使用标准 XML 库读取响应。 It will take some ingenuity to come up with a good way to dig into the XML to find the piece you need that will be as insulated as possible from changes to the page.要想出一个很好的方法来挖掘 XML 以找到您需要的部分,该部分将尽可能避免对页面的更改,这将需要一些独创性。 Keep in mind that your system can break any time that the owners of the site change their page.请记住,您的系统可能会在网站所有者更改其页面的任何时候中断。

Sometimes, you can't just send the POST but have to request the blank page initially in order to get hidden form values that need to be returned in the POST.有时,您不能只发送 POST,而是必须首先请求空白页,以便获取需要在 POST 中返回的隐藏表单值。 You'll have to experiment to find out what it requires.您必须进行试验以找出它需要什么。

Additionally, you probably have to handle cookies as well, since they usually are an integral part of the web site's authentication and session management (though you might get lucky that the session doesn't matter between the initial POST and the first response). Additionally, you probably have to handle cookies as well, since they usually are an integral part of the web site's authentication and session management (though you might get lucky that the session doesn't matter between the initial POST and the first response).

Last, you may be unlucky enough that the site uses javascript to do part of the authentication work, which may require additional digging to understand how the credentials are posted to the site.最后,您可能很不幸,该站点使用 javascript 来完成部分身份验证工作,这可能需要额外挖掘才能了解凭据是如何发布到站点的。

There are other potential barriers such as the site checking to see that the referrer is their own site, possible use of SSL (HTTPS) and so on.还有其他潜在的障碍,例如站点检查以查看引用者是他们自己的站点,可能使用 SSL (HTTPS) 等等。

I'm pretty sure that the protection against cross-site scripting in web browsers will mean that you can't log in to the university's app using javascript running in the web browser.我很确定 web 浏览器中的跨站点脚本保护意味着您无法使用在 web 浏览器中运行的 javascript 登录大学的应用程序。 So the part of your program that fetches data from the university will need to run on your server.因此,从大学获取数据的程序部分将需要在您的服务器上运行。 Once you have the data, you can process it either on your server or in javascript in the browser, but I think it would be easier to do it on the server.获得数据后,您可以在服务器上或浏览器中的 javascript 中处理它,但我认为在服务器上处理会更容易。

See http://en.wikipedia.org/wiki/Same_origin_policyhttp://en.wikipedia.org/wiki/Same_origin_policy

I'm not too sure about GWT, but in general, you would take the form data submitted by the user, check it against a database of username and hashed passwords.我不太确定 GWT,但一般来说,您会获取用户提交的表单数据,对照用户名和哈希密码的数据库进行检查。 If the database checks out, set a session cookie that says the user is logged in.如果数据库签出,则设置一个 session cookie,表明用户已登录。

In your pages, check if the session cookie say the user is logged in. If not, redirect to login page, otherwise allow them to view the pagfe.在您的页面中,检查 session cookie 是否表明用户已登录。如果没有,则重定向到登录页面,否则允许他们查看页面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何验证登录模块不同页面上的特定Web元素 - How to verify specific web element on different page in login module 在本地 Solaris 系统上验证 LDAP 登录凭据的简单方法是什么 - What is an easy way to verify LDAP login credentials on a local Solaris system 如何通过Web登录httpunit(无表单) - how to web login httpunit (with out form) 如果两者都需要在同一类的每个@Test中运行,则如何使用多个凭据验证登录功能[在Maven POM Selenium项目中] - How to verify login functionality with multiple credentials if Both need to run in every @Test in a same Class[In Maven POM Selenium project] 如何在liferay中获取登录凭据? - How to get login credentials in liferay? 如何使用 java 中的系统凭据通过 SAML 登录 web 应用程序 - How can I login into web application with SAML using system credentials in java 如何使用Web登录页面凭据对REST Web服务进行身份验证 - how to authenticate REST webservice get call using web login page credentials 在进行管理员登录的情况下,如何在Java servlet的web.xml中提供安全凭证,又如何从JSP管理员登录页面进行验证? - In case of admin login, how to give security credentials in web.xml of Java servlets and how could it be validated from JSP admin login page? 如何用JAVA以编程方式填写Web表单? - How can I programmatically fill out a web form with JAVA? 如何以编程方式验证使用jarsigner签名的jar - How to verify a jar signed with jarsigner programmatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM