简体   繁体   中英

getPassword() returns null in spring security 3.1 with ldap although I set erase-credentials=“false”

I need to get the username and password for the logged user to use these credentials to reconnect to another product.

Here is my SecurityContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<security:http auto-config="false" use-expressions="true"
    access-denied-page="/app/accessDenied">

    <security:intercept-url pattern="/login"
        access="permitAll" />
    <security:intercept-url pattern="/pages/home.xhtml"
        access="isAuthenticated() and hasAnyRole('ROLE_ARCH_ADMIN','ROLE_ARCH_Data_ENTRY','ROLE_ARCH_VIEWER','ROLE_ARCH_SUPER_VIEWER','ROLE_ARCH_MANAGER')" />
    <security:intercept-url pattern="/pages/realtyDoc/realtyDoc.xhtml"
        access="isAuthenticated() and hasAnyRole('ROLE_ARCH_ADMIN','ROLE_ARCH_DATA_ENTRY','ROLE_ARCH_VIEWER','ROLE_ARCH_SUPER_VIEWER','ROLE_ARCH_MANAGER')" />

    <security:form-login login-page="/login1.xhtml"
        login-processing-url="/processLogin" authentication-failure-url="/login1.xhtml?faces-redirect=true"
        authentication-success-handler-ref="successHandler" />

    <security:logout invalidate-session="true" logout-url="/j_spring_security_logout"
        logout-success-url="/login1.xhtml" />
</security:http>

<bean id="contextSource"
    class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
    <constructor-arg value="ldap://PDA-FNTEST:389/" />
    <property name="userDn" value="user@PD.LOCAL" />
    <property name="password" value="password" />
</bean>

<bean id="userSearch"
    class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
    <constructor-arg index="0" value="DC=PD,DC=LOCAL" />
    <constructor-arg index="1" value="(sAMAccountName={0})" />
    <constructor-arg index="2" ref="contextSource" />
</bean>

<bean id="bindAuthenticator"
    class="org.springframework.security.ldap.authentication.BindAuthenticator">
    <constructor-arg ref="contextSource" />
    <property name="userSearch" ref="userSearch" />
</bean>

<bean id="ldapAuthoritiesPopulator"
    class="org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator">
    <constructor-arg ref="contextSource" />
    <constructor-arg value="OU=ARCH_USERS_OU,DC=PD,DC=LOCAL" />
</bean>

<bean id="ldapAuthProvider"
    class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
    <constructor-arg index="0" ref="bindAuthenticator" />
    <constructor-arg index="1" ref="ldapAuthoritiesPopulator" />
</bean>

<security:authentication-manager erase-credentials="false" >
    <security:authentication-provider
        ref="ldapAuthProvider" />
</security:authentication-manager>

<bean id="successHandler" class="com.eblacorp.archive.security.LoginSuccessHandler">
    <property name="userAccessService" ref="userAccessService" />
</bean>
<bean id="loginFailuerHandler" class="com.eblacorp.archive.security.LoginFailuerHandler" />
</beans>

When I use this code to print username and password, the password shows as null.

UserDetails user = (UserDetails) SecurityContextHolder.getContext()
            .getAuthentication().getPrincipal();

    System.out.println("username: " + user.getUsername());
    System.out.println("password: " + user.getPassword());

I set erase-credentials="false" in authentication manager as:

<security:authentication-manager erase-credentials="false" >
    <security:authentication-provider
        ref="ldapAuthProvider" />
</security:authentication-manager>

To get password value use getCredentials().

  • Object getCredentials()

The credentials that prove the principal is correct. This is usually a password, but could be anything relevant to the AuthenticationManager. Callers are expected to populate the credentials.

Returns: the credentials that prove the identity of the Principal

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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