use of javax.servlet.SessionCookieConfig in project felix by apache.
the class JettyService method configureSessionManager.
private void configureSessionManager(final ServletContextHandler context) throws Exception {
final SessionHandler sessionHandler = context.getSessionHandler();
sessionHandler.setMaxInactiveInterval(this.config.getSessionTimeout() * 60);
sessionHandler.setSessionIdPathParameterName(this.config.getProperty(JettyConfig.FELIX_JETTY_SERVLET_SESSION_ID_PATH_PARAMETER_NAME, SessionHandler.__DefaultSessionIdPathParameterName));
sessionHandler.setCheckingRemoteSessionIdEncoding(this.config.getBooleanProperty(JettyConfig.FELIX_JETTY_SERVLET_CHECK_REMOTE_SESSION_ENCODING, true));
sessionHandler.setSessionTrackingModes(Collections.singleton(SessionTrackingMode.COOKIE));
final SessionCookieConfig cookieConfig = sessionHandler.getSessionCookieConfig();
cookieConfig.setName(this.config.getProperty(JettyConfig.FELIX_JETTY_SERVLET_SESSION_COOKIE_NAME, SessionHandler.__DefaultSessionCookie));
cookieConfig.setDomain(this.config.getProperty(JettyConfig.FELIX_JETTY_SERVLET_SESSION_DOMAIN, SessionHandler.__DefaultSessionDomain));
cookieConfig.setPath(this.config.getProperty(JettyConfig.FELIX_JETTY_SERVLET_SESSION_PATH, context.getContextPath()));
cookieConfig.setMaxAge(this.config.getIntProperty(JettyConfig.FELIX_JETTY_SERVLET_SESSION_MAX_AGE, -1));
cookieConfig.setHttpOnly(this.config.getBooleanProperty(JettyConfig.FELIX_JETTY_SESSION_COOKIE_HTTP_ONLY, true));
cookieConfig.setSecure(this.config.getBooleanProperty(JettyConfig.FELIX_JETTY_SESSION_COOKIE_SECURE, false));
}
use of javax.servlet.SessionCookieConfig in project Payara by payara.
the class Request method configureSessionCookie.
/**
* Configures the given JSESSIONID cookie.
*
* @param cookie The JSESSIONID cookie to be configured
*/
protected void configureSessionCookie(Cookie cookie) {
cookie.setHttpOnly(true);
cookie.setMaxAge(-1);
String contextPath = null;
// START GlassFish 1024
if (isDefaultContext) {
cookie.setPath("/");
} else {
// END GlassFish 1024
if (context != null) {
// START OF SJSAS 6231069
contextPath = context.getPath();
// END OF SJSAS 6231069
}
if (contextPath != null && contextPath.length() > 0) {
cookie.setPath(contextPath);
} else {
cookie.setPath("/");
}
// START GlassFish 1024
}
// END GlassFish 1024
if (isSecure()) {
cookie.setSecure(true);
}
// sessionCookieConfig
if (context != null) {
SessionCookieConfig sessionCookieConfig = context.getSessionCookieConfig();
if (sessionCookieConfig.getDomain() != null) {
cookie.setDomain(sessionCookieConfig.getDomain());
}
if (sessionCookieConfig.getPath() != null) {
cookie.setPath(sessionCookieConfig.getPath());
}
if (sessionCookieConfig.getComment() != null) {
cookie.setVersion(1);
cookie.setComment(sessionCookieConfig.getComment());
}
// do nothing if it is already secure
if (!cookie.getSecure()) {
cookie.setSecure(sessionCookieConfig.isSecure());
}
cookie.setHttpOnly(sessionCookieConfig.isHttpOnly());
cookie.setMaxAge(sessionCookieConfig.getMaxAge());
}
if (requestedSessionCookiePath != null) {
cookie.setPath(requestedSessionCookiePath);
}
}
use of javax.servlet.SessionCookieConfig in project tomcat70 by apache.
the class WebXml method configureContext.
/**
* Configure a {@link Context} using the stored web.xml representation.
*
* @param context The context to be configured
*/
public void configureContext(Context context) {
// As far as possible, process in alphabetical order so it is easy to
// check everything is present
// Some validation depends on correct public ID
context.setPublicId(publicId);
// Everything else in order
context.setEffectiveMajorVersion(getMajorVersion());
context.setEffectiveMinorVersion(getMinorVersion());
for (Entry<String, String> entry : contextParams.entrySet()) {
context.addParameter(entry.getKey(), entry.getValue());
}
context.setDisplayName(displayName);
context.setDistributable(distributable);
for (ContextLocalEjb ejbLocalRef : ejbLocalRefs.values()) {
context.getNamingResources().addLocalEjb(ejbLocalRef);
}
for (ContextEjb ejbRef : ejbRefs.values()) {
context.getNamingResources().addEjb(ejbRef);
}
for (ContextEnvironment environment : envEntries.values()) {
context.getNamingResources().addEnvironment(environment);
}
for (ErrorPage errorPage : errorPages.values()) {
context.addErrorPage(errorPage);
}
for (FilterDef filter : filters.values()) {
if (filter.getAsyncSupported() == null) {
filter.setAsyncSupported("false");
}
context.addFilterDef(filter);
}
for (FilterMap filterMap : filterMaps) {
context.addFilterMap(filterMap);
}
for (JspPropertyGroup jspPropertyGroup : jspPropertyGroups) {
JspPropertyGroupDescriptor descriptor = new ApplicationJspPropertyGroupDescriptor(jspPropertyGroup);
context.getJspConfigDescriptor().getJspPropertyGroups().add(descriptor);
}
for (String listener : listeners) {
context.addApplicationListener(listener);
}
for (Entry<String, String> entry : localeEncodingMappings.entrySet()) {
context.addLocaleEncodingMappingParameter(entry.getKey(), entry.getValue());
}
// Prevents IAE
if (loginConfig != null) {
context.setLoginConfig(loginConfig);
}
for (MessageDestinationRef mdr : messageDestinationRefs.values()) {
context.getNamingResources().addMessageDestinationRef(mdr);
}
// messageDestinations were ignored in Tomcat 6, so ignore here
context.setIgnoreAnnotations(metadataComplete);
for (Entry<String, String> entry : mimeMappings.entrySet()) {
context.addMimeMapping(entry.getKey(), entry.getValue());
}
// Name is just used for ordering
for (ContextResourceEnvRef resource : resourceEnvRefs.values()) {
context.getNamingResources().addResourceEnvRef(resource);
}
for (ContextResource resource : resourceRefs.values()) {
context.getNamingResources().addResource(resource);
}
for (SecurityConstraint constraint : securityConstraints) {
context.addConstraint(constraint);
}
for (String role : securityRoles) {
context.addSecurityRole(role);
}
for (ContextService service : serviceRefs.values()) {
context.getNamingResources().addService(service);
}
for (ServletDef servlet : servlets.values()) {
Wrapper wrapper = context.createWrapper();
if (servlet.getLoadOnStartup() != null) {
wrapper.setLoadOnStartup(servlet.getLoadOnStartup().intValue());
}
if (servlet.getEnabled() != null) {
wrapper.setEnabled(servlet.getEnabled().booleanValue());
}
wrapper.setName(servlet.getServletName());
Map<String, String> params = servlet.getParameterMap();
for (Entry<String, String> entry : params.entrySet()) {
wrapper.addInitParameter(entry.getKey(), entry.getValue());
}
wrapper.setRunAs(servlet.getRunAs());
Set<SecurityRoleRef> roleRefs = servlet.getSecurityRoleRefs();
for (SecurityRoleRef roleRef : roleRefs) {
wrapper.addSecurityReference(roleRef.getName(), roleRef.getLink());
}
wrapper.setServletClass(servlet.getServletClass());
MultipartDef multipartdef = servlet.getMultipartDef();
if (multipartdef != null) {
if (multipartdef.getMaxFileSize() != null && multipartdef.getMaxRequestSize() != null && multipartdef.getFileSizeThreshold() != null) {
wrapper.setMultipartConfigElement(new MultipartConfigElement(multipartdef.getLocation(), Long.parseLong(multipartdef.getMaxFileSize()), Long.parseLong(multipartdef.getMaxRequestSize()), Integer.parseInt(multipartdef.getFileSizeThreshold())));
} else {
wrapper.setMultipartConfigElement(new MultipartConfigElement(multipartdef.getLocation()));
}
}
if (servlet.getAsyncSupported() != null) {
wrapper.setAsyncSupported(servlet.getAsyncSupported().booleanValue());
}
wrapper.setOverridable(servlet.isOverridable());
context.addChild(wrapper);
}
for (Entry<String, String> entry : servletMappings.entrySet()) {
context.addServletMapping(entry.getKey(), entry.getValue());
}
if (sessionConfig != null) {
if (sessionConfig.getSessionTimeout() != null) {
context.setSessionTimeout(sessionConfig.getSessionTimeout().intValue());
}
SessionCookieConfig scc = context.getServletContext().getSessionCookieConfig();
scc.setName(sessionConfig.getCookieName());
scc.setDomain(sessionConfig.getCookieDomain());
scc.setPath(sessionConfig.getCookiePath());
scc.setComment(sessionConfig.getCookieComment());
if (sessionConfig.getCookieHttpOnly() != null) {
scc.setHttpOnly(sessionConfig.getCookieHttpOnly().booleanValue());
}
if (sessionConfig.getCookieSecure() != null) {
scc.setSecure(sessionConfig.getCookieSecure().booleanValue());
}
if (sessionConfig.getCookieMaxAge() != null) {
scc.setMaxAge(sessionConfig.getCookieMaxAge().intValue());
}
if (sessionConfig.getSessionTrackingModes().size() > 0) {
context.getServletContext().setSessionTrackingModes(sessionConfig.getSessionTrackingModes());
}
}
for (Entry<String, String> entry : taglibs.entrySet()) {
TaglibDescriptor descriptor = new ApplicationTaglibDescriptor(entry.getValue(), entry.getKey());
context.getJspConfigDescriptor().getTaglibs().add(descriptor);
}
for (String welcomeFile : welcomeFiles) {
/*
* The following will result in a welcome file of "" so don't add
* that to the context
* <welcome-file-list>
* <welcome-file/>
* </welcome-file-list>
*/
if (welcomeFile != null && welcomeFile.length() > 0) {
context.addWelcomeFile(welcomeFile);
}
}
// Do this last as it depends on servlets
for (JspPropertyGroup jspPropertyGroup : jspPropertyGroups) {
String jspServletName = context.findServletMapping("*.jsp");
if (jspServletName == null) {
jspServletName = "jsp";
}
if (context.findChild(jspServletName) != null) {
for (String urlPattern : jspPropertyGroup.getUrlPatterns()) {
context.addServletMapping(urlPattern, jspServletName, true);
}
} else {
if (log.isDebugEnabled()) {
for (String urlPattern : jspPropertyGroup.getUrlPatterns()) {
log.debug("Skiping " + urlPattern + " , no servlet " + jspServletName);
}
}
}
}
for (Entry<String, String> entry : postConstructMethods.entrySet()) {
context.addPostConstructMethod(entry.getKey(), entry.getValue());
}
for (Entry<String, String> entry : preDestroyMethods.entrySet()) {
context.addPreDestroyMethod(entry.getKey(), entry.getValue());
}
}
use of javax.servlet.SessionCookieConfig in project tomcat70 by apache.
the class ApplicationSessionCookieConfig method createSessionCookie.
/**
* Creates a new session cookie for the given session ID
*
* @param context The Context for the web application
* @param sessionId The ID of the session for which the cookie will be
* created
* @param secure Should session cookie be configured as secure
*/
public static Cookie createSessionCookie(Context context, String sessionId, boolean secure) {
SessionCookieConfig scc = context.getServletContext().getSessionCookieConfig();
// NOTE: The priority order for session cookie configuration is:
// 1. Context level configuration
// 2. Values from SessionCookieConfig
// 3. Defaults
Cookie cookie = new Cookie(SessionConfig.getSessionCookieName(context), sessionId);
// Just apply the defaults.
cookie.setMaxAge(scc.getMaxAge());
cookie.setComment(scc.getComment());
if (context.getSessionCookieDomain() == null) {
// Avoid possible NPE
if (scc.getDomain() != null) {
cookie.setDomain(scc.getDomain());
}
} else {
cookie.setDomain(context.getSessionCookieDomain());
}
// Always set secure if the request is secure
if (scc.isSecure() || secure) {
cookie.setSecure(true);
}
// Always set httpOnly if the context is configured for that
if (scc.isHttpOnly() || context.getUseHttpOnly()) {
cookie.setHttpOnly(true);
}
String contextPath = context.getSessionCookiePath();
if (contextPath == null || contextPath.length() == 0) {
contextPath = scc.getPath();
}
if (contextPath == null || contextPath.length() == 0) {
contextPath = context.getEncodedPath();
}
if (context.getSessionCookiePathUsesTrailingSlash()) {
// sent for requests with a path of /foobar
if (!contextPath.endsWith("/")) {
contextPath = contextPath + "/";
}
} else {
// path of '/' but the servlet spec uses an empty string
if (contextPath.length() == 0) {
contextPath = "/";
}
}
cookie.setPath(contextPath);
return cookie;
}
use of javax.servlet.SessionCookieConfig in project traccar by tananaev.
the class WebServer method initSessionConfig.
private void initSessionConfig(Config config, ServletContextHandler servletHandler) {
if (config.getBoolean(Keys.WEB_PERSIST_SESSION)) {
DatabaseAdaptor databaseAdaptor = new DatabaseAdaptor();
databaseAdaptor.setDatasource(Context.getDataManager().getDataSource());
JDBCSessionDataStoreFactory jdbcSessionDataStoreFactory = new JDBCSessionDataStoreFactory();
jdbcSessionDataStoreFactory.setDatabaseAdaptor(databaseAdaptor);
SessionHandler sessionHandler = servletHandler.getSessionHandler();
SessionCache sessionCache = new DefaultSessionCache(sessionHandler);
sessionCache.setSessionDataStore(jdbcSessionDataStoreFactory.getSessionDataStore(sessionHandler));
sessionHandler.setSessionCache(sessionCache);
}
int sessionTimeout = config.getInteger(Keys.WEB_SESSION_TIMEOUT);
if (sessionTimeout > 0) {
servletHandler.getSessionHandler().setMaxInactiveInterval(sessionTimeout);
}
String sameSiteCookie = config.getString(Keys.WEB_SAME_SITE_COOKIE);
if (sameSiteCookie != null) {
SessionCookieConfig sessionCookieConfig = servletHandler.getServletContext().getSessionCookieConfig();
switch(sameSiteCookie.toLowerCase()) {
case "lax":
sessionCookieConfig.setComment(HttpCookie.SAME_SITE_LAX_COMMENT);
break;
case "strict":
sessionCookieConfig.setComment(HttpCookie.SAME_SITE_STRICT_COMMENT);
break;
case "none":
sessionCookieConfig.setSecure(true);
sessionCookieConfig.setComment(HttpCookie.SAME_SITE_NONE_COMMENT);
break;
default:
break;
}
}
}
Aggregations