使用SpringFramework RestTemplate的ALM 12 REST:";401“缺少会话cookie”;

使用SpringFramework RestTemplate的ALM 12 REST:";401“缺少会话cookie”;,spring,rest,resttemplate,alm,Spring,Rest,Resttemplate,Alm,在ALM 12中,我们必须明确地调用“qcbin/rest/site session”来获取会话。 当我接到电话“/qcbin/rest/site session”时,我收到以下信息: "Set-Cookie=[BIGipServerALMAST330P-QC=rd100o00000000000000000000ffff0fe0dd74o8080; path=/, ]"" 我提取cookie,如下所述: . 我们的项目使用的不是这个RestConnector,而是SpringFramewor

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

"Set-Cookie=[BIGipServerALMAST330P-QC=rd100o00000000000000000000ffff0fe0dd74o8080; path=/, ]""
我提取cookie,如下所述: . 我们的项目使用的不是这个RestConnector,而是SpringFramework中的RestTemplate,所以我做了:

        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;
        }
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);
    }
要使用RestTemplate在GET调用中发送cookie,我遵循了中的说明,因此我做到了:

        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;
        }
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);
    }
然而,我仍然得到“401会话cookie丢失”。
我已经尝试在GET调用中发送JSESSIONID,但也没有成功

谢谢你的帮助。
有什么线索吗?

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

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


希望这对您有所帮助

我不知道,它是否能帮助您,但您正在使用query param发送它以进行UI身份验证

"POST .../rest/site-session?login-form-required=y"
我建议在没有查询参数的情况下发布它

"POST .../rest/site-session"
此外,在请求QCSession令牌之前应执行的操作顺序为:

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

 "GET .../rest/is-authenticated"
2.如果没有,您将获得认证位置的参考:WWW-authenticate:LWSSO-realm=“…/authentication-point”

3.将基本身份验证头发送到身份验证点,最后添加alm authenticate。它返回您的LWSSO\u COOKIE\u密钥

"POST .../authentication-point/alm-authenticate"
Authentication: Basic BASE64{username:password}
4.然后您需要将LWSSO_COOKIE_密钥发布到站点会话,ALM将返回您的QCSession密钥

"POST .../rest/site-session"
"Cookie: LWSSO_COOKIE_KEY={cookie}; Path=/"
希望我能帮助你。
如果您仍然有问题,请随时与我联系。

我使用的是示例RestConnector,但POST to“/rest/site session”仅返回HTTP错误411。请求必须分块或具有内容长度。我试图发送一些数据,但这是错误的请求返回。。。