我使用Spring和Session来控制使用ReactJS作为前端的应用程序。我的问题很简单,我尝试了几种方法来处理,但没有成功。
React部分在登录后使用AJAX调用Spring服务(我也使用Security ),这至少30分钟令人惊讶。在此之后,会话就死了,所有调用都接收以登录页作为响应的302。这是意料之中的。
但我的问题是:有什么更好的方式来扩大一些后端的生存时间(超过30分钟的默认时间)?
// Gradle portion
compile('org.springframework.boot:spring-boot-devtools')
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-aop')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.security:spring-security-test:4.1.1.RELEASE')
// Cache configuration - JDBC
compile('org.springframework.session:spring-session:1.2.2.RELEASE')
compile('org.springframework.session:spring-session-jdbc:1.2.2.RELEASE')
compile('org.springframework.boot:spring-boot-starter-jdbc')
我习惯于补充:
// A 24 hours long session
server.session.timeout = 86400
这样,我就可以在SPRING_SESSION表上看到MAX_INACTIVE_INTERVAL = 86400存储的会话。一切看起来都很好..。只有30分钟。在第31分钟,我尝试单击另一个触发AJAX调用的页面,我将接收302到登录页面作为响应。
我使用另一种方法获得了完全相同的行为,通过Java设置了8月的成功:
@Component
public class AuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
@Value("${server.session.timeout}")
private String defaultSessionTimeoutInSeconds;
@Override
public void onAuthenticationSuccess(
HttpServletRequest request,
HttpServletResponse response,
Authentication authentication) throws ServletException, IOException {
request.getSession().setMaxInactiveInterval(Integer.parseInt(defaultSessionTimeoutInSeconds));
super.onAuthenticationSuccess(request, response, authentication);
}
}
当然,我能够在数据库存储的会话上验证我的编号,但是30分钟后,会话再次被删除。
那么,真正将Spring会话超时扩展到超过30分钟的正确方法是什么?既然MAX_INACTIVE_INTERVAL没有做我认为应该做的事情,那么正确的方法是什么?
我能够使用任何lib的最新版本。
PS:当我的AJAX调用(基于JQuery的)也接收到/login重定向到回退情况时,我可以考虑另一种解决方案来重定向整个浏览器。
提前谢谢。
更新:
我尝试了以下几点:
添加属性-> server.session.cookie.max-age= 777777 security.sessions=never
这种行为没有任何改变。我可以在JdbcOperationsSessionRepository#cleanUpExpiredSessions:上看到调试
@Scheduled(cron = "0 * * * * *")
public void cleanUpExpiredSessions() {
long now = System.currentTimeMillis();
long maxInactiveIntervalSeconds = (this.defaultMaxInactiveInterval != null)
? this.defaultMaxInactiveInterval
: MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS;
final long sessionsValidFromTime = now - (maxInactiveIntervalSeconds * 1000);
this.defaultMaxInactiveInterval
仍然总是填充"1800“,这意味着30分钟来杀死所有的会话。
这是预期的行为,由评论:
我还在努力把这个持续的默认值1800改为更大的.:)
更新2
仔细查看代码,在我的示例中,当JdbcOperationsSessionRepository被实例化时,它是由JdbcHttpSessionConfiguration#sessionRepository
创建的
具体而言:
@Bean
public JdbcOperationsSessionRepository sessionRepository(
@Qualifier("springSessionJdbcOperations") JdbcOperations jdbcOperations,
PlatformTransactionManager transactionManager) {
JdbcOperationsSessionRepository sessionRepository =
new JdbcOperationsSessionRepository(jdbcOperations, transactionManager);
String tableName = getTableName();
if (StringUtils.hasText(tableName)) {
sessionRepository.setTableName(tableName);
}
sessionRepository
.setDefaultMaxInactiveInterval(this.maxInactiveIntervalInSeconds); // Always 1800 (private Integer maxInactiveIntervalInSeconds = 1800;)
if (this.lobHandler != null) {
sessionRepository.setLobHandler(this.lobHandler);
}
if (this.springSessionConversionService != null) {
sessionRepository.setConversionService(this.springSessionConversionService);
}
else if (this.conversionService != null) {
sessionRepository.setConversionService(this.conversionService);
}
else if (deserializingConverterSupportsCustomClassLoader()) {
GenericConversionService conversionService = createConversionServiceWithBeanClassLoader();
sessionRepository.setConversionService(conversionService);
}
return sessionRepository;
}
我没有找到任何明确的选择来推翻这个美丽的。
更新3
在注释之后,我只能将注释配置为:
import org.springframework.session.jdbc.config.annotation.web.http.EnableJdbcHttpSession;
@EnableJdbcHttpSession(tableName="MYSCHEMA.SPRING_SESSION", maxInactiveIntervalInSeconds = 86400)
public class HttpSessionConfig {
}
这样,我就能够用定义的MAX_INACTIVE_INTERVAL = 86400来存储会话。
但是,如果我在新会话中保留相关的SPRING_SECURITY_CONTEXT (SPRING_SESSION_ATTRIBUTES表)信息,则整个会话和属性将在30分钟后删除。
在一个疯狂的测试中,我做了一个登录,删除了我的会话的SPRING_SECURITY_CONTEXT属性,会话仍然在那里.
默认的会话清洁器是正确的,而不是这里的违法者。
2016-10-04 12:18:02,081 8808479 [pool-1-thread-1] INFO d.s.t.s.ScheduledCacheRefresher - Checking refreshable caches now.
2016-10-04 12:19:00,001 8866399 [pool-1-thread-1] DEBUG o.s.s.j.JdbcOperationsSessionRepository - Cleaning up sessions older than Mon Oct 03 12:19:00 BRT 2016
2016-10-04 12:19:02,050 8868448 [pool-1-thread-1] DEBUG o.s.s.j.JdbcOperationsSessionRepository - Cleaned up 0 expired sessions
2016-10-04 12:19:02,051 8868449 [pool-1-thread-1] INFO d.s.t.s.ScheduledCacheRefresher - Checking refreshable caches now.
2016-10-04 12:20:00,001 8926399 [pool-1-thread-1] INFO d.s.t.s.ScheduledCacheRefresher - Checking refreshable caches now.
2016-10-04 12:20:00,003 8926401 [pool-1-thread-1] DEBUG o.s.s.j.JdbcOperationsSessionRepository - Cleaning up sessions older than Mon Oct 03 12:20:00 BRT 2016
2016-10-04 12:20:02,063 8928461 [pool-1-thread-1] DEBUG o.s.s.j.JdbcOperationsSessionRepository - Cleaned up 0 expired sessions
日志中没有显示对他们的删除。
因此,检查SPRING_SECURITY_CONTEXT的其他任何东西仍然有大约30分钟的默认超时时间,并且它会触发整个会话无效。
我正在尝试添加更多的断点来解决这个问题。:)
发布于 2021-07-12 17:16:53
您必须在您的server.session.timeout
文件中设置application.properties
。参考本文档:服务器属性
https://stackoverflow.com/questions/39802031
复制相似问题