Package org.atmosphere.interceptor
Class AuthInterceptor
java.lang.Object
org.atmosphere.cpr.AtmosphereInterceptorAdapter
org.atmosphere.interceptor.AuthInterceptor
- All Implemented Interfaces:
AtmosphereInterceptor,AtmosphereConfigAware,InvokationOrder
Authentication interceptor that validates tokens on every inbound request.
Supports all Atmosphere transports (WebSocket, SSE, long-polling, streaming).
Tokens are extracted from:
- The
X-Atmosphere-AuthHTTP header (long-polling, streaming) - The query parameter (configurable, defaults to
X-Atmosphere-Auth) — all transports, especially WebSocket and SSE where custom headers are not supported
On token expiration, if a TokenRefresher is configured, the interceptor attempts
a server-side refresh and sends the new token to the client via the
X-Atmosphere-Auth-Refresh response header.
Programmatic usage
framework.interceptor(new AuthInterceptor(token -> {
var claims = jwt.verify(token);
return new TokenValidator.Valid(claims.getSubject(), claims);
}));
Configuration (init-params)
org.atmosphere.auth.tokenValidator— FQCN of aTokenValidatororg.atmosphere.auth.tokenRefresher— FQCN of aTokenRefresher(optional)org.atmosphere.auth.queryParam— query param name (default: X-Atmosphere-Auth)org.atmosphere.auth.disconnectOnFailure— disconnect on auth failure (default: true)
- Since:
- 4.0
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.atmosphere.interceptor.InvokationOrder
InvokationOrder.PRIORITY -
Field Summary
Fields inherited from interface org.atmosphere.interceptor.InvokationOrder
AFTER_DEFAULT, BEFORE_DEFAULT, FIRST_BEFORE_DEFAULT -
Constructor Summary
ConstructorsConstructorDescriptionCreate an AuthInterceptor configured via init-params.AuthInterceptor(TokenValidator validator) Create an AuthInterceptor with a programmatic validator.AuthInterceptor(TokenValidator validator, TokenRefresher refresher) Create an AuthInterceptor with a programmatic validator and refresher. -
Method Summary
Modifier and TypeMethodDescriptionintvoidconfigure(AtmosphereConfig config) Configure an AtmosphereFramework object.voiddestroy()Clean the AtmosphereInterceptor when removed or when the Atmosphere is undeployed.Invoked before anAtmosphereResourcegets dispatched toAtmosphereHandler.voidInvoked after anAtmosphereResourcegets dispatched toAtmosphereHandler.priority()Return the priority an AtmosphereInterceptor must be executed.toString()longlong
-
Constructor Details
-
AuthInterceptor
Create an AuthInterceptor with a programmatic validator.- Parameters:
validator- the token validator
-
AuthInterceptor
Create an AuthInterceptor with a programmatic validator and refresher.- Parameters:
validator- the token validatorrefresher- the token refresher (may be null)
-
AuthInterceptor
public AuthInterceptor()Create an AuthInterceptor configured via init-params. TheTokenValidatorwill be loaded from theorg.atmosphere.auth.tokenValidatorinit-param.
-
-
Method Details
-
configure
Description copied from interface:AtmosphereConfigAwareConfigure an AtmosphereFramework object.- Specified by:
configurein interfaceAtmosphereConfigAware- Overrides:
configurein classAtmosphereInterceptorAdapter- Parameters:
config-AtmosphereConfig
-
inspect
Description copied from interface:AtmosphereInterceptorInvoked before anAtmosphereResourcegets dispatched toAtmosphereHandler.- Specified by:
inspectin interfaceAtmosphereInterceptor- Overrides:
inspectin classAtmosphereInterceptorAdapter- Parameters:
r- aAtmosphereResource- Returns:
Action.CONTINUEorAction.SUSPENDto dispatch theAtmosphereResourceto otherAtmosphereInterceptororAtmosphereHandler. ReturnAction.TYPE.CANCELLEDto stop the processing.
-
postInspect
Description copied from interface:AtmosphereInterceptorInvoked after anAtmosphereResourcegets dispatched toAtmosphereHandler.- Specified by:
postInspectin interfaceAtmosphereInterceptor- Overrides:
postInspectin classAtmosphereInterceptorAdapter- Parameters:
r- aAtmosphereResource
-
destroy
public void destroy()Description copied from interface:AtmosphereInterceptorClean the AtmosphereInterceptor when removed or when the Atmosphere is undeployed.- Specified by:
destroyin interfaceAtmosphereInterceptor- Overrides:
destroyin classAtmosphereInterceptorAdapter
-
priority
Description copied from interface:InvokationOrderReturn the priority an AtmosphereInterceptor must be executed.- Specified by:
priorityin interfaceInvokationOrder- Overrides:
priorityin classAtmosphereInterceptorAdapter- Returns:
- PRIORITY
-
totalRejected
public long totalRejected()- Returns:
- total requests rejected due to authentication failure
-
totalRefreshed
public long totalRefreshed()- Returns:
- total tokens successfully refreshed
-
authenticatedCount
public int authenticatedCount()- Returns:
- number of currently authenticated resources
-
toString
- Overrides:
toStringin classAtmosphereInterceptorAdapter
-