Defer Tomcat’s session ID generator initialization until it’s needed
By default, Tomcat forces the generation of a session id during startup to ensure that a SecureRandom instance has been initialized. When there is a lack of entropy (as is often the case on a newly booted VPS, for example) this can block for a long time (several minutes in some cases) causing users to incorrectly believe that their application has hung during startup. This is particularly problematic for applications that don't use HTTP sessions as they are paying the startup cost for no benefit. This commit address the problem by configuring a custom SessionIdGenerator that does not initialize itself during startup. Instead, the initialization is now deferred until a request for a session id is made. Closes gh-6174
This commit is contained in:
parent
bce6bd6594
commit
f0ce0e3e72
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Copyright 2012-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
|
||||
*
|
||||
* http://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.boot.context.embedded.tomcat;
|
||||
|
||||
import org.apache.catalina.LifecycleException;
|
||||
import org.apache.catalina.LifecycleState;
|
||||
import org.apache.catalina.util.StandardSessionIdGenerator;
|
||||
|
||||
/**
|
||||
* A specialization of {@link StandardSessionIdGenerator} that initializes
|
||||
* {@code SecureRandom} lazily.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
class LazySessionIdGenerator extends StandardSessionIdGenerator {
|
||||
|
||||
@Override
|
||||
protected void startInternal() throws LifecycleException {
|
||||
setState(LifecycleState.STARTING);
|
||||
}
|
||||
|
||||
}
|
|
@ -437,6 +437,7 @@ public class TomcatEmbeddedServletContainerFactory
|
|||
else {
|
||||
context.addLifecycleListener(new DisablePersistSessionListener());
|
||||
}
|
||||
context.addLifecycleListener(new LazySessionIdGeneratorListener());
|
||||
}
|
||||
|
||||
private void configurePersistSession(Manager manager) {
|
||||
|
@ -807,4 +808,19 @@ public class TomcatEmbeddedServletContainerFactory
|
|||
|
||||
}
|
||||
|
||||
private static class LazySessionIdGeneratorListener implements LifecycleListener {
|
||||
|
||||
@Override
|
||||
public void lifecycleEvent(LifecycleEvent event) {
|
||||
if (event.getType().equals(Lifecycle.START_EVENT)) {
|
||||
Context context = (Context) event.getLifecycle();
|
||||
Manager manager = context.getManager();
|
||||
if (manager != null) {
|
||||
manager.setSessionIdGenerator(new LazySessionIdGenerator());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue