[英]Switch LDAP connection at runtime in Spring
我是春天的新手。 我基於Spring的Web應用程序的管理員希望通過Web界面配置設置,因此用戶可以使用其公司用戶名和密碼針對LDAP服務器進行身份驗證。
無需重新啟動應用程序,就應該可以更改LDAP設置。 這可能在“遷移”期間或任何原因下發生。 我有幾個bean,在管理員保存LDAP服務器的新設置后需要刷新:
<bean id="ldapServer" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
<constructor-arg>
<list>
<value>${ldap.url1}</value>
...
</list>
</constructor-arg>
<constructor-arg value="${ldap.basedn}"</constructor-arg>
<property name="referral" value="${ldap.referral}" />
<property name="baseEnvironmentProperties">...</property>
<property name="userDn" value="${ldap.username}" />
<property name="password" value="${ldap.password}" />
</bean>
我正在使用Springframework 3.1.2。 問題是,有一些構造函數參數,我想更改它們而不影響其他正在運行的作業。 我嘗試使用Scoped代理,但未取得太大成功:
<bean id="ldapServer" scope="prototype" ...>
<aop:scoped-proxy/>
通過運行以下代碼在使用原型范圍時,我成功地使ldapServer重新實例化:
@Controller
public class LDAPSettingsController implements ApplicationContextAware {
public ModelAndView handleRequest(...) {
DefaultSpringSecurityContextSource ldap;
ldap = context.getParentBeanFactor().getBean("ldapServer");
System.out.println(ldap.hashCode());
return new ModelAndView(new RedirectView('login.jsp'));
}
...
}
是作用域和代理在這里,還是Spring中將配置更改反映到正在運行的程序實例中的另一種機制?
更新:清除問題。 更新:AOP代理的根本問題是根異常之后:
java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
有效的方法是將proxy-target-class="false"
屬性添加到<aop:scoped-proxy/>
標記。 我創建了一個新的范圍,該范圍比原型的效果更好-它會在設置更新時破壞bean。 現在,在我的beans.xml中有這個:
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="ldap">
<ref bean="ldapScope" />
</entry>
</map>
</property>
</bean>
<bean id="ldapScope" class="com.myapp.SettingsScope" />
<bean id="ldapServer" scope="ldap" ...>
<aop:scoped-proxy proxy-target-class="false"/>
<constructor-args>
<list><value>${ldap.url1}</value> .. </list>
</constructor-args>
...
</bean>
我還有一個用於LDAP設置的控制器,我將ldapScope注入其中,並調用了一種方法,該方法每次用戶按下Apply按鈕時都會破壞當前的生命周期對象並啟動一個新的生命周期。
PS:不確定我是否以正確的方式處理了生命周期“重新啟動”-人們以這種方式尋找自動啟動bean並在此類事件發生后啟動它們(例如:設置->應用)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.