简体   繁体   中英

How do I use “Remember Me” authentication with Spring Security and LDAP?

I want to use a Spring Security's "Remember me" with LDAP authentication. LDAP authentication configuration is described here , I've just made some tiny changes. Could you explain to me how can i add "Remember me" in that configuration? Or, may be, you can give me a sample which describes how to do it. Thank you.

You really just need to give the remember-me attribute a data-source-ref or a token-repository-ref and a user-service-ref. I saw some other examples that used a voter based access-decision-manager-ref in the http element, but that seemed to void the use-expressions="true." The only thing I don't like about this is having to specify the ldap properties twice.

<beans:import resource="datasource-context.xml"/>

<http use-expressions="true" >
    <intercept-url pattern="/auth/**" access="permitAll" />
    <intercept-url pattern="/admin/**" access="hasRole('MY_ROLE_ADMIN')" />
    <intercept-url pattern="/**" access="isAuthenticated()" />
    <form-login  />
    <logout  />
    <remember-me key="_my_remember_me_key" 
        token-validity-seconds="864000" 
        data-source-ref="dataSource"
        user-service-ref="ldapUserService" />
</http>

<ldap-server id="ldapServerContext" ldif="classpath:users.ldif" root="dc=springframework,dc=org" port="33389" />

<ldap-user-service 
    id="ldapUserService" 
    server-ref="ldapServerContext" 
    user-search-base="ou=people"
    user-search-filter="(uid={0})"
    group-search-base="ou=groups"
    group-role-attribute="cn"
    group-search-filter="(member={0})"
    role-prefix="MY_ROLE_" />

<authentication-manager>
    <ldap-authentication-provider
        server-ref="ldapServerContext"
        user-search-base="ou=people"
        user-search-filter="(uid={0})"
        group-search-base="ou=groups"
        group-role-attribute="cn"
        group-search-filter="(member={0})"
        role-prefix="MY_ROLE_" />
</authentication-manager>

This link should help:

http://static.springsource.org/spring-security/site/docs/3.0.x/reference/remember-me.html

Important note:

If you are using an authentication provider which doesn't use a UserDetailsService ( for example, the LDAP provider ) then it won't work unless you also have a UserDetailsService bean in your application context.

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