2

私はいくつかのファイルを含むアプリを持っています。これらの//コンボを .jar 内のこのクリア テキスト ファイルではなく、JNDI ルックアップにproperties格納する方が簡単かどうか興味がありました。usernamepasswordserver-url-to-login-to

いくつか質問があります:

  1. JNDI を使用して、データベースではない認証情報を保存できますか?
  2. これらのプロパティーを保管するために、新しい JNDI リソースをどのように構成しますか?
  3. これらのプロパティをリソースからどのように取得しますか?

私が読んだドキュメントのほとんどは、データベース接続用に JNDI をセットアップする方法でした。私が使用するために、データベースには接続していません、代わりに、さまざまな認証方式 (要求メッセージの SOAP ユーザー/パスワード、 HTTP Basic、ヘッダー、SSL など)。

4

1 に答える 1

3

ご質問への回答:

  1. JNDI を使用して、データベースではない認証情報を保存できますか?

はい。

  1. これらのプロパティーを保管するために、新しい JNDI リソースをどのように構成しますか?

Glassfish 2 を使用している場合は、カスタムPropertiesObjectFactoryクラスを作成してJNDIのプロパティを処理する必要がありjava.util.Porpertiesます。

たとえば、PropertiesObjectFactoryクラスは次のようになります。

public class PropertiesObjectFactory implements Serializable, ObjectFactory {

    public static final String FILE_PROPERTY_NAME = "org.glassfish.resources.custom.factory.PropertiesFactory.fileName";

    public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment)
            throws Exception {
        Reference ref = (Reference) obj;
        Enumeration<RefAddr> refAddrs = ref.getAll();

        String fileName = null;
        Properties fileProperties = new Properties();
        Properties properties = new Properties();

        while (refAddrs.hasMoreElements()) {
            RefAddr addr = refAddrs.nextElement();
            String type = addr.getType();
            String value = (String) addr.getContent();

            if (type.equalsIgnoreCase(FILE_PROPERTY_NAME)) {
                fileName = value;
            } else {
                properties.put(type, value);
            }
        }

        if (fileName != null) {
            File file = new File(fileName);
            if (!file.isAbsolute()) {
                file = new File(System.getProperty("com.sun.aas.installRoot") + File.separator + fileName);
            }
            try {
                if (file.exists()) {
                    try {
                        FileInputStream fis = new FileInputStream(file);
                        if (fileName.toUpperCase().endsWith("XML")) {
                            fileProperties.loadFromXML(fis);
                        } else {
                            fileProperties.load(fis);
                        }
                    } catch (IOException ioe) {
                        throw new IOException("IO Exception during properties load : " + file.getAbsolutePath());
                    }
                } else {
                    throw new FileNotFoundException("File not found : " + file.getAbsolutePath());
                }
            } catch (FileNotFoundException fnfe) {
                throw new FileNotFoundException("File not found : " + file.getAbsolutePath());
            }
        }
        fileProperties.putAll(properties);
        return fileProperties;
    }
}

そのクラスの jar を作成し、glassfish グローバル クラスパスに追加します。それは次のようになります:/glassfish/domains/domain1/libそして、JNDIプロパティ構成でファクトリ クラスとして指定できます。

Glassfish 3 にはすでにプロパティ ファクトリ クラスがあります。次のように設定されていますorg.glassfish.resources.custom.factory.PropertiesFactory

Glassfish 管理コンソールを開き、[Resources] -> [JNDI] -> [Custom Resources] に移動し、[New] をクリックして JNDI 名を指定しますjndi/credentials。例:および値列内。[OK] をクリックすると、リソースが構成されました。このリソースには、好きなだけプロパティを追加できます。java.util.Propertiesorg.glassfish.resources.custom.factory.PropertiesFactorytestUsernameNametestUsernameValueJNDIjndi/credentials

リソースの作成が完了したら、忘れずにアプリ サーバーを再起動してください。

  1. これらのプロパティをリソースからどのように取得しますか?
public Properties getProperties(String jndiName) {
    Properties properties = null;
    try {
        InitialContext context = new InitialContext();
        properties = (Properties) context.lookup(jndiName);
        context.close();
    } catch (NamingException e) {
        LOGGER.error("Naming error occurred while initializing properties from JNDI.", e);
        return null;
    }
    return properties;
}

プロパティを取得する方法の例:

String username = someInstance.getProperties("jndi/credentials").getProperty("testUsernameName"); 

あなたは次のusernameようになります: testUsernameValue.

アプリケーションでこのメソッドを呼び出すときJNDIは、アプリケーション サーバーで構成した名前を指定しますjndi/credentials。デプロイメント記述子でリソースをマップした場合は、次を使用する必要があります: java:comp/env/jndi/credentials.

を使用して同じことをしたい場合Spring

<jee:jndi-lookup id="credentials"
                 jndi-name="jndi/credentials"/>

それでいいと思います。お役に立てれば。

于 2012-10-18T10:35:02.627 に答える