从servlet java HttpURLConnection为每个用户设置cookie

从servlet java HttpURLConnection为每个用户设置cookie,java,servlets,cookies,http-headers,Java,Servlets,Cookies,Http Headers,我有一个API servlet java应用程序,允许用户通过其用户和密码查看外部页面,视图URL需要在调用之前设置cookies 我使用此代码查看页面 CookieManager cookieManager = new CookieManager(); CookieHandler.setDefault(cookieManager); String request_url = "http://url/view.js

我有一个API servlet java应用程序,允许用户通过其用户和密码查看外部页面,视图URL需要在调用之前设置cookies

我使用此代码查看页面

            CookieManager cookieManager = new CookieManager();
            CookieHandler.setDefault(cookieManager);
            String request_url = "http://url/view.jsp?id"+ id;
            URL url = new URL(request_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setInstanceFollowRedirects(false);
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setConnectTimeout(20000);
            httpURLConnection.setReadTimeout(20000);
            httpURLConnection.connect();

            try (BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()))) {
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    data = data + inputLine;
                    helper.status = 0;
                    helper.errorDesc = "0";
                }

                in.close();
            }

            httpURLConnection.disconnect();
如果它没有返回访问数据,我将使用此代码使用用户凭据登录

            CookieManager cookieManager = new CookieManager();
            CookieHandler.setDefault(cookieManager);
            String urlParameters = "username=" + username + "&password=" + password + "&displayLangCode=" + lang  + "&langCode=" + lang;
            System.out.println("urlParameters" + urlParameters);
            byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
            int postDataLength = postData.length;
            String activeUrl = "http://url/login.jsp";
            URL url = new URL(activeUrl);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpURLConnection.setRequestProperty("X-Service", "AuthenticateUser");
            httpURLConnection.setRequestProperty("Content-Length", Integer.toString(postDataLength));
            httpURLConnection.setInstanceFollowRedirects(false);
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setConnectTimeout(20000);
            httpURLConnection.setReadTimeout(20000);
            httpURLConnection.connect();
            try (DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream())) {
                wr.write(postData);
                wr.close();
            }
            httpURLConnection.disconnect();
它工作得很好,我可以得到内容

问题是,如果另一个用户打电话查看内容,他可以作为第一个登录的用户查看内容

这意味着Cookie是按API级别设置的,而不是按用户级别设置的


我需要帮助,因为我不知道如何解决此Cookie问题以设置每个用户呼叫的Cookie

我可以通过禁用自动Cookie来解决此问题

CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_NONE);
然后在登录方法中,我提取cookies并从函数返回它

List<String> cookies = httpURLConnection.getHeaderFields().get("Set-Cookie");
String token = null;
if (cookies != null) {
 for (String cookie : cookies) {
  token = cookie.split(";", 1)[0];
 }
}
return token
httpURLConnection.addRequestProperty("Cookie", token);