ご質問への回答:
- JNDI を使用して、データベースではない認証情報を保存できますか?
はい。
- これらのプロパティーを保管するために、新しい 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.Properties
org.glassfish.resources.custom.factory.PropertiesFactory
testUsernameName
testUsernameValue
JNDI
jndi/credentials
リソースの作成が完了したら、忘れずにアプリ サーバーを再起動してください。
- これらのプロパティをリソースからどのように取得しますか?
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"/>
それでいいと思います。お役に立てれば。