設置application.properties
#設置過期時間6小時
server.servlet.session.timeout=21600s
於debug模式測試
session.getMaxInactiveInterval();
發現是預設時間,需增加SessionListener.java並實作HttpSessionListener
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Configuration
class SessionListener implements HttpSessionListener {
@Value("${server.servlet.session.timeout}")
private String session_timeout;
@Override
public void sessionCreated(HttpSessionEvent event) {
event.getSession().setMaxInactiveInterval(Integer.parseInt(session_timeout.replaceAll("[^\\d]", "")));
}
@Override
public void sessionDestroyed(HttpSessionEvent event) {}
}
再次檢查session.getMaxInactiveInterval(),就會是21600s