Class ServerConfig
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionallowedOrigins(String... allowedOrigins) apiPrefixEnabled(boolean apiPrefixEnabled) contentSecurityPolicy(String contentSecurityPolicy) contextPath(String contextPath) Create a new session using the configured session store.csrfEnabled(boolean csrfEnabled) csrfExemptPaths(String... csrfExemptPaths) Register paths where CSRF token validation is skipped.devMode(boolean devMode) devTrace(boolean devTrace) Enable fragment-trace HTML comments around everyth:insert/th:replaceresolution.engineSupplier(Supplier<io.karatelabs.js.Engine> engineSupplier) Set a custom ExternalBridge to control Java interop.errorTemplate404(String errorTemplate404) errorTemplate500(String errorTemplate500) String[]Supplier<io.karatelabs.js.Engine> longintglobalVariables(Map<String, Object> globalVariables) Set global variables that will be available in all templates and API handlers.hstsEnabled(boolean hstsEnabled) hstsIncludeSubDomains(boolean hstsIncludeSubDomains) hstsMaxAge(long hstsMaxAge) booleanCheck if the given path should be handled as an API route.booleanbooleanbooleanisCsrfExemptPath(String path) booleanbooleanbooleanbooleanbooleanCheck if the given request path opts out of shell-wrap.booleanbooleanCheck if sessions are enabled.booleanisStaticPath(String path) Check if the given path should be served as a static file.booleanlogHandler(Consumer<String> logHandler) Register paths that ship their own full HTML shell and should NOT be wrapped withshellTemplate(String).referrerPolicy(String referrerPolicy) requestFilter(BiFunction<HttpRequest, ServerMarkupContext, HttpResponse> requestFilter) Set a request filter that runs after session loading but before CSRF validation.requestInterceptor(Consumer<HttpRequest> requestInterceptor) resourceRoot(String resourceRoot) securityHeadersEnabled(boolean securityHeadersEnabled) sessionCookieName(String sessionCookieName) sessionExpirySeconds(int sessionExpirySeconds) sessionSameSite(SameSite sessionSameSite) Set theSameSiteattribute on the session cookie.sessionStore(SessionStore sessionStore) shellTemplate(String shellTemplate) Set a layout shell template that wraps full-page navigations.staticPrefix(String staticPrefix) staticPrefixEnabled(boolean staticPrefixEnabled) templateRoute(String pathPattern, String templateName) Add a path pattern → template mapping.toString()xFrameOptions(String xFrameOptions)
-
Constructor Details
-
ServerConfig
public ServerConfig() -
ServerConfig
-
-
Method Details
-
getResourceRoot
-
getContextPath
-
isDevMode
public boolean isDevMode() -
isDevTrace
public boolean isDevTrace() -
getSessionExpirySeconds
public int getSessionExpirySeconds() -
getSessionCookieName
-
getSessionStore
-
getApiPrefix
-
isApiPrefixEnabled
public boolean isApiPrefixEnabled() -
getStaticPrefix
-
isStaticPrefixEnabled
public boolean isStaticPrefixEnabled() -
isCsrfEnabled
public boolean isCsrfEnabled() -
getSessionSameSite
-
isCsrfExemptPath
-
getAllowedOrigins
-
isSecurityHeadersEnabled
public boolean isSecurityHeadersEnabled() -
getContentSecurityPolicy
-
getXFrameOptions
-
getReferrerPolicy
-
isHstsEnabled
public boolean isHstsEnabled() -
getHstsMaxAge
public long getHstsMaxAge() -
isHstsIncludeSubDomains
public boolean isHstsIncludeSubDomains() -
getErrorTemplate404
-
getErrorTemplate500
-
getTemplateRoutes
-
getShellTemplate
-
getRawPaths
-
getRequestInterceptor
-
getRequestFilter
-
getLogHandler
-
getEngineSupplier
-
getGlobalVariables
-
resourceRoot
-
contextPath
-
devMode
-
devTrace
Enable fragment-trace HTML comments around everyth:insert/th:replaceresolution. Useful for debugging composition.Requires
devMode(boolean)= trueas a precondition — trace emission is silently a no-op outside dev mode (production safety: never leak fragment names or scope payloads to end users).Can also be enabled via the
KARATE_DEV_TRACE=trueenvironment variable, which is read once whenServerConfigis constructed. Programmatic configuration via this method overrides the env var. -
sessionExpirySeconds
-
sessionCookieName
-
sessionStore
-
apiPrefix
-
apiPrefixEnabled
-
staticPrefix
-
staticPrefixEnabled
-
csrfEnabled
-
csrfExemptPaths
Register paths where CSRF token validation is skipped. Use for endpoints that legitimately receive cross-site POSTs without a CSRF token but have their own anti-CSRF mechanism, for example:- OAuth/OIDC
response_mode=form_postcallbacks — the IdP postscode+stateback to/signin; thestateparameter validation provides CSRF protection. - Webhook receivers where authenticity is verified via a signature header (e.g. Stripe, GitHub).
Exact path match (not prefix). For more granular control, use
requestFilter(java.util.function.BiFunction). - OAuth/OIDC
-
sessionSameSite
Set theSameSiteattribute on the session cookie. Default isSameSite.LAX, which works for ordinary form posts and OAuth callbacks that useresponse_mode=query.Switch to
SameSite.NONEwhen an OAuth/OIDC provider usesresponse_mode=form_post(cross-site top-level POST back to your callback URL). Lax cookies are dropped on cross-site POSTs by every standards-compliant browser, so the session cookie set during the pre-redirect signin flow would not survive the IdP's POST back.NonerequiresSecure, which is added automatically outside dev mode.When using
None, ensure the path receiving the cross-site POST is incsrfExemptPaths(String...)(the IdP cannot send your CSRF token) and that the path performs its own CSRF check (OAuth state parameter validation). -
allowedOrigins
-
securityHeadersEnabled
-
contentSecurityPolicy
-
xFrameOptions
-
referrerPolicy
-
hstsEnabled
-
hstsMaxAge
-
hstsIncludeSubDomains
-
errorTemplate404
-
errorTemplate500
-
templateRoute
Add a path pattern → template mapping. Checked before file resolution — routes take priority. The original request path is preserved so the template can use request.pathMatches() to extract parameters.Example:
config.templateRoute("/sessions/{id}", "session.html") .templateRoute("/sessions/{id}/report", "report.html") .templateRoute("/flows/{path}", "flows.html")Register more specific patterns first.
-
shellTemplate
Set a layout shell template that wraps full-page navigations. The shell'sth:utext="content"slot receives the rendered content template's HTML.The wrap is skipped when:
- The request has the
HX-Requestheader (HTMX fragment swap) - The resolved path is in
rawPaths(String...) - The resolved template equals the shell template (defensive anti-recursion)
- The resolved content short-circuited with
context.redirect()
Content renders first (so its ka:scope side effects — flash, session, switch, redirect — commit before the shell evaluates). If content calls
context.switch(t), the switched template becomes the new content and the shell still wraps it.The shell receives these additional template variables:
content— the rendered content HTML (useth:utext="content")contentTemplate— the resolved content template path (e.g.,items.html)
- The request has the
-
rawPaths
Register paths that ship their own full HTML shell and should NOT be wrapped withshellTemplate(String). Typical uses:/signin,/signout, landing pages with bespoke branding.Exact path match (not prefix). Use the request path, not the template name.
-
requestInterceptor
-
requestFilter
public ServerConfig requestFilter(BiFunction<HttpRequest, ServerMarkupContext, HttpResponse> requestFilter) Set a request filter that runs after session loading but before CSRF validation. The filter receives the request and fully initialized context (with session loaded). Return null to continue normal processing, or an HttpResponse to short-circuit.Use this to:
- Set
context.setSession(Session.TEMPORARY)for public API endpoints (webhooks) - Return a 401/403 response for unauthorized requests based on session state
- Implement centralized auth validation across all routes
- Set
-
logHandler
-
engineSupplier
Set a custom ExternalBridge to control Java interop. Override forType() to restrict which Java classes can be accessed from templates. By default, all classes are accessible. -
globalVariables
Set global variables that will be available in all templates and API handlers. These variables are injected into the JavaScript engine context. -
isApiPath
Check if the given path should be handled as an API route. -
isStaticPath
Check if the given path should be served as a static file. -
isRawPath
Check if the given request path opts out of shell-wrap. -
isSessionEnabled
public boolean isSessionEnabled()Check if sessions are enabled. -
createSession
Create a new session using the configured session store. Returns null if sessions are not enabled. -
toString
-