Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有URLConnection的Java基本授权_Java_Web Services_Authorization - Fatal编程技术网

带有URLConnection的Java基本授权

带有URLConnection的Java基本授权,java,web-services,authorization,Java,Web Services,Authorization,我正在使用URLConnection类打开与WebService的连接。我还为基本授权设置了请求属性,如下所示: c.setRequestProperty("Authorization", "Basic " + usernameAndPasswordEncoded); 其中c是URLConnection类型的对象。这是WebService调用的客户端。现在在服务器端,我需要从会话中获取用户名: User user = (User) request.getSession().getAttribu

我正在使用URLConnection类打开与WebService的连接。我还为基本授权设置了请求属性,如下所示:

c.setRequestProperty("Authorization", "Basic " + usernameAndPasswordEncoded);
其中c是URLConnection类型的对象。这是WebService调用的客户端。现在在服务器端,我需要从会话中获取用户名:

User user = (User) request.getSession().getAttribute("user");
但这不会得到用户名。另外,如果我查看调试模式,我会在HttpSession对象中看到一个匿名用户名。 如何解决这个问题,以便通过客户端将用户名发送到WebService服务器进行授权


谢谢大家

servlet规范为此提供了明确的抽象-您需要的是,或者在服务器端,您需要在web.xml中指定登录方法。比如说,

<login-config>
  <auth-method>BASIC</auth-method>
  <realm-name>My App</realm-name>
</login-config>

基本的
我的应用程序

一旦这样做,用户名应该可以使用
request.getRemoteUser()

在web.xml中“保护”这个url了吗?不,我没有。谢谢,我真的不知道我必须这么做。@zigomir:那么授权过程就不起作用了。查看是否可以在server log.Tnx中找到任何提示。我有,但我没有在web.xml文件中定义。