Bonjour,

Dans le cadre d'un developpement d'un intranet, cette application doit pouvoir acces à des ressources Web. Hors L'entreprise dans laquelle je suis dispose d'un proxy autentifié Ntlm.

J'ai donc réalisé ce servlet d'exemple qui fonctionne très bien lorsque je passe par le plugin WTP / tomcat d'eclipse.

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
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
import java.net.Proxy.Type;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class Test extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.setProperty("http.auth.ntlm.domain", "domain");
 
 
		final Proxy proxy = new Proxy(Type.HTTP, InetSocketAddress
                .createUnresolved("ip_proxy", port_proxy));
 
		Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication
            getPasswordAuthentication() {
                //respond only to proxy auth requests
                if (getRequestorType().equals(RequestorType.PROXY)) {
                    return new  PasswordAuthentication("user",
                            "password"
                                    .toCharArray());
                } else {
                    return null;
                }
            }
        });
 
		HttpURLConnection con = (HttpURLConnection) new URL("http://www.google.fr").openConnection(proxy);
 
		System.out.println(con.getResponseCode());
 
		BufferedReader br;
        try {
            InputStream stream = con.getInputStream();
            br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
            StringBuffer buf = new StringBuffer();
            String line;
            while (null != (line = br.readLine())) {
                buf.append(line).append("\n");
            }
            System.out.println(buf.toString());
            response.getWriter().write(buf.toString());
            response.getWriter().flush();
            stream.close();
            con.disconnect();
        } catch (IOException ioe) {
        	ioe.printStackTrace();
        }
 
	}
}
Hors, lorsque que j'exporte ce projet en .war pour le déployer dans le server tomcat de la même machine (i.e l'installation de celui ci et du tomcat utilisé dans eclipse sont les mêmes) mon code me retourne un http 403

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
 
403
java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.google.fr
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
	at jcifs.http.NtlmHttpURLConnection.getInputStream(NtlmHttpURLConnection.java:242)
	at Test.doPost(Test.java:87)
	at Test.doGet(Test.java:43)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
	at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:857)
	at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:565)
	at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1509)
	at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.google.fr
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(Unknown Source)
	at jcifs.http.NtlmHttpURLConnection.parseResponseCode(NtlmHttpURLConnection.java:424)
	at jcifs.http.NtlmHttpURLConnection.doHandshake(NtlmHttpURLConnection.java:436)
	at jcifs.http.NtlmHttpURLConnection.handshake(NtlmHttpURLConnection.java:103)
	at jcifs.http.NtlmHttpURLConnection.getResponseCode(NtlmHttpURLConnection.java:393)
	at Test.doPost(Test.java:83)
	... 15 more

nota : L'idée est de se passer du HttpClient d'apache, et de conserver le code avec HttpUrlConnection

Si quelqu'un a une idée, une piste...

EDIT :
En fait il faut setter la variable http.auth.preference=basic

Ce qui est bizarre c'est que lancé dans wtp, il n'y a pas besoin.

Voilà, en espérant que ça puisse servir à quelqu'un