简单的实现remember-me
http节点中配置一个过滤器
<security:remember-me/>
验证管理器
<security:authentication-manager alias="authenticationManager" erase-credentials="false" >
。。。
</security:authentication-manager>
登录页面
<input type="checkbox" name="remember-me" value="true">
第二种方式
自定义过滤器实现remember-me
<security:custom-filter ref="rememberMeFilter" position="REMEMBER_ME_FILTER"/> 替换原来的 <security:remember-me/>
<!--remember-me拦截器-->
<bean id="rememberMeFilter" class="org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter">
<constructor-arg index="0" ref="authenticationManager" />
<constructor-arg index="1" ref="rememberMeServices" />
</bean>
<!-- <bean id="rememberMeServices" class="org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices">
<constructor-arg index="0" value="maomao" />
<constructor-arg index="1" ref="userDetailsServiceImpl" />
<property name="tokenValiditySeconds" value="604800"></property>
</bean> --> <!-- 默认的持久化方式,安全性比较差 -->
<bean id="rememberMeServices" class="org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices">
<constructor-arg index="0" value="maomao" />
<constructor-arg index="1" ref="userDetailsServiceImpl" />
<constructor-arg index="2">
<bean class="org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl">
<property name="dataSource" ref="dataSource"/>
<!--<property name="createTableOnStartup" value="true"/>--> <!-- 是否在系统启动时创建持久化token的数据库表 -->
</bean>
</constructor-arg>
<property name="tokenValiditySeconds" value="604800"/>
</bean>
还需要在验证管理器里增加remember-me验证器
<!--验证管理器-->
<security:authentication-manager alias="authenticationManager" erase-credentials="false" >
<security:authentication-provider ref="authenticationProvider"/>
<security:authentication-provider ref="rememberMeAuthenticationProvider"/>
</security:authentication-manager>
<!-- remember-me验证器 -->
<bean id="rememberMeAuthenticationProvider" class="org.springframework.security.authentication.RememberMeAuthenticationProvider">
<constructor-arg index="0" value="maomao" />
</bean>
完整源码下载
https://github.com/skyrocks/a-spring