简体   繁体   English

在Spring的运行时切换LDAP连接

[英]Switch LDAP connection at runtime in Spring

I am new to spring. 我是春天的新手。 Admins of my spring based web app want to configure settings from the web interface, so users can authenticate against LDAP server with their company username and password. 我基于Spring的Web应用程序的管理员希望通过Web界面配置设置,因此用户可以使用其公司用户名和密码针对LDAP服务器进行身份验证。

Change in LDAP settings should be possible without restarting the application. 无需重新启动应用程序,就应该可以更改LDAP设置。 This might happen during a 'migration' or whatever reason. 这可能在“迁移”期间或任何原因下发生。 I have a couple beans, which need to be refreshed after the admin saves new settings for the LDAP server: 我有几个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>

I am using Springframework 3.1.2. 我正在使用Springframework 3.1.2。 The problem is, there are constructor arguments, which I want to change and not affect other running jobs. 问题是,有一些构造函数参数,我想更改它们而不影响其他正在运行的作业。 I tried playing with Scoped proxy, but not to much success yet: 我尝试使用Scoped代理,但未取得太大成功:

<bean id="ldapServer" scope="prototype" ...>
    <aop:scoped-proxy/>

I was successful though to get ldapServer to reinstantiate, when using prototype scope by running this piece of code: 通过运行以下代码在使用原型范围时,我成功地使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'));
    }
    ...
}

Are scopes and proxies here the way to go, or is the another mechanism in Spring to reflect configuration changes into a running program instance? 是作用域和代理在这里,还是Spring中将配置更改反映到正在运行的程序实例中的另一种机制?

UPDATE: Clear up the question. 更新:清除问题。 UPDATE: The root problem with the AOP proxies was following root exception: 更新:AOP代理的根本问题是根异常之后:

java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given

What worked was adding proxy-target-class="false" attribute to the <aop:scoped-proxy/> tag. 有效的方法是将proxy-target-class="false"属性添加到<aop:scoped-proxy/>标记。 I created a new scope, which works better than prototype - It destroys beans on settings update. 我创建了一个新的范围,该范围比原型的效果更好-它会在设置更新时破坏bean。 Now I have this in my beans.xml: 现在,在我的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>

I also have a controller for LDAP settings into which I inject ldapScope and I call a method which destroys current life-cycle objects and starts a new life-cycle every time, user presses the apply button. 我还有一个用于LDAP设置的控制器,我将ldapScope注入其中,并调用了一种方法,该方法每次用户按下Apply按钮时都会破坏当前的生命周期对象并启动一个新的生命周期。

PS: Not sure if I handle the life-cycle "re-start" in the right way - people my way to look for auto-start beans and start them after such event happens (ie: Setting -> Apply) PS:不确定我是否以正确的方式处理了生命周期“重新启动”-人们以这种方式寻找自动启动bean并在此类事件发生后启动它们(例如:设置->应用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM