Сделайте весеннюю защиту и apaches перечитайте файл.ldif
Я использую Spring-Security для простой аутентификации, и apacheDS через простой файл ldif:
<!-- BEGIN LDIF CONFIGURATION -->
<security:ldap-server ldif="classpath:spring-security-on-LDIF.ldif" root="dc=foo,dc=com" />
<bean id="userDetailsContextMapper" class="com.foo.myapp.login.springsecurity.MyLdapUserDetailsMapper">
<constructor-arg ref="MyUserDetailsService" />
</bean>
<security:authentication-manager alias="authenticationManager" >
<security:ldap-authentication-provider user-search-base="ou=users" user-search-filter="uid={0}" user-context-mapper-ref="userDetailsContextMapper"/>
</security:authentication-manager>
<!-- END LDIF CONFIGURATION -->
Это отлично работает. Теперь я хочу добавить нового пользователя в мой файл.ldif. К сожалению, мне нужно перезапустить tomcat, чтобы перечитать файл.ldif. Есть ли способ заставить apacheDS перечитать / перечитать файл ldif в определенный момент?
1 ответ
Ну, думаю, я понял это. Вы получаете ApacheDSContainer из контекста и вызываете его destroy() (который вызывает stop и уничтожает workingDir). Затем вы вызываете afterPropertiesSet() (который создает workingDir, а затем также вызывает start ()). Похоже, работает довольно хорошо. Я делаю это всякий раз, когда вижу изменения файла. Я использовал org.apache.commons.io.monitor.FileAlteredListener для просмотра файла.ldif, который запускает onFileChange() в соответствующее время.
import org.springframework.security.config.BeanIds;
import org.springframework.security.ldap.server.ApacheDSContainer;
...
public void onFileChange(File file) {
ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
if (ctx == null)
return;
ApacheDSContainer container = (ApacheDSContainer)ctx.getBean(BeanIds.EMBEDDED_APACHE_DS);
if (container != null) {
try {
container.destroy();
container.afterPropertiesSet();
}
catch(Exception exec) {
// handle error
}
}
}