答案 0 :(得分:1)
如果你想改变记住我的工作方式,你需要扩展AbstractRememberMeServices
或TokenBasedRememberMeServices
(基于你想要做的事情)。
然后,您需要通过配置xml文件中remember-me指令的services-ref
指令将Spring指向您的Remember Me实现:
<remember-me services-ref="sword101CustomRememberMeServices" key="{GUID}" token-validity-seconds="3600" data-source-ref="dataSource"/>
如果您对设置和目标有所了解,我可能会提供更详细的帮助。
捐赠
答案 1 :(得分:0)
我假设您使用自定义表单但仍然调用spring登录URL(j_spring_security_check)。
为什么不在自定义登录表单上使用RememberMe字段(_spring_security_remember_me)?
答案 2 :(得分:0)
以下是在手动授权期间您可以手动执行 RememberMe 的方法,
首先,您将用户保存在安全上下文
中@Autowired
RememberMeServices rememberMeServices;
@RequestMapping(value = "/login")
public String login(HttpServletRequest request,HttpServletResponse response) {
Authentication auth = new UsernamePasswordAuthenticationToken(currentUser, null, currentUser.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(auth);
rememberMeServices.loginSuccess(request, response, auth);
}
这是配置
@Configuration
...
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Autowired
DataSource dataSource;
@Bean
public RememberMeServices rememberMeServices() {
// TokenBasedRememberMeServices if you don't want to persist token
// return new TokenBasedRememberMeServices("key", userDetailsService);
PersistentTokenBasedRememberMeServices rememberMeServices =
new PersistentTokenBasedRememberMeServices("key", userDetailsService, persistentTokenRepository());
rememberMeServices.setAlwaysRemember(true);
return rememberMeServices;
}
@Bean
public PersistentTokenRepository persistentTokenRepository() {
JdbcTokenRepositoryImpl db = new JdbcTokenRepositoryImpl();
db.setDataSource(dataSource);
return db;
}