[{"data":1,"prerenderedAt":175},["ShallowReactive",2],{"\u002Fblog\u002Fcode-log-day-12":3},{"id":4,"title":5,"author":6,"body":7,"date":161,"description":162,"draft":163,"extension":164,"image":165,"language":166,"meta":167,"navigation":70,"path":168,"seo":169,"stem":170,"tags":171,"__hash__":174},"blog\u002Fblog\u002FCode Log - Day 12.md","Code Log - Day 12","Caio Prado",{"type":8,"value":9,"toc":156},"minimark",[10,15,19,24,35,152],[11,12,14],"h2",{"id":13},"log","Log",[16,17,18],"p",{},"Today i worked on a side project.",[20,21,23],"h3",{"id":22},"side-project","Side Project",[16,25,26,27,34],{},"On the ",[28,29,33],"a",{"href":30,"rel":31},"https:\u002F\u002Fgithub.com\u002Fcaioopr\u002Fixtore",[32],"nofollow","Ixtore"," project, i needed to check if the id of the user trying to call some product-related operation was the same as the user id referenced by the product. Since i'm using Spring Security, in every controller route handler method, i have access to an object of type \"Authentication\" as a parameter, in which i can retrieve the current logged user's id, and pass it to the service layer method, so it can perform the verification.",[36,37,42],"pre",{"className":38,"code":39,"language":40,"meta":41,"style":41},"language-java shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","@RestController\n@RequestMapping(\"\u002Fapi\u002Fproducts\")\npublic class ProductsController {\n\n    @Autowired\n    final private ProductsService productsService;\n\n    public ProductsController(ProductsService productsService){\n        this.productsService = productsService;\n    }\n\n    @GetMapping(\"{code}\")\n    @ResponseStatus(HttpStatus.OK)\n    public ProductEntity getProductsByCode(@PathVariable(\"code\") String code, Authentication currentUser) throws ProductNotFoundException{\n        UserEntity currentUserPrincipal = (UserEntity) currentUser.getPrincipal();\n\n        return productsService.getByCode(code,currentUserPrincipal.getUser_uuid());\n    }\n\n","java","",[43,44,45,53,59,65,72,78,84,89,95,101,107,112,118,124,130,136,141,147],"code",{"__ignoreMap":41},[46,47,50],"span",{"class":48,"line":49},"line",1,[46,51,52],{},"@RestController\n",[46,54,56],{"class":48,"line":55},2,[46,57,58],{},"@RequestMapping(\"\u002Fapi\u002Fproducts\")\n",[46,60,62],{"class":48,"line":61},3,[46,63,64],{},"public class ProductsController {\n",[46,66,68],{"class":48,"line":67},4,[46,69,71],{"emptyLinePlaceholder":70},true,"\n",[46,73,75],{"class":48,"line":74},5,[46,76,77],{},"    @Autowired\n",[46,79,81],{"class":48,"line":80},6,[46,82,83],{},"    final private ProductsService productsService;\n",[46,85,87],{"class":48,"line":86},7,[46,88,71],{"emptyLinePlaceholder":70},[46,90,92],{"class":48,"line":91},8,[46,93,94],{},"    public ProductsController(ProductsService productsService){\n",[46,96,98],{"class":48,"line":97},9,[46,99,100],{},"        this.productsService = productsService;\n",[46,102,104],{"class":48,"line":103},10,[46,105,106],{},"    }\n",[46,108,110],{"class":48,"line":109},11,[46,111,71],{"emptyLinePlaceholder":70},[46,113,115],{"class":48,"line":114},12,[46,116,117],{},"    @GetMapping(\"{code}\")\n",[46,119,121],{"class":48,"line":120},13,[46,122,123],{},"    @ResponseStatus(HttpStatus.OK)\n",[46,125,127],{"class":48,"line":126},14,[46,128,129],{},"    public ProductEntity getProductsByCode(@PathVariable(\"code\") String code, Authentication currentUser) throws ProductNotFoundException{\n",[46,131,133],{"class":48,"line":132},15,[46,134,135],{},"        UserEntity currentUserPrincipal = (UserEntity) currentUser.getPrincipal();\n",[46,137,139],{"class":48,"line":138},16,[46,140,71],{"emptyLinePlaceholder":70},[46,142,144],{"class":48,"line":143},17,[46,145,146],{},"        return productsService.getByCode(code,currentUserPrincipal.getUser_uuid());\n",[46,148,150],{"class":48,"line":149},18,[46,151,106],{},[153,154,155],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":41,"searchDepth":55,"depth":55,"links":157},[158],{"id":13,"depth":55,"text":14,"children":159},[160],{"id":22,"depth":61,"text":23},"2025-01-19","Using Spring Security's Authentication object to check product ownership before running product operations on Ixtore.",false,"md",null,"en",{},"\u002Fblog\u002Fcode-log-day-12",{"title":5,"description":162},"blog\u002FCode Log - Day 12",[172,173,40],"challenge","daily-coding","EzjNe2V1CqQEyZmLJp5Shxlg3bU2pO_KvVlQWqpzqd8",1785357633431]