How to get bean properties from security-context.xml and use them in java class?

时间:2017-06-15 10:29:00

标签: xml spring security ldap

I need to pass the properties of my bean in the security-context.xml and use it in the LDAP server configurations, passing only the variable and not writing the configuration data directly, so only the variables need to be changed from the security context. Xml in case of modifications.

This is my security-context bean

<beans:bean id="myUserDetailsService" class="com.tilab.ngasp.policy.PolicyManager">
    <beans:property name="UrlLDAP" value="ldap://localhost:389" />
    <beans:property name="Principal" value="cn=Manager,dc=maxcrc,dc=com" />
    <beans:property name="Credential" value="secret" />
    <beans:property name="InitialContext" value="com.sun.jndi.ldap.LdapCtxFactory" />
</beans:bean>

And this is the part of java class when i have to configure the LDAP server without insert configuration informations but only pass the variables with beans property

@SuppressWarnings("unchecked")
public NgaspUsers findUser(String user)
{
    NgaspUsers utente = null;       

    Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389");
    env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=maxcrc,dc=com");
    env.put(Context.SECURITY_CREDENTIALS, "secret");

I want a DLAP Config like this :

env.put(Context.INITIAL_CONTEXT_FACTORY,StringVariableWithContextFactory);
env.put(Context.PROVIDER_URL, StringVariableWithLdapUrl);
env.put(Context.SECURITY_PRINCIPAL, StringVariableWithRootInformation);
env.put(Context.SECURITY_CREDENTIALS,StringVariableWithPassword);

I'm using Spring Security, Eclipse and Tomcat 6.0.

Thank's all, hope this informations is sufficient for helping me!

--- EDIT FOR Messaoud GUERNOUTI

I tried using your last method to also use the external class but I have this error in env.put (Context.INITIAL_CONTEXT_FACTORY, myUserDetailsService.getInitialContext ());

Error : The method getInitialContext() is undefined for the type PolicyManager

PolicyManager.java now is so right now :

@SuppressWarnings("unchecked")
@Autowired
@Qualifier("myUserDetailsService")
PolicyManager myUserDetailsService;
public NgaspUsers findUser(String user)
{
    NgaspUsers utente = null;       

    Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY,myUserDetailsService.getInitialContext()); 
    env.put(Context.PROVIDER_URL, "ldap://localhost:389");
    env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=maxcrc,dc=com");
    env.put(Context.SECURITY_CREDENTIALS, "secret");

I just tried the first "put" but I already have this problem, did I correctly insert the annotations in the right place or is there any other problem?

EDIT 2 :

Basics Getters and setters like this or need to extract the value in xml file?

public String InitialContext1;
public String Credential1;
public String UrlLDAP1;
public String Principal1;


public String getInitialContext1() {
    return InitialContext1;
}

public void setInitialContext1(String initialContext) {
    InitialContext1 = initialContext;
}

public String getCredential1() {
    return Credential1;
}

public void setCredential1(String credential) {
    Credential1 = credential;
}

public String getUrlLDAP1() {
    return UrlLDAP1;
}

public void setUrlLDAP1(String urlLDAP) {
    UrlLDAP1 = urlLDAP;
}

public String getPrincipal1() {
    return Principal1;
}

public void setPrincipal1(String principal) {
    Principal1 = principal;
}

1 个答案:

答案 0 :(得分:3)

为什么不使用Spring Security LDAP默认Provider进行身份验证:

<sec:authentication-manager>
    <sec:authentication-provider ref="ldapAuthProvider" />
</sec:authentication-manager>

<bean id="contextSource"
        class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
        <constructor-arg value="ldap://localhost:389/cn=Manager,dc=maxcrc,dc=com" />
        <property name="userDn" value="user" />
        <property name="password" value="pass" />
    </bean>

    <bean id="userSearch"
        class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
        <constructor-arg index="0" value="" />
        <constructor-arg index="1" value="(sAMAccountName={0})" />
        <constructor-arg index="2" ref="contextSource" />
        <property name="searchSubtree" value="true" />
    </bean>

    <bean id="ldapAuthProvider"
        class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
        <constructor-arg>
            <bean
                class="org.springframework.security.ldap.authentication.BindAuthenticator">
                <constructor-arg ref="contextSource" />
                <property name="userSearch" ref="userSearch" />
            </bean>
        </constructor-arg>
        <constructor-arg>
            <bean
                class="org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator">
                <constructor-arg ref="contextSource" />
                <constructor-arg value="" />
                <property name="defaultRole" value="ROLE_USER"/>
                <property name="searchSubtree" value="true" />
                <property name="ignorePartialResultException" value="true" />
            </bean>
        </constructor-arg>
    </bean>

如果您想使用自己的课程,可以添加

@Autowired
@Qualifier("myUserDetailsService") 
PolicyManager myUserDetailsService;

然后通过示例访问getter的所有属性:

Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY,myUserDetailsService.getInitialContext()); 

您必须生成getter&amp;对{class 1}}

上的所有属性setter