OpenAPITools / OpenAPITools/openapi-generator

[REQ] [Java] [Spring] Security info without SecurityContextHolder

Open
#24,391 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Is your feature request related to a problem? Please describe.

No. This suggests is about include a modern alternative to get security info in Controllers, not replace SecurityContextHolder, but include a possibility to use this modern approach.

Describe the solution you'd like

Instead of use SecurityContextHolder to get security info in protected endpoints is include a one new param in methods:

@PostMapping("/orders")
public ResponseEntity<PedidoDTO> createOrder(@RequestBody final OrdrerRequest request, final Principal principal) { // using Principal from Java
    
    final var username = principal.getName();
    // ...
}

or for custom impl

@PostMapping("/orders")
public ResponseEntity<PedidoDTO> createOrder(@RequestBody final OrderRequest request,@Authentication final Principal CustomPrincipal principal) { // custom impl with annotation
    
    Long userId = principal.getId();
    // ...
}

the suggest is provide this alternative if want. This approach is helpfull for create unit tests for controller

Describe alternatives you've considered

could be created a config to enable this, something like

<InjectPrincipalInController>true</InjectPrincipalInController>//default false

for define custom principal:

<CustomPrincipalToUse>FULL_QUALIFIED_NAME_IN_CLASS</CustomPrincipalToUse>

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files or tests are identified. Start by locating the Spring controller generation entry point and review how controller parameters are currently represented; define the scope for Java Principal support, custom principal annotations, configuration, and tests before implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.