使用SpringFramework RestTemplate的ALM 12 REST:“401 QCSession cookie丢失”

时间:2015-08-21 18:02:46

标签: spring rest resttemplate alm

在ALM 12中,我们必须明确地调用“qcbin / rest / site-session”来获取会话。 当我GET调用“/ qcbin / rest / site-session”时,我会收到以下内容:

"Set-Cookie=[BIGipServerALMAST330P-QC=rd100o00000000000000000000ffff0fe0dd74o8080; path=/, ]""

我按照此处的描述提取cookie: HP ALM 12 REST not returning QCSession cookie。 我们的项目使用的是SpringFramework中的RestTemplate,而不是这个RestConnector,所以我做了:

        private HashMap getQCSession() throws Exception {
            URL url = new URL("https://almxxxx.saas.hp.com/qcbin/rest/site-session?login-form-required=y");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/xml");
            conn.setRequestProperty("Accept", "application/xml");
            conn.connect();

            HashMap cookies = updateCookies(conn);
            return cookies;
        }


        public HashMap updateCookies(HttpURLConnection conn) {
            String cookie2 = conn.getHeaderField("Set-Cookie");             
            int equalIndex = cookie2.indexOf('=');
            int semicolonIndex = cookie2.indexOf(';');
            String cookieKey = cookie2.substring(0, equalIndex);
            String cookieValue = cookie2.substring(equalIndex + 1, semicolonIndex);

            HashMap cookies = new HashMap();
            cookies.put(cookieKey, cookieValue);
            System.out.println(cookies.toString());
            return cookies;
        }

使用RestTemplate在GET调用中发送cookie我遵循http://springinpractice.com/2012/04/08/sending-cookies-with-resttemplate/的说明,所以我做了:

public <U extends Object> ResponseEntity<U> getFromURL(URI url, Class<U> responseBodyClass) throws Exception {
        logger.info("GET na URL: {} esperando classe: {} no response", url, responseBodyClass);

        HashMap cookie = this.getQCSession();
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Cookie", this.getQCSession().toString());
        this.httpEntity = new HttpEntity(null, requestHeaders);

        return super.exchange(url, HttpMethod.GET, this.httpEntity, responseBodyClass);
    }

添加到HttpEntity(SpringFramework)的requestHeaders内容是:

{Cookie=[{BIGipServerALMAST330P-QC=rd100o00000000000000000000ffff0fe0dd74o8080}]}

但是我仍然得到“401 QCSession cookie丢失”。
我已经尝试在GET中调用JSESSIONID,但也没有成功。

我感谢任何帮助。 任何线索?

2 个答案:

答案 0 :(得分:1)

我碰到了这个。从ALM12开始,您还需要创建一个会话。

我将一些XML或JSON发送到这里“/ authentication-point / alm-authenticate”进行身份验证 然后收集Set-Cookie标头 然后我使用前一个响应中的cookie POST到“/ rest / site-session” 我从该响应中收集会话cookie,以便在我的后续请求中使用。

希望有所帮助

答案 1 :(得分:1)

我不知道,它是否可以帮到你,但你发送的是查询参数,用于UI身份验证。

date -d "$var" +"%s"
date: invalid date ‘wto, 1 mar 2016, 16:00:14 CET’

我建议在没有查询参数的情况下发布它

"POST .../rest/site-session?login-form-required=y"

在请求QCSession令牌之前,您应该执行的操作顺序是:

1.检查您是否经过身份验证

"POST .../rest/site-session"

2.如果不是,您将获得验证身份的参考: WWW-Authenticate:LWSSO realm =&#34; ... / authentication-point&#34;

3.将基本身份验证标头发送到身份验证点,并在末尾添加 alm-authenticate 。返回LWSSO_COOKIE_KEY。

 "GET .../rest/is-authenticated"

4.然后,您需要将LWSSO_COOKIE_KEY发送到站点会话,ALM将返回QCSession密钥。

"POST .../authentication-point/alm-authenticate"
Authentication: Basic BASE64{username:password}

希望我能帮到你。 如果您仍有问题,请随时与我联系。