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

Entrée/Sortie Java Discussion :

[Apache] S'identifier auprès d'un serveur apache [FAQ]


Sujet :

Entrée/Sortie Java

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    25
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 25
    Points : 21
    Points
    21
    Par défaut [Apache] S'identifier auprès d'un serveur apache
    Bonjour,

    j'aimerai savoir comment s'identifier sur un site protégé par la sécurité apache defini dans le htaccess au moyen d'un script java.

    Merci

  2. #2
    Membre confirmé
    Avatar de Glob
    Homme Profil pro
    Architecte de système d'information
    Inscrit en
    Avril 2002
    Messages
    428
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Suisse

    Informations professionnelles :
    Activité : Architecte de système d'information

    Informations forums :
    Inscription : Avril 2002
    Messages : 428
    Points : 630
    Points
    630
    Par défaut
    Mhhh...?

    C'est une question Java, Apache ou Javascript?

    Ou les 3?

    Glob
    What would you do if you were not afraid?

    Cours et tutoriels pour apprendre Java , FAQ Java, et Forum Java

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    25
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 25
    Points : 21
    Points
    21
    Par défaut
    C'est une question java.
    Une fois la connexion etablie avec le serveur par le biais de la classe URLConnection, le serveur ne me laisse pas aller plus loin car il faut que je m'identifie. Or je ne sais pas comment le faire. Pour moi il faut passer des parametres dans le DataOutputStream renvoyé par la méthode getOutputStream() de l'objet URLConnection.
    Que dois je faire?

  4. #4
    Membre à l'essai
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    25
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 25
    Points : 21
    Points
    21
    Par défaut
    Bonjour,

    ayant trouvé la solution, je reviens vers ce post pour vous en faire profiter au cas ou...
    Voici le code qui permet de se connecter à un serveur apache sécurisé au moyen d'une connexion derriere un proxy.
    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
     
    private static InputStream getInputStreamServer(HashMap hashParam){
    		try{
     
    			System.err.println("Debut connexion ...");
    			String uname =(String) hashParam.get(Constantes.APACHE_USERNAME);
    			String pwd=(String) hashParam.get(Constantes.APACHE_PASSWD);
    			URL aUrl = null;
    			URLConnection anURLConnection = null;
    			String content = "";
    			BufferedReader in = null;
    			String line;
     
     
    			aUrl = new URL((String)hashParam.get(Constantes.URL));
     
     
    			if(hashParam.get(Constantes.PROXY_HOST)!=null && ((String)hashParam.get(Constantes.PROXY_HOST)).length()!=0){
    				System.err.println("Configuration Proxy ...");
    				System.getProperties().put("proxySet", "true");
    				System.getProperties().put("proxyHost",hashParam.get(Constantes.PROXY_HOST));
    				System.getProperties().put("proxyPort", hashParam.get(Constantes.PROXY_PORT));
    				String authString = hashParam.get(Constantes.PROXY_USERNAME)+":"+hashParam.get(Constantes.PROXY_PASSWD);
     
    				String auth =
    						   "Basic "
    							   + new sun.misc.BASE64Encoder().encode(authString.getBytes());
    				System.err.println("Configuration Proxy OK...");
    			}
    			System.err.println("Connnexion en cours...");
     
    			anURLConnection = aUrl.openConnection();
     
     
     
    			//identification apache							   						
    			if(uname !=null && uname.length()>0){
    				System.err.println("Identification Apache...");
    				anURLConnection.setRequestProperty("Authorization",		 
    				encodeAuthorization(uname, pwd));	
    				System.err.println("Identification Apache OK");
    			}
     
    			InputStream inputStream =anURLConnection.getInputStream();
    			System.err.println("Connexion etablie avec le serveur distant");
    			return inputStream;
    		}
    		catch (IOException e) {				
     
    			try{
    				String url =(String) hashParam.get(Constantes.URL);
    				FileOutputStream file;
    				if(url.lastIndexOf("/")+1 == url.length() || url.lastIndexOf("/")==6){
    					file = new FileOutputStream((String) hashParam.get(Constantes.PATH_FICHIER_OUT)+(String) hashParam.get(Constantes.DEFAULT_FICHIER));					
    				}
    				else{
    					file = new FileOutputStream((String) hashParam.get(Constantes.PATH_FICHIER_OUT)+url.substring(url.lastIndexOf("/")+1));					
    				}
    				file.write("Connexion serveur KO".getBytes());
    				file.close();
    			}
    			finally{
    				throw new RuntimeException("Impossible de se connecter au serveur distant "+e.getMessage());
    			}
     
    		}				
    	}
     
    private static String encodeAuthorization (String username,String password) {
            String authorization = username + ":" + password;
            sun.misc.BASE64Encoder enCoder = new sun.misc.BASE64Encoder();
            return "Basic " + enCoder.encode(authorization.getBytes());
    	}
    Si vous avez des commentaires n'hesitez pas.

    @+

  5. #5
    Membre actif Avatar de orelero
    Étudiant
    Inscrit en
    Novembre 2004
    Messages
    389
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2004
    Messages : 389
    Points : 274
    Points
    274
    Par défaut
    bonjour jnjp28, je suis debutant , la seule chose que je connais c'est simplement programmer un programme serveur client, mais je suis interessé de pouvoir se connecter à un serveur apache pour pouvoir y ecrire de simples données pour qu'elles puissent etre lues par la suite avec la partie cliente, est ce que tu sais comment faire ? merci
    (juste une indication me suffira )
    "L'imagination est plus importante que la connaissance." - Albert Einstein.

  6. #6
    Débutant
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    496
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 496
    Points : 149
    Points
    149
    Par défaut
    Citation Envoyé par JnJp28
    Bonjour,

    ayant trouvé la solution, je reviens vers ce post pour vous en faire profiter au cas ou...
    Voici le code qui permet de se connecter à un serveur apache sécurisé au moyen d'une connexion derriere un proxy.
    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
     
    private static InputStream getInputStreamServer(HashMap hashParam){
            try{
     
                System.err.println("Debut connexion ...");
                String uname =(String) hashParam.get(Constantes.APACHE_USERNAME);
                String pwd=(String) hashParam.get(Constantes.APACHE_PASSWD);
                URL aUrl = null;
                URLConnection anURLConnection = null;
                String content = "";
                BufferedReader in = null;
                String line;
     
     
                aUrl = new URL((String)hashParam.get(Constantes.URL));
     
     
                if(hashParam.get(Constantes.PROXY_HOST)!=null && ((String)hashParam.get(Constantes.PROXY_HOST)).length()!=0){
                    System.err.println("Configuration Proxy ...");
                    System.getProperties().put("proxySet", "true");
                    System.getProperties().put("proxyHost",hashParam.get(Constantes.PROXY_HOST));
                    System.getProperties().put("proxyPort", hashParam.get(Constantes.PROXY_PORT));
                    String authString = hashParam.get(Constantes.PROXY_USERNAME)+":"+hashParam.get(Constantes.PROXY_PASSWD);
     
                    String auth =
                               "Basic "
                                   + new sun.misc.BASE64Encoder().encode(authString.getBytes());
                    System.err.println("Configuration Proxy OK...");
                }
                System.err.println("Connnexion en cours...");
     
                anURLConnection = aUrl.openConnection();
     
     
     
                //identification apache                                                       
                if(uname !=null && uname.length()>0){
                    System.err.println("Identification Apache...");
                    anURLConnection.setRequestProperty("Authorization",         
                    encodeAuthorization(uname, pwd));    
                    System.err.println("Identification Apache OK");
                }
     
                InputStream inputStream =anURLConnection.getInputStream();
                System.err.println("Connexion etablie avec le serveur distant");
                return inputStream;
            }
            catch (IOException e) {                
     
                try{
                    String url =(String) hashParam.get(Constantes.URL);
                    FileOutputStream file;
                    if(url.lastIndexOf("/")+1 == url.length() || url.lastIndexOf("/")==6){
                        file = new FileOutputStream((String) hashParam.get(Constantes.PATH_FICHIER_OUT)+(String) hashParam.get(Constantes.DEFAULT_FICHIER));                    
                    }
                    else{
                        file = new FileOutputStream((String) hashParam.get(Constantes.PATH_FICHIER_OUT)+url.substring(url.lastIndexOf("/")+1));                    
                    }
                    file.write("Connexion serveur KO".getBytes());
                    file.close();
                }
                finally{
                    throw new RuntimeException("Impossible de se connecter au serveur distant "+e.getMessage());
                }
     
            }                
        }
     
    private static String encodeAuthorization (String username,String password) {
            String authorization = username + ":" + password;
            sun.misc.BASE64Encoder enCoder = new sun.misc.BASE64Encoder();
            return "Basic " + enCoder.encode(authorization.getBytes());
        }
    Si vous avez des commentaires n'hesitez pas.

    @+
    S'il n'y aurait pas un proxy, est-ce que le code restera le même ?

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

Discussions similaires

  1. Utilisation de l'ASP sur un serveur Apache
    Par 3adoula dans le forum Réseau
    Réponses: 1
    Dernier message: 29/10/2004, 14h34
  2. Tomcat avec un serveur apache specifique
    Par benji999 dans le forum Tomcat et TomEE
    Réponses: 1
    Dernier message: 20/04/2004, 19h02
  3. Sécurité sur serveur apache
    Par Beaunico dans le forum Apache
    Réponses: 8
    Dernier message: 13/04/2004, 07h03
  4. Sécuriser son serveur Apache sous Linux
    Par rolkA dans le forum Sécurité
    Réponses: 6
    Dernier message: 01/12/2003, 15h16

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