如何将jsessionid与基本身份验证结合使用

时间:2013-01-29 09:29:41

标签: session-cookies basic-authentication

我正在使用JBoss 7.1并使用基本身份验证保护我的Web应用程序但我只希望第一次调用需要Basic身份验证标头,后续调用应使用jsessionid进行身份验证。如何做到这一点?

到目前为止,我已经创建了一个rest servlet,通过调用request.getSession()来强制创建会话

@Path("/rest/HelloWorld")
public class HelloWorld {

    @GET()
    @Produces("text/plain")
    public String sayHello(@Context HttpServletResponse response,
        @Context HttpServletRequest request) {
        HttpSession session = request.getSession();
        return "Hello World! " + request.getUserPrincipal().getName();
    }

我的想法是,任何其他调用应该只需要jsessionid cookie,但在查看fiddler时,我看到第一个调用的行为符合预期。首先,您获得401并且客户端正在重新发送,包括基本授权标头,并返回jsessionid。在第二次调用时,包含了jsessionid cookie,但我仍然得到一个401,它触发客户端重新发送基本授权头。

这是成功通过身份验证的第一次调用返回的标头。

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Pragma: No-cache
Cache-Control: no-cache
Expires: Thu, 01 Jan 1970 01:00:00 CET
Set-Cookie: JSESSIONID=AFDFl2etiUNkn-mpM+DXr3KE; Path=/Test
Content-Type: text/plain
Content-Length: 18
Date: Tue, 29 Jan 2013 09:12:48 GMT

Hello World! test1

当我再次拨打电话时,包括jsessionid

GET /Test/index.html HTTP/1.1
Host: cwl-rickard:8080
Cookie: JSESSIONID=AFDFl2etiUNkn-mpM+DXr3KE

我正在获得401强制客户端重新发送请求,包括基本授权标题。

HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
Pragma: No-cache
Cache-Control: no-cache
Expires: Thu, 01 Jan 1970 01:00:00 CET
WWW-Authenticate: Basic realm="ApplicationRealm"
Content-Type: text/html;charset=utf-8
Content-Length: 958
Date: Tue, 29 Jan 2013 09:12:48 GMT

任何想法我都缺少。

0 个答案:

没有答案
相关问题