为什么 cookie 在后续请求中没有加载?

huangapple go评论57阅读模式
英文:

why cookie is not loaded for subsequent requests?

问题

我正在使用 okhttp 4.9.0

private OkHttpClient http;

public SonarApi(String serverUrl, String secret) {
    HttpLoggingInterceptor it = new HttpLoggingInterceptor();
    it.setLevel(Level.BODY);
    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    JavaNetCookieJar cookieJar = new JavaNetCookieJar(cookieManager);
    http = new OkHttpClient.Builder().addInterceptor(it).cookieJar(cookieJar).build();
}

public void login(String userName, String password) throws IOException {
    RequestBody formBody = new FormBody.Builder().add("login", userName).add("password", password).build();
    Request req = new Request.Builder()
            .addHeader("Accept", "application/json")
            .addHeader("Host", "x.x.x.x:9876")
            .addHeader("Origin", serverUrl)
            .addHeader("Content-Type", "application/x-www-form-urlencoded")
            .url(serverUrl + "/api/authentication/login").post(formBody).build();
    http.newCall(req).execute();
}

public boolean updateRuleMarkdownNote(String ruleKey, String mdNote) throws IOException {
    RequestBody formBody = new FormBody.Builder().add("key", ruleKey).add("markdown_note", mdNote).build();
    Request req = new Request.Builder().addHeader("Content-Type", "application/x-www-form-urlencoded").url(serverUrl + "/api/rules/update").post(formBody).build();
    int code = http.newCall(req).execute().code();
    return code >= 200 && code < 300;
}

public static void main(String[] args) {
    SonarApi sonarApi = new SonarApi("http://x.x.x.x:9876", "cac3b3c65347e87cdf1f9a7352935db79e2435f5");
    try {
        sonarApi.login("admin", "******");
        sonarApi.updateRuleMarkdownNote("custom-rules-java:ExampleRules01", "*foo*");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

日志:

六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: --&gt; POST http://172.25.160.238:9876/api/authentication/login
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Content-Length: 34
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Accept: application/json
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Host: 172.25.160.238:9876
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Origin: http://172.25.160.238:9876
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Content-Type: application/x-www-form-urlencoded
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: 
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: login=admin&amp;password=%23Fgglgy0223
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: --&gt; END POST (34-byte body)
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: &lt;-- 200 http://172.25.160.238:9876/api/authentication/login (266ms)
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: X-Frame-Options: SAMEORIGIN
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: X-XSS-Protection: 1; mode=block
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: X-Content-Type-Options: nosniff
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Set-Cookie: XSRF-TOKEN=5iohli0jdhtla6tk8a87598log; Max-Age=259200; Expires=Mon, 19-Jun-2023 06:44:23 GMT; Path=/
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Set-Cookie: JWT-SESSION=eyJhbGciOiJIUzI1NiJ9.eyJsYXN0UmVmcmVzaFRpbWUiOjE2ODY4OTc4NjMyNTMsInhzcmZUb2tlbiI6IjVpb2hsaTBqZgh0bGE2dGs4YTg3NTk4bG9nIiwianRpIjoiQVlqQzhxcFNkRWtsQ2VJdXhIQ0IiLCJzdWIiOiJBWHd3MUhN5ncNCN0xfa1UtMEhydyIsImlhdCI6MTY4Njg5Nzg2MywiZXhwIjoxNjg3MTU3MDYzfQ.sDbWX2_pNGIAIMjrgOBSyEnsBLc9fncz2-7XS_uyx-M; Max-Age=259200; Expires=Mon, 19-Jun-2023 06:44:23 GMT; Path=/; HttpOnly
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Content-Length: 0
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Date: Fri, 16 Jun 2023 06:44:23 GMT
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Keep-Alive: timeout=60
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Connection: keep-alive
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: &lt;-- END HTTP (0-byte body)
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: --&gt; POST http://172.25.160.238:9876/api/rules/update
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Content-Length: 58
六月 16, 2023 2:
<details>
<summary>英文:</summary>
I am using okhttp 4.9.0
```java
private OkHttpClient http;
public SonarApi(String serverUrl, String secret) {
HttpLoggingInterceptor it = new HttpLoggingInterceptor();
it.setLevel(Level.BODY);
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
JavaNetCookieJar cookieJar = new JavaNetCookieJar(cookieManager);
http = new OkHttpClient.Builder().addInterceptor(it).cookieJar(cookieJar).build();
}
public void login(String userName, String password) throws IOException {
RequestBody formBody = new FormBody.Builder().add(&quot;login&quot;, userName).add(&quot;password&quot;, password).build();
Request req = new Request.Builder()
.addHeader(&quot;Accept&quot;, &quot;application/json&quot;)
.addHeader(&quot;Host&quot;, &quot;x.x.x.x:9876&quot;)
.addHeader(&quot;Origin&quot;, serverUrl)
.addHeader(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;)
.url(serverUrl + &quot;/api/authentication/login&quot;).post(formBody).build();
http.newCall(req).execute();
}
public boolean updateRuleMarkdownNote(String ruleKey, String mdNote) throws IOException {
RequestBody formBody = new FormBody.Builder().add(&quot;key&quot;, ruleKey).add(&quot;markdown_note&quot;, mdNote).build();
Request req = new Request.Builder().addHeader(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;).url(serverUrl + &quot;/api/rules/update&quot;).post(formBody).build();
int code = http.newCall(req).execute().code();
return code &gt;= 200 &amp;&amp; code &lt; 300;
}
public static void main(String[] args) {
SonarApi sonarApi = new SonarApi(&quot;http://x.x.x.x:9876&quot;, &quot;cac3b3c65347e87cdf1f9a7352935db79e2435f5&quot;);
try {
sonarApi.login(&quot;admin&quot;, &quot;******&quot;);
sonarApi.updateRuleMarkdownNote(&quot;custom-rules-java:ExampleRules01&quot;, &quot;*foo*&quot;);
} catch (IOException e) {
e.printStackTrace();
}
}

logs:

六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: --&gt; POST http://172.25.160.238:9876/api/authentication/login
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Content-Length: 34
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Accept: application/json
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Host: 172.25.160.238:9876
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Origin: http://172.25.160.238:9876
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Content-Type: application/x-www-form-urlencoded
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: 
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: login=admin&amp;password=%23Fgglgy0223
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: --&gt; END POST (34-byte body)
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: &lt;-- 200 http://172.25.160.238:9876/api/authentication/login (266ms)
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: X-Frame-Options: SAMEORIGIN
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: X-XSS-Protection: 1; mode=block
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: X-Content-Type-Options: nosniff
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Set-Cookie: XSRF-TOKEN=5iohli0jdhtla6tk8a87598log; Max-Age=259200; Expires=Mon, 19-Jun-2023 06:44:23 GMT; Path=/
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Set-Cookie: JWT-SESSION=eyJhbGciOiJIUzI1NiJ9.eyJsYXN0UmVmcmVzaFRpbWUiOjE2ODY4OTc4NjMyNTMsInhzcmZUb2tlbiI6IjVpb2hsaTBqZGh0bGE2dGs4YTg3NTk4bG9nIiwianRpIjoiQVlqQzhxcFNkRWtsQ2VJdXhIQ0IiLCJzdWIiOiJBWHd3MUhOY1NCN0xfa1UtMEhydyIsImlhdCI6MTY4Njg5Nzg2MywiZXhwIjoxNjg3MTU3MDYzfQ.sDbWX2_pNGIAIMjrgOBSyEnsBLc9fncz2-7XS_uyx-M; Max-Age=259200; Expires=Mon, 19-Jun-2023 06:44:23 GMT; Path=/; HttpOnly
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Content-Length: 0
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Date: Fri, 16 Jun 2023 06:44:23 GMT
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Keep-Alive: timeout=60
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Connection: keep-alive
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: &lt;-- END HTTP (0-byte body)
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: --&gt; POST http://172.25.160.238:9876/api/rules/update
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Content-Length: 58
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Content-Type: application/x-www-form-urlencoded
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: 
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: key=custom-rules-java%3AExampleRules01&amp;markdown_note=*foo*
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: --&gt; END POST (58-byte body)
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: &lt;-- 401 http://172.25.160.238:9876/api/rules/update (7ms)
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: X-Frame-Options: SAMEORIGIN
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: X-XSS-Protection: 1; mode=block
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: X-Content-Type-Options: nosniff
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Content-Length: 0
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Date: Fri, 16 Jun 2023 06:44:23 GMT
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Keep-Alive: timeout=60
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Connection: keep-alive
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: &lt;-- END HTTP (0-byte body)

after login, I can see setcookie header from server, but why cookies not loaded for subsequent requests? I can't figure it out why. as you can see in the logs, login request is successful and cookies named as XSRF-TOKEN and JWT-SESSION was returned within response headers, but the following request did not send a Cookie header with them.

答案1

得分: 0

HttpLoggingInterceptor没有打印来自CookieJar的cookie,因为它在内部拦截器加载来自CookieJar的cookie之前执行。

通过使用tcpdump,我可以看到后续请求的Cookie头。

案件已关闭。

英文:

HttpLoggingInterceptor didn't print cookies from CookieJar, because it was executed before the internal Interceptor which load cookie from CookieJar

By using tcpdump, I can see Cookie headers for subsequent requests.

case closed.

huangapple
  • 本文由 发表于 2023年6月16日 14:54:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487636.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定