swagger-api / swagger-api/swagger-codegen

[java spring] API Key based Authentication how to access ?

Open
#7,088 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Java General: Question help wanted Server: Java Server: Spring
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

This is more of a question.

We have a swagger file with authentication mechanism as apikey and appid

security:
  - apiKey: []
    appId: []

Upon generation of Server ( Java Spring ) and Client (Java) below snippets got generated

Server
the interface GroupAPI has below code

 @ApiOperation(value = "Return the group which was created", nickname = "createGroup", notes = "Returns HTTP 201 if the group is successfully created.", response = Void.class, authorizations = {
        @Authorization(value = "apiKey"),
        @Authorization(value = "appId")
    }, tags={ "UserGroups", })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "OK. Request is OK."),
        @ApiResponse(code = 201, message = "The request has been fulfilled, and a new resource is created ."),
        @ApiResponse(code = 400, message = "Bad Request. The request cannot be fulfilled due to bad syntax."),
        @ApiResponse(code = 401, message = "Unauthorized. Authentication failed or not provided"),
        @ApiResponse(code = 403, message = "Forbidden. The user might not have the necessary permissions for a resource."),
        @ApiResponse(code = 500, message = "Internal Server Error. An unexpected condition was encountered."),
         })
    @RequestMapping(value = "/identity/groups",
        produces = { "application/json" }, 
        consumes = { "application/json" },
        method = RequestMethod.POST)
    ResponseEntity<Void> createGroup(@ApiParam(value = "" ,required=true )  @Valid @RequestBody Group body, @RequestHeader(value = "Accept", required = false) String accept) throws Exception;

How do we access the apikey and appId in the controller class method that looks like below ?

public ResponseEntity<Void> createGroup(@ApiParam(value = "" ,required=true )  @Valid @RequestBody Group body,
        @RequestHeader(value = "Accept", required = false) String accept) throws Exception {
        // do some magic!
        return new ResponseEntity<Void>(HttpStatus.OK);
    }

Is there any reference example , greatly appreciated .

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

Start with the generated GroupAPI interface and the controller method shown in the issue, then consult the Swagger Codegen Java Spring examples or documentation for API-key and appId security definitions. Confirm how generated server code exposes these values and document a reference example for accessing them in the controller.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.