Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ out
*.rdb
.checkstyle
!etc/eclipse/.checkstyle
gradle/
!**/src/**/build
.DS_Store
spring-session-docs/package-lock.json
Expand Down
21 changes: 21 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ buildscript {

plugins {
id "com.github.ben-manes.versions"
id 'maven-publish'
}

apply plugin: 'io.spring.convention.root'
Expand Down Expand Up @@ -66,3 +67,23 @@ springRelease {
apiDocUrl = "https://docs.spring.io/spring-session/reference/{version}/api/java/index.html"
replaceSnapshotVersionInReferenceDocUrl = true
}


configure(subprojects) {project -> {
apply plugin: 'maven-publish'

publishing {
repositories {
maven {
url = release_url
name = 'kyligence'
if (project.hasProperty('kyligenceRepoUsername')) {
credentials {
username "$kyligenceRepoUsername"
password "$kyligenceRepoPassword"
}
}
}
}
}
}}
6 changes: 5 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
org.gradle.jvmargs=-Xmx4g -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.parallel=true
version=3.5.6
version=3.5.6-kylin-r1-SNAPSHOT

group_id=org.springframework.session
artifact_id=spring-session-core
release_url=https://repo-ofs.kyligence.com/repository/maven-releases/
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
package org.springframework.session;

import java.io.Serializable;
import java.security.SecureRandom;
import java.time.Duration;
import java.time.Instant;
import java.util.Base64;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
import org.springframework.util.DigestUtils;

/**
* <p>
* A {@link Session} implementation that is backed by a {@link java.util.Map}. The
Expand Down Expand Up @@ -242,7 +247,25 @@ public int hashCode() {
}

private static String generateId() {
return UUID.randomUUID().toString();
String id;
if (SpringHttpSessionConfiguration.secureRandomCreateEnabled) {
byte[] salt1 = new byte[36];
byte[] salt2 = new byte[36];
SecureRandom secureRandom = new SecureRandom();
secureRandom.setSeed(System.currentTimeMillis());
secureRandom.nextBytes(salt1);
secureRandom.nextBytes(salt2);
id = DigestUtils.md5DigestAsHex(salt1) + DigestUtils.md5DigestAsHex(salt2);
}
else {
id = UUID.randomUUID().toString();
}

if (SpringHttpSessionConfiguration.jdbcEncodeEnable) {
return new String(Base64.getEncoder().encode(id.getBytes()));
}

return id;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -106,11 +107,31 @@ public class SpringHttpSessionConfiguration implements InitializingBean, Applica

private List<HttpSessionListener> httpSessionListeners = new ArrayList<>();

@Value("${kylin.web.session-skip-header-name:Auto}")
private String skipUpdateSessionHeaderName;

@Value("${spring.session.timeout:-1}")
private int sessionTimeout;

public static boolean secureRandomCreateEnabled;

@Override
public void afterPropertiesSet() {
this.defaultHttpSessionIdResolver.setCookieSerializer(getCookieSerializer());
}

@Value("${kylin.web.session.secure-random-create-enabled:false}")
public void setSecureRandomCreateEnabled(boolean enabled) {
secureRandomCreateEnabled = enabled;
}

public static boolean jdbcEncodeEnable;

@Value("${kylin.web.session.jdbc-encode-enabled:false}")
public void setJdbcEncodeEnable(boolean enabled) {
jdbcEncodeEnable = enabled;
}

@Bean
public SessionEventHttpSessionListenerAdapter sessionEventHttpSessionListenerAdapter() {
return new SessionEventHttpSessionListenerAdapter(this.httpSessionListeners);
Expand All @@ -121,6 +142,8 @@ public <S extends Session> SessionRepositoryFilter<? extends Session> springSess
SessionRepository<S> sessionRepository) {
SessionRepositoryFilter<S> sessionRepositoryFilter = new SessionRepositoryFilter<>(sessionRepository);
sessionRepositoryFilter.setHttpSessionIdResolver(this.httpSessionIdResolver);
sessionRepositoryFilter.setSessionTimeout(this.sessionTimeout);
sessionRepositoryFilter.setSkipCommitSessionHeaderName(this.skipUpdateSessionHeaderName);
return sessionRepositoryFilter;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.session.web.http;

public class SaveSessionException extends RuntimeException {

public SaveSessionException() {
super();
}

public SaveSessionException(String s) {
super(s);
}

public SaveSessionException(String message, Throwable cause) {
super(message, cause);
}

public SaveSessionException(Throwable cause) {
super(cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.session.web.http;

import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.List;

Expand Down Expand Up @@ -105,6 +106,18 @@ public class SessionRepositoryFilter<S extends Session> extends OncePerRequestFi

private HttpSessionIdResolver httpSessionIdResolver = new CookieHttpSessionIdResolver();

private String skipCommitSessionHeaderName = "Auto";

public void setSkipCommitSessionHeaderName(String skipCommitSessionHeaderName) {
this.skipCommitSessionHeaderName = skipCommitSessionHeaderName;
}

private int sessionTimeout = -1;

public void setSessionTimeout(int sessionTimeout) {
this.sessionTimeout = sessionTimeout;
}

/**
* Creates a new instance.
* @param sessionRepository the <code>SessionRepository</code> to use. Cannot be null.
Expand Down Expand Up @@ -223,10 +236,30 @@ private void commitSession() {
}
}
else {
if (Boolean.valueOf(this.getHeader(SessionRepositoryFilter.this.skipCommitSessionHeaderName))) {
return;
}

S session = wrappedSession.getSession();
String requestedSessionId = getRequestedSessionId();
clearRequestedSessionCache();
SessionRepositoryFilter.this.sessionRepository.save(session);
try {
S sourceSession = SessionRepositoryFilter.this.sessionRepository.findById(session.getId());

if (null == sourceSession) {
SessionRepositoryFilter.this.sessionRepository.save(session);
}
else {
long halfExpireMillis = sourceSession.getLastAccessedTime().toEpochMilli()
+ session.getMaxInactiveInterval().toMillis() / 2;
if (System.currentTimeMillis() > halfExpireMillis) {
SessionRepositoryFilter.this.sessionRepository.save(session);
}
}
}
catch (Exception ex) {
throw new SaveSessionException("Failed to save session!", ex);
}
String sessionId = session.getId();
if (!isRequestedSessionIdValid() || !sessionId.equals(requestedSessionId)) {
SessionRepositoryFilter.this.httpSessionIdResolver.setSessionId(this, this.response, sessionId);
Expand Down Expand Up @@ -317,6 +350,12 @@ public HttpSessionWrapper getSession(boolean create) {
new RuntimeException("For debugging purposes only (not an error)"));
}
S session = SessionRepositoryFilter.this.sessionRepository.createSession();
if (SessionRepositoryFilter.this.sessionTimeout > 60) {
session.setMaxInactiveInterval(Duration.ofSeconds(SessionRepositoryFilter.this.sessionTimeout));
}
else if (SessionRepositoryFilter.this.sessionTimeout > 0) {
session.setMaxInactiveInterval(Duration.ofSeconds(60));
}
session.setLastAccessedTime(Instant.now());
currentSession = new HttpSessionWrapper(session, getServletContext());
setCurrentSession(currentSession);
Expand Down
Loading
Loading