I want to secure a backend API written in Java/SpringBoot and using Auth0. I am following the Auth0 example.
Everything works fine and as expected, however, when I apply this to my application I want to detect the user who made the API call. In various examples I've come across it appears to be possible with Spring Security by injecting the OidcUser.
Some relevant links I've found are:
In my application, my controller looks like this:
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/project")
public class ProjectController {
@PostMapping("/add")
@PreAuthorize("hasAuthority('write:project')")
public ResponseEntity<DummySubmitResponseDto> createNewProject(@AuthenticationPrincipal OidcUser oidcUser,
@RequestBody DummyDto dummyDto) {
DummySubmitResponseDto projectSubmitResponseDto = new DummySubmitResponseDto("TheResponse");
return new ResponseEntity<>(projectSubmitResponseDto, HttpStatus.CREATED);
}
}
I'm using a React SPA to get an access-token. I save it manually and use Curl or POSTMAN to call the API. However, oidcUser is always null.
I'm not sure if I'm following the correct approach. Separate to this, I don't understand how the Backend (which is a resource server) is able to get the user information from the access token.