Package com.networknt.router.middleware
Class TokenHandler
- java.lang.Object
-
- com.networknt.router.middleware.TokenHandler
-
- All Implemented Interfaces:
LightHttpHandler,MiddlewareHandler,io.undertow.server.HttpHandler
public class TokenHandler extends Object implements MiddlewareHandler
This is a middleware handler that is responsible for getting a JWT access token from OAuth 2.0 provider for the particular router client. The only token that is supported in this handler is client credentials token as there is no user information available here. The client_id and client_secret will be retrieved from client.yml and client_secret should be encrypted or set as an environment variable. In Kubernetes cluster, you can create a sealed secret for it. This handler will also responsible for checking if the cached token is about expired or not. In which case, it will renew the token in another thread. When request comes and the cached token is already expired, then it will block the request and go to the OAuth provider to get a new token and then resume the request to the next handler in the chain. The logic is very similar with client module in light-4j but this is implemented in a handler instead. Multiple OAuth 2.0 providers are supported and the token cache strategy can be defined based on your OAuth 2.0 providers. This light-router is designed for standalone or client that is not implemented in Java Otherwise, you should use client module instead of this one. In the future, we might add Authorization Code grant type support by providing an endpoint in the light-router to accept Authorization Code redirect and then get the token from OAuth 2.0 provider. There is no specific configuration file for this handler just to enable or disable it. If you want to bypass this handler, you can comment it out from handler.yml middleware handler section or change the token.yml to disable it. Once the token is retrieved from OAuth 2.0 provider, it will be placed in the header as Authorization Bearer token according to the OAuth 2.0 specification.
-
-
Field Summary
Fields Modifier and Type Field Description static Map<String,Jwt>cacheprotected io.undertow.server.HttpHandlernext-
Fields inherited from interface com.networknt.handler.LightHttpHandler
AUDIT_CONFIG_NAME, AUDIT_ON_ERROR, AUDIT_STACK_TRACE, auditConfig, auditOnError, auditStackTrace, CONFIG_NAME, ERROR_NOT_DEFINED
-
-
Constructor Summary
Constructors Constructor Description TokenHandler()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static com.networknt.monad.Result<Jwt>getJwtToken(String serviceId)io.undertow.server.HttpHandlergetNext()voidhandleRequest(io.undertow.server.HttpServerExchange exchange)booleanisEnabled()voidregister()voidreload()MiddlewareHandlersetNext(io.undertow.server.HttpHandler next)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.networknt.handler.LightHttpHandler
setExchangeStatus, setExchangeStatus, setExchangeStatus
-
-
-
-
Method Detail
-
handleRequest
public void handleRequest(io.undertow.server.HttpServerExchange exchange) throws Exception- Specified by:
handleRequestin interfaceio.undertow.server.HttpHandler- Throws:
Exception
-
getNext
public io.undertow.server.HttpHandler getNext()
- Specified by:
getNextin interfaceMiddlewareHandler
-
setNext
public MiddlewareHandler setNext(io.undertow.server.HttpHandler next)
- Specified by:
setNextin interfaceMiddlewareHandler
-
isEnabled
public boolean isEnabled()
- Specified by:
isEnabledin interfaceMiddlewareHandler
-
register
public void register()
- Specified by:
registerin interfaceMiddlewareHandler
-
reload
public void reload()
- Specified by:
reloadin interfaceMiddlewareHandler
-
-