Bonjour, j'ai besoin d'un tutoriel qui explique comment une communication en HTTPS se passe entre deux projets java l'un client et l'autre serveur.
Merci
Version imprimable
Bonjour, j'ai besoin d'un tutoriel qui explique comment une communication en HTTPS se passe entre deux projets java l'un client et l'autre serveur.
Merci
Simple,
1 - ton projet "Serveur", doit être une application tournant sur un serveur d'application avec HTTPS, comme Tomcat, GlassFish, etc. Tu peux aussi embarqué ton propre mini serveur HTTPS pour faire tourner ton code : http://tjws.sourceforge.net/
2 - ton projet client se connecte à l'URL du projet serveur en HTTPS de manière programmatique. Je te conseille l'usage de la librairie Apache Commons HttpClient, incluse maintenant dans Apache Commons Components. Tu as un tutorial HTTPS à cette adresse : http://hc.apache.org/httpclient-3.x/sslguide.html
Bon courage.
j'ai déjà créé les deux projets "serveur" et "client" dans le fichier webapps de Tomcat 6, après j'ai configuré le fichier server.xml comme suis pour mettre en place le connecteur HTTPS:
voici maintenant une partie de mon code source du client:Code:
1
2
3 <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" keystoreFile="C:\Users\\java\Java\jre6\bin\server-keystore.jks" keystorePass="password" />
Mais quand j'exécute les deux projets j'obtiens cette exception :Code:
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 public class Transporter { // URL components (should be configured variables) private static final String HTTP = "HTTP"; private static final String HTTPS = "HTTPS"; private static final String HOSTNAME = "localhost"; private static final Integer PORT = 8443; // secure channel key material stores (should be configured) private static final String keystore = "C:/Users/certificat/client-keystore.jks"; private static finalStringtruststore= "C:/Users/certificat/servertruststore.jks"; private static final String keypass = "password"; private static final String trustpass = "password"; // secure channel variables private Boolean isSecure = true; private SSLSocketFactory sslSocketFactory = null; public Transporter() { setupSocketFactory(); } private void setupSocketFactory(){ try { String protocol = "TLS"; String type = "JKS"; String algorithm = KeyManagerFactory.getDefaultAlgorithm(); String trustAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); // create and initialize an SSLContext object SSLContext sslContext = SSLContext.getInstance(protocol); sslContext.init(getKeyManagers(type, algorithm), getTrustManagers(type, trustAlgorithm), new SecureRandom()); // obtain the SSLSocketFactory from the SSLContext sslSocketFactory = sslContext.getSocketFactory(); System. out.println(" sslSocketFactory ---:"+ sslSocketFactory); } catch (Exception e) { e.printStackTrace();} } //fin methode private KeyStore getStore(String type,String filename, String pwd) throws Exception{ System.out.println("---methode keystore getStore---"); KeyStore ks = KeyStore.getInstance(type); InputStream istream = null; try {File ksfile = new File(filename); istream = new FileInputStream(ksfile); ks.load(istream, pwd != null? pwd.toCharArray(): null); } finally { if (istream != null) istream.close(); } return ks; } //fin methode private KeyManager[] getKeyManagers(String type,String algorithm) throws Exception { System. out.println("---methode keyManager---"); KeyStore ks = getStore(type, keystore, keypass); KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm); kmf.init(ks, keypass.toCharArray()); return kmf.getKeyManagers(); } //fin methode private TrustManager[] getTrustManagers(String type,String algorithm) throws Exception { KeyStore ts = getStore(type, truststore, trustpass); TrustManagerFactory tmf =TrustManagerFactory.getInstance(algorithm); tmf.init(ts); return tmf.getTrustManagers(); } //fin methode public void send(String message) { System. out.println("methode send(---"); byte[] bytes = message.toString().getBytes(); HttpURLConnection conn = null; try { URL url = new URL(isSecure? HTTPS: HTTP, HOSTNAME, PORT, "/TESTE/servlett1"); conn = (HttpURLConnection)url.openConnection(); System.out.println("connexion conn"); if (isSecure) { HttpsURLConnection sconn = (HttpsURLConnection)conn; sconn.setSSLSocketFactory(sslSocketFactory); System.out.println("avant post"); } conn.setRequestMethod("POST"); conn.addRequestProperty("Content-Length", "" + bytes.length); // conn.addRequestProperty("Content-Type", CTYPE); conn.addRequestProperty("Connection", "Keep-Alive"); conn.setDoOutput(true); conn.setDoInput(true); System.out.println("--avant connect"); conn.connect(); System.out.println("--apres connect"); // send POST data OutputStream out = (OutputStream)conn.getOutputStream(); System.out.println("OutPutStream"); out.write(bytes); out.flush(); out.close(); // get response code and data System.out.println(conn.getResponseCode()); BufferedReader read = new BufferedReader(new InputStreamReader(conn.getInputStream())); String query = null; while((query = read.readLine()) != null) System.out.println(query); } catch(MalformedURLException e) { e.printStackTrace(); } catch(ProtocolException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } finally { conn.disconnect(); } } }
A votre avis pourquoi cette exception se lance??Citation:
javax.net.ssl.SSLHandshakeException
: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
parce que la JVM cliente doit avoir accès au certificat public de ton serveur Tomcat.
Consulte ton tomcat avec FireFox en HTTPS.
Sauvegarde le certificat public
ou alors récupère directement le certificat depuis le keystore. (tu as bien mis un certificat dans ton keystore du serveur Tomcat)
enregistre ensuite ton certificat dans le keystore de la JVM de ton client.
Je te recommande d'utiliser l'outil graphique "portecle" pour gérer tes keystores. C'est graphique et intuitif. : http://portecle.sourceforge.net/
maintenant ton client devrait pouvoir se connecter en HTTPS à ton serveur.
et puis autre conseil, quand tu as une exception, direct Google ... le premier lien c'est cette article qui t'aurait bien dépanné :
https://blogs.oracle.com/gc/entry/un..._certification
Merci beaucoup pour ton aide mais c’était déjà fait, j'ai déjà importé le certificat server.crt (et même ca.crt qui a signé le certificat du serveur) dans le fichier cacerts à l'aide de cette commande:
./keystool -import -alias alias -file ca.cer -keystore ../lib/security/cacerts
et je les ai aussi ajouté dans le navigateur. Mais j'ai toujours cette exception :(
Essaye de suivre cet exemple de "SelfTrustedCert" :
http://svn.apache.org/viewvc/httpcom...va?view=markup
biensûr il faudra que tu utilises HttpClients :
http://hc.apache.org/httpclient-3.x/sslguide.html
tu arrives à attaquer en HTTPS ton Tomcat déjà avec un simple navigateur, comme FireFox ?
Bonjour,
merci pour ton aide en tout cas le problème que j'avais c'est que quand j'ajoutais le serveur tomcat à eclipse ce dernier conserve la configuration de server.xml une fois pour toute, c'est pour ça l'application ne pouvait pas prendre en considération les nouveaux chemins de certificats que je lui ai donné. Elle gardait les chemins des anciens certificats qui ne respectent pas le domaine dans lequel je travaille (localhost) donc j'étais obligé de supprimer tomcat et de le rajouter avec la bonne configuration :lol:
:ccool:
tu peux passer le fil de discussion à "résolu" en cliquant sur le bouton, alors ;)