package Ġdev . top pe . me em io . service Ċ Ċ import Ġdev . top pe . me em io . model . User Ċ import Ġdev . top pe . me em io . repository . UserRepository Ċ import Ġorg . springframework . security . core . authority . Simple Granted Authority Ċ import Ġorg . springframework . security . core . context . SecurityContext Holder Ċ import Ġorg . springframework . security . core . user details . UserDetails Ċ import Ġorg . springframework . security . core . user details . UserDetails Service Ċ import Ġorg . springframework . security . core . user details . Username NotFoundException Ċ import Ġorg . springframework . stereotype . Service Ċ import Ġjavax . transaction . Transactional Ċ Ċ import Ġorg . springframework . security . core . user details . User Ġas ĠSimple UserDetails Ċ Ċ @ Service Ċ class ĠUser Details ServiceImpl ( val Ġuser Repository : ĠUser Repository ) Ġ: ĠUser Details Service Ġ{ ĊĊĠĠĠ Ġ@ Transactional ĊĠĠĠ Ġoverride Ġfun Ġload User By Username ( username : ĠString ): ĠUser Details Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġuser : ĠUser Ġ= Ġuser Repository . findBy Username ( username ) Ġ?: Ġthrow ĠUsername NotFoundException ( username ) ĊĠĠĠĠĠĠĠ Ġval Ġauthor ities Ġ= Ġuser . roles . map Ġ{ ĠSimple Granted Authority ( it . name ) Ġ} ĊĠĠĠĠĠĠĠ Ġreturn ĠCurrent User ( user . username , Ġuser . password , Ġauthor ities , Ġuser ) ĊĠĠĠ Ġ} Ċ } Ċ Ċ /** Ċ Ġ* ĠAdds Ġuser Ġfield Ġto Ġorg . springframework . security . core . user details . User Ċ Ġ* Ċ Ġ*/ Ċ class ĠCurrent User ( ĊĠĠĠĠĠĠĠ Ġusername : ĠString ?, ĊĠĠĠĠĠĠĠ Ġpassword : ĠString ?, ĊĠĠĠĠĠĠĠ Ġauthor ities : ĠList < Simple Granted Authority >, ĊĠĠĠĠĠĠĠ Ġ/** ĠWarning : Ġlazy Ġattributes Ġmay Ġnot Ġwork Ġwell Ġ*/ ĊĠĠĠĠĠĠĠ Ġval Ġuser : ĠUser Ċ ) Ġ: ĠSimple UserDetails ( username , Ġpassword , Ġauthor ities ) ĊĊ Ċ /** ĠWarning : Ġlazy Ġattributes Ġmay Ġnot Ġwork Ġwell Ġ*/ Ċ fun ĠgetCurrent User () Ġ= Ġ( SecurityContext Holder . getContext (). authentication . principal Ġas ĠCurrent User ). user