Tutoriel sur la sécurité Java SE - Etape 3

Le reste du tutoriel explique comment activer l'authentification de client avant de se connecter à un serveur eXtreme Scale. Pour vous préparer à l'étape suivante de ce tutoriel, vous devez empaqueter le programme SecureSimpleApp.java en un fichier JAR et créer un ensemble de fichiers de configuration, incluant un fichier security.xml et deux fichiers de configuration JAAS. Le fichier security.xml vous permet d'écrire l'authentification dans l'environnement et les fichiers de configuration JAAS fournissent le mécanisme d'authentification lors de la connexion au serveur.

Procédure

  1. Dans une fenêtre de ligne de commande, accédez au répertoire rép_base_wxs/applib que vous avez créé dans Tutoriel sur la sécurité Java SE - Etape 1.
  2. Créez et compilez la classe SecureSimpleApp.java suivante :
    SecureSimpleApp.java
    package com.ibm.websphere.objectgrid.security.sample.guide;
    
    import com.ibm.websphere.objectgrid.ClientClusterContext;
    import com.ibm.websphere.objectgrid.ObjectGrid;
    import com.ibm.websphere.objectgrid.ObjectGridManager;
    import com.ibm.websphere.objectgrid.ObjectGridManagerFactory;
    import com.ibm.websphere.objectgrid.security.config.ClientSecurityConfiguration;
    import com.ibm.websphere.objectgrid.security.config.ClientSecurityConfigurationFactory;
    import com.ibm.websphere.objectgrid.security.plugins.CredentialGenerator;
    import com.ibm.websphere.objectgrid.security.plugins.builtins.UserPasswordCredentialGenerator;
    
    public class SecureSimpleApp extends SimpleApp {
    
        public static void main(String[] args) throws Exception {
    
            SecureSimpleApp app = new SecureSimpleApp();
            app.run(args);
        }
    
        /**
         * Get the ObjectGrid
         * @return an ObjectGrid instance
         * @throws Exception
         */
        protected ObjectGrid getObjectGrid(String[] args) throws Exception {
            ObjectGridManager ogManager = ObjectGridManagerFactory.getObjectGridManager();
            ogManager.setTraceFileName("logs/client.log");
            ogManager.setTraceSpecification("ObjectGrid*=all=enabled:ORBRas=all=enabled");
    
            // crée un objet ClientSecurityConfiguration à l'aide du fichier spécifié
            ClientSecurityConfiguration clientSC = ClientSecurityConfigurationFactory
                    .getClientSecurityConfiguration(args[0]);
            
            // crée un CredentialGenerator en utilisant le nom de l'utilisateur et le mot de passe fournis.
            CredentialGenerator credGen = new UserPasswordCredentialGenerator(args[1], args[2]);
            clientSC.setCredentialGenerator(credGen);
            
            // crée un ObjectGrid en se connectant au serveur de catalogue 
            ClientClusterContext ccContext = ogManager.connect("localhost:2809", clientSC, null);
            ObjectGrid og = ogManager.getObjectGrid(ccContext, "accounting");
    
            return og;
    
        }
    
    }
  3. Compilez le package avec ces fichiers et nommez le fichier JAR sec_sample.jar.
  4. Accédez au répertoire rép_base_wxs.
  5. Créez un répertoire appelé security.
  6. Créez un fichier de configuration appelé security.xml. Les propriétés de sécurité du serveur sont spécifiées dans ce fichier. Ces propriétés sont communes pour les serveurs de catalogue et les serveurs de conteneur.
    security.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <securityConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://ibm.com/ws/objectgrid/config/security ../objectGridSecurity.xsd"
    	xmlns="http://ibm.com/ws/objectgrid/config/security">
    
    	<security securityEnabled="true" loginSessionExpirationTime="300" >
            
            <authenticator className ="com.ibm.websphere.objectgrid.security.plugins.builtins.KeyStoreLoginAuthenticator">
            </authenticator>
        </security>
    	
    </securityConfig>