gpt4 book ai didi

Spring 安全性:记住我 token 仅工作一次

转载 作者:行者123 更新时间:2023-12-03 23:37:03 29 4
gpt4 key购买 nike

我在Spring安全方面遇到了一个非常奇怪的问题。

“记住我” token 似乎只能持续一次自动登录,此后,它将停止工作。

1.登录后:

enter image description here

2.然后,我手动删除JSESSIONID cookie并重新加载页面

enter image description here

3.我再次删除JSESSIONID cookie,然后再次重新加载页面。

现在,我已注销!

在控制台中,我得到以下信息:

SEVERE [http-nio-8080-exec-10] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [dispatcher] in context with path [] threw exception
org.springframework.security.web.authentication.rememberme.CookieTheftException: Invalid remember-me token (Series/token) mismatch. Implies previous cookie theft attack.

我读到这可能是浏览器同时发出多个请求的结果,我检查了一下(禁用了所有资源,只保留了纯HTML,但无济于事)

enter image description here

这是我的配置
@EnableWebSecurity
public class Security extends WebSecurityConfigurerAdapter {

@Autowired
private CustomUserDetailsService customUserDetailsService;

@Autowired
DataSource dataSource;

@Override
protected void configure(HttpSecurity http) throws Exception {

http.authorizeRequests().antMatchers("/assets/**").permitAll();
http.authorizeRequests().anyRequest().authenticated();

http.formLogin().permitAll();
http.rememberMe().tokenRepository(persistentTokenRepository()).userDetailsService(customUserDetailsService);

http.logout().permitAll();
}

@Bean
public PersistentTokenRepository persistentTokenRepository() {
JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
tokenRepository.setDataSource(dataSource);
return tokenRepository;
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider());
}

@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
authProvider.setUserDetailsService(customUserDetailsService);
authProvider.setPasswordEncoder(passwordEncoder());
return authProvider;
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(11);
}
}

最佳答案

从配置中拉取dataSource对我有用,尝试一下

@Autowired
JpaConfiguration jpaConfig;

@Bean(name = "persistentTokenRepository")
public PersistentTokenRepository persistentTokenRepository() {
JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
tokenRepository.setDataSource(jpaConfig.dataSource());
return tokenRepository;
}

或者您也可以尝试提高 token 的有效性
 @Override
protected void configure(HttpSecurity http) throws Exception {

http.authorizeRequests().antMatchers("/assets/**").permitAll();
http.authorizeRequests().anyRequest().authenticated();

http.formLogin().permitAll();
http.rememberMe().tokenRepository(persistentTokenRepository()).userDetailsService(customUserDetailsService)
.tokenValiditySeconds(1209600);

http.logout().permitAll();
}

关于 Spring 安全性:记住我 token 仅工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49804331/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com