package Ġmovies . spring . data . neo 4 j . infrastructure . security Ċ Ċ import Ġorg . springframework . security . authentication . Authentication Manager Ċ import Ġorg . springframework . security . authentication . Internal Authentication ServiceException Ċ import Ġorg . springframework . security . core . Authentication Ċ import Ġorg . springframework . security . core . Authentication Exception Ċ import Ġorg . springframework . security . core . context . SecurityContext Holder Ċ import Ġorg . springframework . security . web . authentication . pre auth . Pre Authenticated Authentication Token Ċ import Ġorg . springframework . web . filter . Generic Filter Bean Ċ import Ġjava . io . IOException Ċ import Ġjavax . servlet . FilterChain Ċ import Ġjavax . servlet . Servlet Exception Ċ import Ġjavax . servlet . Servlet Request Ċ import Ġjavax . servlet . Servlet Response Ċ import Ġjavax . servlet . http . HttpServletRequest Ċ import Ġjavax . servlet . http . HttpServletResponse Ċ Ċ class ĠToken Authentication Filter ( private Ġval Ġauthentication Manager : ĠAuthentication Manager ) ĊĠĠĠ Ġ: ĠGeneric Filter Bean () Ġ{ ĊĊĠĠĠ Ġ@ Throws ( IOException :: class , ĠServletException :: class ) ĊĠĠĠ Ġoverride Ġfun Ġdo Filter ( request : ĠServlet Request , Ġresponse : ĠServlet Response , Ġchain : ĠFilter Chain ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval ĠhttpRequest Ġ= Ġrequest Ġas ĠHttpServletRequest ĊĠĠĠĠĠĠĠ Ġval ĠhttpResponse Ġ= Ġresponse Ġas ĠHttpServletResponse ĊĊĠĠĠĠĠĠĠ Ġval Ġtoken : ĠString ? Ġ= ĠhttpRequest . getHeader (" Access - Token ") ĊĠĠĠĠĠĠĠ Ġval ĠapiKey : ĠString ? Ġ= ĠhttpRequest . getHeader (" API - Key ") ĊĊĠĠĠĠĠĠĠ Ġtry Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ(! token . isNull OrEmpty () Ġ&& Ġ! apiKey . isNull OrEmpty ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġprocess Token Authentication ( token !! , ĠapiKey !! ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġchain . do Filter ( request , Ġresponse ) ĊĠĠĠĠĠĠĠ Ġ} Ġcatch Ġ( internal Authentication ServiceException : ĠInternal Authentication ServiceException ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠSecurity Context Holder . clear Context () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġlogger . error (" Internal Ġauthentication Ġservice Ġexception ", Ġinternal Authentication ServiceException ) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠhttpResponse . send Error ( HttpServletResponse . SC _ INTERNAL _ SERVER _ ERROR ) ĊĠĠĠĠĠĠĠ Ġ} Ġcatch Ġ( authentication Exception : ĠAuthentication Exception ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠSecurity Context Holder . clear Context () ĊĠĠĠĠĠĠĠĠĠĠĠ ĠhttpResponse . send Error ( HttpServletResponse . SC _ UN AUTHORIZ ED , Ġauthentication Exception . message ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġprocess Token Authentication ( token : ĠString , ĠapiKey : ĠString ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġauth Credentials Ġ= ĠSession Credentials ( apiKey , Ġtoken ) ĊĠĠĠĠĠĠĠ Ġval Ġrequest Authentication Ġ= ĠPre Authenticated Authentication Token ( auth Credentials , Ġauth Credentials ) ĊĠĠĠĠĠĠĠ Ġval Ġresult Of Authentication Ġ= Ġtry To Authenticate ( request Authentication ) ĊĠĠĠĠĠĠĠ ĠSecurity Context Holder . getContext (). authentication Ġ= Ġresult Of Authentication ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġtry To Authenticate ( request Authentication : ĠAuthentication ): ĠAuthentication Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġresponse Authentication Ġ= Ġauthentication Manager . authenticate ( request Authentication ) ĊĠĠĠĠĠĠĠ Ġif Ġ( response Authentication Ġ== Ġnull Ġ|| Ġ! response Authentication . is Authenticated ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow ĠInternal Authentication ServiceException (" Unable Ġto Ġauthenticate ĠDomain ĠUser Ġfor Ġprovided Ġcredentials ") ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġreturn Ġresponse Authentication ĊĠĠĠ Ġ} Ċ Ċ } Ċ