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

半腔热情 提交于 2019-12-01 11:29:42

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!