Bonjour,
J'ai un call à l'URL https://MyUrl1.domain.com/api/property qui fait un redirect vers https://AnUrl2.domain.com/login
Je traite ce call à l'API avec le code Java suivant:
avec
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 public class MyAPICall { public static void main(String[] args) String url = "https://MyUrl1.domain.com/api/property"; HttpRequestBase req = new HttpGet(url); CloseableHttpClient httpClient = HttpClientBuilder.create().addInterceptorFirst(new MyInterceptor()).setRedirectStrategy(new MyRedirectStrategy()).build(); HTTPResponseHandler myHttpRespHdl = new HTTPResponseHandler(); String content = httpClient.execute(req, myHttpRespHdl, new HttpClientContext()); System.out.println(content); }
et
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 public class MyInterceptor implements HttpRequestInterceptor { @Override public void process(HttpRequest req, HttpContext ctx) throws HttpException, IOException { HttpClientContext clientCtx = HttpClientContext.adapt(ctx); HttpHost targetHost = clientCtx.getTargetHost(); String host = targetHost.getHostName(); if (host.matches("(.*)domain.com")) { if (req.getRequestLine().getUri().startsWith("/login")) { if (req.getRequestLine().getMethod().equalsIgnoreCase("get")) { Header accept = req.getFirstHeader("Accept"); if (accept != null) { if (!accept.getValue().equals("application/json")) req.removeHeader(accept); req.addHeader("Accept", "application/json"); } } else { req.addHeader("Accept", "application/json"); } } } } } }
Sur une machine A où le code fonctionne j'ai le séquencement suivant:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public class MyRedirectStrategy implements RedirectStrategy { @Override public boolean isRedirected(HttpRequest req, HttpResponse res, HttpContext ctx) throws ProtocolException { HttpClientContext clientCtx = HttpClientContext.adapt(ctx); HttpHost targetHost = clientCtx.getTargetHost(); String host = targetHost.getHostName(); int returnCode = res.getStatusLine().getStatusCode(); if (returnCode == 302) { if (req.getRequestLine().getMethod().equalsIgnoreCase("get")) { return true; } else{ if (host.matches("(.*)domain.com")) { if (req.getRequestLine().getUri().startsWith("/login")) { if (req.getRequestLine().getMethod().equalsIgnoreCase("post")) { return true; } } } return false; } } else { if (returnCode == 200) { if (host.matches("(.*)domain.com")) { if (req.getRequestLine().getUri().startsWith("/login")) { if (req.getRequestLine().getMethod().equalsIgnoreCase("get")) { return true; } } } } return false; } } @Override public HttpUriRequest getRedirect(HttpRequest req, HttpResponse res, HttpContext ctx) throws ProtocolException { HttpClientContext clientCtx = HttpClientContext.adapt(ctx); HttpHost targetHost = clientCtx.getTargetHost(); String host = targetHost.getHostName(); int returnCode = res.getStatusLine().getStatusCode(); if (returnCode == 302) { Header header = res.getFirstHeader("Location"); if (header != null) { String location = header.getValue(); HttpGet follow = new HttpGet(location); return follow; } else { throw new ProtocolException("302 without Location header"); } } else { if (returnCode == 200) { if (host.matches("(.*)domain.com")) { if (req.getRequestLine().getUri().startsWith("/login")) { if (req.getRequestLine().getMethod().equalsIgnoreCase("get")) { HttpPost logReq = BuildMyLoginRequest(); //Call that allow me to create a Post call with credentials not detail here return logReq; } else if (req.getRequestLine().getMethod().equalsIgnoreCase("post")) { throw new ProtocolException("code 302 expeted here"); } else { throw new ProtocolException("Request method invalid"); } } else { throw new ProtocolException("Not a login request"); } } else { throw new ProtocolException("Not the right logon host"); } } else { throw new ProtocolException("Unexpected Http code"); } } } }
Maintenant le même jar lancé sur une machine B, le code ne fonctionne plus car pour une raison que j'ignore, le format des HttpRequest n'est pas le même:1er call à process : req = GET / HTTP/1.1 [Accept: application/json]
1er call à isRedirected : req = GET /api/property HTTP/1.1 [Accept: application/json, Host: MyUrl1.domain.com, Connection: Keep-Alive, User-Agent: Apache-HttpClient/4.5 (Java/11.0.3), Accept-Encoding: gzip,deflate]
=> host = MyUrl1.domain.com
=> returnCode = 302
=> On a bien une Method Request "GET" -> donc isRedirected = True
1er call à getRedirect : req = GET /api/property HTTP/1.1 [Accept: application/json, Host: MyUrl1.domain.com, Connection: Keep-Alive, User-Agent: Apache-HttpClient/4.5 (Java/11.0.3), Accept-Encoding: gzip,deflate]
=> host = MyUrl1.domain.com
=> returnCode = 302
=> Location = https://AnUrl2.domain.com/login
2eme call à process : req = GET /login HTTP/1.1 [Accept: application/json]
=> host = AnUrl2.domain.com
=> req.getRequestLine().getUri() = "/login", donc commence bien par /login
=> l'accept header est déjà "application/json"
2eme call à isRedirected : req = GET /login HTTP/1.1 [Accept: application/json, Host: AnUrl2.domain.com, C onnection: Keep-Alive, User-Agent: Apache-HttpClient/4.5 (Java/11.0.3), Accept-Encoding: gzip,deflate]
=> host = AnUrl2.domain.com
=> returnCode = 200
=> req.getRequestLine().getUri() = "/login", donc commence bien par /login
=> On a bien une Method Request "GET" -> donc isRedirected = True
2eme call à getRedirect : req = GET /login HTTP/1.1 [Accept: application/json, Host: AnUrl2.domain.com, Connection: Keep-Alive, User-Agent: Apache-HttpClient/4.5 (Java/11.0.3), Accept-Encoding: gzip,deflate]
=> host =AnUrl2.domain.com
=> returnCode = 200
=> Location = https://AnUrl2.domain.com/login
=> On a bien une Method Request "GET"
=> Je redirige vers ma Request POST avec les credential pour me logger
Du coup je ne passe pas dans le getRedirect pour faire ma Request POST avec les credential pour me logger1er call à process : req = GET /api/property HTTP/1.1 [Accept: application/json]
1er call à isRedirected : req = GET https://MyUrl1.domain.com/api/property HTTP/1.1
=> host = MyUrl1.domain.com
=> returnCode = 302
=> On a bien une Method Request "GET" -> donc isRedirected = True
1er call à getRedirect : req = GET https://MyUrl1.domain.com/api/property HTTP/1.1
=> host = MyUrl1.domain.com
=> returnCode = 302
=> Location = https://AnUrl2.domain.com/login
2eme call à process : req = GET /login HTTP/1.1 [Accept: application/json]
=> host = AnUrl2.domain.com
=> req.getRequestLine().getUri() = "/login", donc commence bien par /login
=> l'accept header est déjà "application/json"
2eme call à isRedirected : req = GET https://AnUrl2.domain.com/login HTTP/1.1
=> host = AnUrl2.domain.com
=> returnCode = 200
=> req.getRequestLine().getUri() = "https://AnUrl2.domain.com/login", donc ne commence par "/login"
=> donc isRedirected = False
Je n'arrive pas à comprendre ce qui peut entrainer cette différence de comportement, mais du coup mon JAR n'est pas déployable. Je ne vois pas non plus comment debugger ce problème.
D'avance merci pour votre aide.
Partager