简体   繁体   English

无法弄清楚如何使用用户名/密码从 JSP 发送 HTTP POST 请求

[英]Can't figure out how to send HTTP POST request from a JSP- with username/password

I've been banging my head against this one all week and I'm finally one step away from a working POC.我整个星期都在用这个方法来解决这个问题,我终于离工作中的 POC 一步之遥了。 Any help with this is appreciated, and hope y'all are having a happy Friday regardless.对此的任何帮助表示赞赏,并希望你们都能度过一个愉快的星期五。

My goal is to create a JSP that will:我的目标是创建一个 JSP,它将:

-Send a predetermined, hardcoded username/password to a Guidewire Servlet via HTTP POST - 通过 HTTP POST 将预先确定的、硬编码的用户名/密码发送到 Guidewire Servlet

-At which point the servlet authenticates, logs the user in, and redirects them to the trainingApp login page - 此时 servlet 进行身份验证、登录用户并将其重定向到 trainingApp 登录页面

-Without any user input, just run it and you'll land on Guidewire already logged in. - 无需任何用户输入,只需运行它,您就会登陆已经登录的 Guidewire。

I've got it working!我已经开始工作了! However, it's not up to spec.但是,它不符合规范。 One of my requirements is that the JSP does all of this automatically on run.我的要求之一是 JSP 在运行时自动完成所有这些工作。 It's meant to send the same username/password combo every time, with no need for user input.它旨在每次发送相同的用户名/密码组合,无需用户输入。

I've only been able to get the JSP to work as a username/password form with a 'submit' button.我只能让 JSP 作为带有“提交”按钮的用户名/密码表单工作。 like this:像这样:

<body>

<%@page import="java.io.*" %>
<%@page import="java.net.*" %>

    <form method="post" action="http://localhost:8180/pc/service/test">
        User:<input name="username"/>
        Password:<input name="password"/>
        <input type="Submit" value="Log in"/>
    </form>

  </body> 
</html>

This sends the username/password, sure enough, but I need something more like this, and try as I might I can't get it to work.这确实发送了用户名/密码,当然,但我需要更多类似的东西,并尽可能尝试我无法让它工作。

<% 
URLConnection connection = new URL("http://localhost:8180/pc/service/test").openConnection();
connection.setDoOutput(true); // Triggers POST.
connection.setRequestProperty("Authorization", "Basic c3U6Z3c=");
%>

I can't seem to get the POST request to fire off and send the auth data without a "submit" button or a form to pass in the username and password.如果没有“提交”按钮或传递用户名和密码的表单,我似乎无法触发 POST 请求并发送身份验证数据。 The servlet seems to be working fine, and I'm relieved to be almost done. servlet 似乎工作正常,我对快要完成感到宽慰。

Thanks in advance if you can help!如果您能提供帮助,请提前致谢!

You can make use of the implicit object in JSP, ie request.您可以利用 JSP 中的隐式对象,即请求。

String name = request.getParameter("username");

Using the name, we can redirect to the form or to the specified URL.使用名称,我们可以重定向到表单或指定的 URL。

you don't need a form.你不需要表格。 just do a redirect and pass the authorization header.只需进行重定向并传递授权标头即可。

You can do a digest as well, which could be more secure if this url isn't https你也可以做一个摘要,如果这个 url 不是 https,这可能更安全

String headerKey = "Authorization"; String headerKey = "授权";

String headerValue = "Basic + Base64.encodeString(user+":"+pass); String headerValue = "Basic + Base64.encodeString(user+":"+pass);

response.setHeader(headerKey ,headerValue); response.setHeader(headerKey ,headerValue);

String redirectURL = " http://whatever.com/myJSPFile.jsp "; String redirectURL = " http://whatever.com/myJSPFile.jsp ";

response.sendRedirect(redirectURL); response.sendRedirect(redirectURL);

Solved it!解决了!

I'm still using the form, HOWEVER:我仍在使用该表格,但是:

the inputs are now hidden AND, here's the key: I added an onload tag to the body to submit the form on page load!输入现在隐藏了,这里是关键:我在正文中添加了一个 onload 标签,以便在页面加载时提交表单!

WORKING AS INTENDED.按预期工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM