IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Sécurité Java Discussion :

[HTTPClient] Authentification à mon blog Wordpress


Sujet :

Sécurité Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    406
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 406
    Par défaut [HTTPClient] Authentification à mon blog Wordpress
    Bonjour à tous,

    Je souhaiterais utiliser HTTPClient pour me connecter à mon blog Wordpress et ensuite récupérer des informations dans une page de l'administration (la page qui gère les tags).

    Pour récupérer les informations, j'essaie déjà d'afficher le code source de la page de l'administration (la page qui gère les tags) une fois m'être authentifié (avec la première requête) mais cela ne fonctionne pas .

    La variable "authState" est toujours nulle . Du coup, j'ai l'impression qu'il ne s'authentifie jamais. De plus, le code source de la deuxième page (celle des tags) est celui de la première.

    Voici mon code :
    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
    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
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
     
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    import org.apache.http.auth.AuthScope;
    import org.apache.http.auth.AuthState;
    import org.apache.http.auth.Credentials;
    import org.apache.http.auth.UsernamePasswordCredentials;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.client.protocol.ClientContext;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.protocol.BasicHttpContext;
    import org.apache.http.protocol.HttpContext;
    import org.apache.http.util.EntityUtils;
     
    public class Main {
    	public static void main(String[] args) {
    		DefaultHttpClient httpclient = new DefaultHttpClient();
    		try {
    			if (authentification(httpclient)) {
    				System.out.println("Authentification réussie !");
    				recupererPageApresAuthentification(httpclient);
    			} else {
    				System.out.println("Authentification râtée !");
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			httpclient.getConnectionManager().shutdown();
    		}
    	}
     
    	private static boolean authentification(DefaultHttpClient httpclient) throws IOException, ClientProtocolException {
    		String login = "monLogin";
    		String password = "monMotDePasse";
    		String identifiant = login + ":" + password;
     
    		// Authentification Basic
    		// Create local execution context
    		HttpResponse response = null;
    		HttpContext localContext = new BasicHttpContext();
     
    		HttpPost httpPost = new HttpPost("http://" + identifiant + "@monblogwordpress.fr/wp-login.php");
    		boolean trying = true;
    		while (trying) {
    			response = httpclient.execute(httpPost, localContext);
     
    			System.out.println("URL analysée : " + httpPost.getRequestLine());
    			System.out.println(response.getStatusLine());
    			System.out.println("----------------------------------------");
     
    			// Consume response content
    			HttpEntity entity = response.getEntity();
    			EntityUtils.consume(entity);
     
    			int sc = response.getStatusLine().getStatusCode();
     
    			AuthState authState = null;
    			if (sc == HttpStatus.SC_UNAUTHORIZED) { // ERREUR 401
    				// Target host authentication required
    				authState = (AuthState) localContext.getAttribute(ClientContext.TARGET_AUTH_STATE);
    			}
    			if (sc == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) { // ERREUR
    																		// 407
    				// Proxy authentication required
    				authState = (AuthState) localContext.getAttribute(ClientContext.PROXY_AUTH_STATE);
    			}
     
    			if (authState != null) {
    				System.out.println("----------------------------------------");
    				AuthScope authScope = authState.getAuthScope();
    				System.out.println("Please provide credentials");
    				System.out.println(" Host: " + authScope.getHost() + ":" + authScope.getPort());
    				System.out.println(" Realm: " + authScope.getRealm());
     
    				if (login != null && password.length() > 0) {
    					Credentials creds = new UsernamePasswordCredentials(login, password);
    					httpclient.getCredentialsProvider().setCredentials(authScope, creds);
    					trying = true;
    				} else {
    					trying = false;
    				}
    			} else {
    				trying = false;
    			}
    		}
    		if (response.getStatusLine().getStatusCode() == 200) {
    			httpPost.abort();
    			return true;
    		}
    		httpPost.abort();
    		return false;
    	}
     
             private static void recupererPageApresAuthentification(DefaultHttpClient httpclient) throws IOException, RuntimeException {
    		HttpGet httpGet = new HttpGet("http://monblogwordpress.fr/wp-admin/edit-tags.php?taxonomy=post_tag");
    		HttpResponse getResponse = httpclient.execute(httpGet);
     
    		HttpEntity entity = getResponse.getEntity();
     
    		System.out.println("----------------------------------------");
    		System.out.println("URL analysée : " + httpGet.getRequestLine());
    		System.out.println(getResponse.getStatusLine());
    		System.out.println("----------------------------------------");
     
    		if (entity != null) {
    			InputStream instream = null;
    			try {
    				instream = entity.getContent();
    				BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
    				// On lit le HTML
    				String ligne = null;
    				System.out.println("Code source de la page de l'administration (page des tags) :");
    				while ((ligne = reader.readLine()) != null) {
    					System.out.println(ligne);
    				}
    			} catch (IOException ex) {
    				httpGet.abort();
    				throw ex;
    			} catch (RuntimeException ex) {
    				httpGet.abort();
    				throw ex;
    			} finally {
    				httpGet.abort();
    				if (instream != null) {
    					instream.close();
    				}
    			}
    		}
    		System.out.println("----------------------------------------");
    	}
    }

  2. #2
    Membre éclairé
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    406
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 406
    Par défaut
    J'ai finalement lu pas mal de choses sur le protocole HTTP et j'ai fais par comprendre le mécanisme. J'ai donc réussi à me connecter à mon blog.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Windows Live Writer - puis l'utiliser avec mon blog ?
    Par DonJR dans le forum Autres Logiciels
    Réponses: 2
    Dernier message: 16/12/2006, 19h23
  2. Faire de mon blog le home page dans un navigateur internet.
    Par Paulinho dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 10/08/2006, 10h58

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo