package Ġio . cz ar . db inf ode mo . security Ċ Ċ import Ġio . cz ar . db inf ode mo . security . Web Security Config . Companion . TOKEN _ PREFIX Ċ import Ġio . json web token . Jw ts Ċ import Ġorg . springframework . boot . context . properties . Configuration Properties Ċ import Ġorg . springframework . http . HttpHeaders Ċ import Ġorg . springframework . security . authentication . Authentication Manager Ċ import Ġorg . springframework . security . authentication . Username Password Authentication Token Ċ import Ġorg . springframework . security . core . Granted Authority Ċ import Ġorg . springframework . security . core . context . SecurityContext Holder Ċ import Ġorg . springframework . security . web . authentication . www . Basic Authentication Filter Ċ import Ġjavax . servlet . FilterChain Ċ import Ġjavax . servlet . http . HttpServletRequest Ċ import Ġjavax . servlet . http . HttpServletResponse Ċ Ċ @ Configuration Properties (" security . token ") Ċ class ĠToken Based Authorization Filter ( authentication Manager : ĠAuthentication Manager ) Ġ: Ċ ĉ Basic Authentication Filter ( authentication Manager ) Ġ{ Ċ ĉ late init Ġvar Ġsecret : ĠString Ċ ĉ override Ġfun Ġdo Filter Internal ( request : ĠHttpServletRequest , Ġresponse : ĠHttpServletResponse , Ġchain : ĠFilter Chain ) Ġ{ ĊĊĉ ĉ var Ġauthorization Token : ĠString ? Ġ= Ġrequest . getHeader ( HttpHeaders . AUTHORIZATION ) ĊĊĉ ĉ if Ġ( authorization Token Ġ!= Ġnull Ġ&& Ġauthorization Token . startsWith ( TOKEN _ PREFIX )) Ġ{ Ċĉĉ ĉ authorization Token Ġ= Ġauthorization Token . replace First ( TOKEN _ PREFIX . to Regex (), Ġ"") ĊĊĉĉ ĉ val ĠuserId Ġ= ĠJ w ts . parser () Ċĉĉĉ ĉ . set Signing Key ( secret ) Ċĉĉĉ ĉ . parse Claims J ws ( authorization Token ) Ċĉĉĉ ĉ . body [" userId "]. let Ġ{ Ċĉĉĉ ĉ when Ġ( it ) Ġ{ Ċĉĉĉĉ ĉ is ĠInt Ġ-> Ġit . to Long () Ċĉĉĉĉ ĉ is ĠLong Ġ-> Ġit Ċĉĉĉĉ ĉ else Ġ-> Ġthrow ĠIllegalArgumentException (" UserId Ġshould Ġbe Ġof Ġtype ĠLong .") Ċĉĉĉ ĉ } Ċĉĉ ĉ } ĊĊĉĉ ĉ SecurityContext Holder . getContext (). authentication Ġ= Ċĉĉĉĉ ĉ Username Password Authentication Token ( userId , Ġnull , ĠlistOf ( User Authority )) Ċĉ ĉ } ĊĊĉ ĉ chain . do Filter ( request , Ġresponse ) Ċ ĉ } Ċ } Ċ Ċ object ĠUser Authority Ġ: ĠGr anted Authority Ġ{ Ċ ĉ override Ġfun Ġget Authority () Ġ= Ġ" USER " Ċ } Ċ