アプリケーション間で Spring Security 構成を共有する
-
13-09-2019 - |
質問
私は Spring を始めたばかりで、知識の大部分は Apress の Spring Recipes 本から得ています。
1 つの Web アプリ内で Spring Security を使用して LDAP 認証を動作させています。ただし、この 1 つの Web アプリケーションからアプリケーションのコンテキスト Bean とプロパティ ファイルを取り出し、何らかの方法で外部化して、すべての Web アプリケーションが同じ Bean を参照できるようにしたいと考えています。そのため、何か (ldapuser や ldap URL など) を変更する必要がある場合、1 か所で変更するだけで、残りのアプリがそれを認識します。
アップデート実装しました リロード可能なスプリングのプロパティ これは、プロパティの元のファイルが操作されたときにプロパティを再読み込みします。ただし、暗号化されたプロパティを使用しているため、以下は Reloadable Spring Properties のものの上に作成したクラスです。
ReloadingEncryptablePropertyPlaceholderConfigurer.java
package;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.lang.Validate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.util.text.TextEncryptor;
import org.jasypt.properties.PropertyValueEncryptionUtils;
import org.springframework.beans.factory.BeanDefinitionStoreException;
public class ReloadingEncryptablePropertyPlaceholderConfigurer extends ReloadingPropertyPlaceholderConfigurer {
protected final Log logger = LogFactory.getLog(getClass());
private final StringEncryptor stringEncryptor;
private final TextEncryptor textEncryptor;
public ReloadingEncryptablePropertyPlaceholderConfigurer(TextEncryptor textEncryptor) {
super();
logger.info("Creating configurer with TextEncryptor");
Validate.notNull(textEncryptor, "Encryptor cannot be null");
this.stringEncryptor = null;
this.textEncryptor = textEncryptor;
}
public ReloadingEncryptablePropertyPlaceholderConfigurer(StringEncryptor stringEncryptor) {
super();
logger.info("Creating configurer with StringEncryptor");
Validate.notNull(stringEncryptor, "Encryptor cannot be null");
this.stringEncryptor = stringEncryptor;
this.textEncryptor = null;
}
@Override
protected String convertPropertyValue(String originalValue) {
if (!PropertyValueEncryptionUtils.isEncryptedValue(originalValue)) {
return originalValue;
}
if (this.stringEncryptor != null) {
return PropertyValueEncryptionUtils.decrypt(originalValue, this.stringEncryptor);
}
return PropertyValueEncryptionUtils.decrypt(originalValue, this.textEncryptor);
}
@Override
protected String parseStringValue(String strVal, Properties props, Set visitedPlaceholders) throws BeanDefinitionStoreException {
return convertPropertyValue(super.parseStringValue(strVal, props, visitedPlaceholders));
}
}
そして、これが私がそれを私の中でどのように使用するかです securityContext.xml:
<bean id="securityContextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
<constructor-arg value="ldaps://ldapserver" />
<property name="urls" value="#{ldap.urls}" />
</bean>
<bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<bean id="reloadProperties" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="period" value="1000"/>
<property name="runnable">
<bean class="ReloadConfiguration">
<property name="reconfigurableBeans">
<list>
<ref bean="configproperties"/>
</list>
</property>
</bean>
</property>
</bean>
</property>
</bean>
<bean id="configproperties" class="ReloadablePropertiesFactoryBean">
<property name="location" value="classpath:ldap.properties"/>
</bean>
<bean id="ldapPropertyConfigurer" class="ReloadingEncryptablePropertyPlaceholderConfigurer">
<constructor-arg ref="configurationEncryptor" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="properties" ref="configproperties"/>
</bean>
<bean id="jasyptConfig" class="org.jasypt.encryption.pbe.config.SimpleStringPBEConfig">
<property name="algorithm" value="PBEWithMD5AndTripleDES" />
<property name="password" value="########" />
</bean>
<bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
<property name="config" ref="jasyptConfig" />
</bean>
解決
どうでしょうか:
- リストを返すメソッドの記述 LDAPサーバの数 - からの読み取り データベース表またはプロパティー・ファイル
- このメソッドを jndi 経由で公開し、それを使用してサーバーのリストを Spring config に挿入します。
- LDAP サーバーを動的に更新する必要がある場合は、ジョブで定期的に変更をポーリングするか、管理 Web ページまたは JMX Bean を使用して更新をトリガーすることができます。これらの両方のメソッドの同時実行の問題 (更新中にリストを読み取るもの) に注意してください。
他のヒント
それはSpring Securityではないでしょうか?LDAP を処理できます。そして、それを誰もが使用する 1 つのセキュリティ サービスにすれば、それを管理する方法になるのではありませんか?
所属していません StackOverflow