开发者

Java basic authorization with URLConnection

I'm opening a connection to WebService with an URLConnection class. I also set request property for basic authorization like this:

c.setRequestProperty("Authorization", "Basic " + usern开发者_运维技巧ameAndPasswordEncoded);

Where c is an object of type URLConnection. So this is client side of WebService call. Now on server side I need to get username from session:

User user = (User) request.getSession().getAttribute("user");

But this won't get an username. Also if I look through debug mode, I see an anonymous userName in HttpSession object. What to do to solve this problem, so that username is sent through client to WebService server for authorization?

Thanks everyone!


On the server end, you need to specify the login method in web.xml. For example,

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

Once you do that, the username should be available using request.getRemoteUser().


The servlet specification provides explicit abstractions for that - what you need is request.getRemoteUser()., or perhaps request.getUserPrincipal()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜