java 在JAAS Security中创建一个可以传递用户名和密码的URLConnection
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/937412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a URLConnection that can pass username and password in JAAS Security
提问by monceaux
Hi I have something like the following that makes a URL Connection and retrieves the data. The problem is that when I hit a page that requires authentication first I get the login page. I'm unsure of how to pass a username and password in the URLConnection. I'm hitting a site that is using JAAS authenticaiton.
嗨,我有类似以下内容的内容,可以创建 URL 连接并检索数据。问题是,当我首先点击需要身份验证的页面时,我会得到登录页面。我不确定如何在 URLConnection 中传递用户名和密码。我正在访问一个使用 JAAS 身份验证的站点。
import java.net.URL;
import java.net.URLConnection;
.....
URL location = new URL("www.sampleURL.com");
URLConnection yc = location.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
data += inputLine + "\n";
in.close();
I tried something like this but it didn't seem to work...
我试过这样的事情,但它似乎没有用......
URL url = new URL("www.myURL.com");
URLConnection connection = url.openConnection();
String login = "username:password";
String encodedLogin = new BASE64Encoder().encodeBuffer(login.getBytes());
connection.setRequestProperty("Proxy-Authorization", "Basic " + encodedLogin);
回答by monceaux
Instead of "Proxy-Authorization", try "Authorization". The "Proxy-Authorization" header is used when authenticating against a proxy rather than the actual web server you are trying to access.
而不是“代理授权”,尝试“授权”。在针对代理而不是您尝试访问的实际 Web 服务器进行身份验证时,将使用“代理授权”标头。

