Skip to content

Commit 5174906

Browse files
author
Ivan Franchin
committed
Some adjustments
- replace rolesClaim to groupsClaim; - use Principal instead of JwtAuthenticationToken in the JobController.
1 parent 46ab51f commit 5174906

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

jobs-api/src/main/java/com/ivanfranchin/jobsapi/rest/JobController.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.springframework.data.domain.Sort.Direction;
1919
import org.springframework.data.web.PageableDefault;
2020
import org.springframework.http.HttpStatus;
21-
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
2221
import org.springframework.web.bind.annotation.DeleteMapping;
2322
import org.springframework.web.bind.annotation.GetMapping;
2423
import org.springframework.web.bind.annotation.PathVariable;
@@ -30,6 +29,7 @@
3029
import org.springframework.web.bind.annotation.ResponseStatus;
3130
import org.springframework.web.bind.annotation.RestController;
3231

32+
import java.security.Principal;
3333
import java.util.List;
3434
import java.util.stream.Collectors;
3535

@@ -50,9 +50,9 @@ public class JobController {
5050
@GetMapping
5151
public Page<JobResponse> getJobs(
5252
@ParameterObject @PageableDefault(sort = {"createDate"}, direction = Direction.DESC) Pageable pageable,
53-
JwtAuthenticationToken token) {
53+
Principal principal) {
5454
log.info("Request to get a page of jobs (offset = {}, pageSize = {}) made by {}",
55-
pageable.getOffset(), pageable.getPageSize(), token.getName());
55+
pageable.getOffset(), pageable.getPageSize(), principal.getName());
5656
return jobService.getJobsByPage(pageable).map(jobMapper::toJobResponse);
5757
}
5858

@@ -73,8 +73,8 @@ public List<JobResponse> getNewestJobs(@RequestParam(value = "number", required
7373
summary = "Get a job by id",
7474
security = {@SecurityRequirement(name = BEARER_KEY_SECURITY_SCHEME)})
7575
@GetMapping("/{id}")
76-
public JobResponse getJobById(@PathVariable String id, JwtAuthenticationToken token) {
77-
log.info("Request to get a job with id {} made by {}", id, token.getName());
76+
public JobResponse getJobById(@PathVariable String id, Principal principal) {
77+
log.info("Request to get a job with id {} made by {}", id, principal.getName());
7878
Job job = jobService.validateAndGetJobById(id);
7979
return jobMapper.toJobResponse(job);
8080
}
@@ -84,8 +84,8 @@ public JobResponse getJobById(@PathVariable String id, JwtAuthenticationToken to
8484
security = {@SecurityRequirement(name = BEARER_KEY_SECURITY_SCHEME)})
8585
@ResponseStatus(HttpStatus.CREATED)
8686
@PostMapping
87-
public JobResponse createJob(@Valid @RequestBody CreateJobRequest createJobRequest, JwtAuthenticationToken token) {
88-
log.info("Request to create a job made by {}", token.getName());
87+
public JobResponse createJob(@Valid @RequestBody CreateJobRequest createJobRequest, Principal principal) {
88+
log.info("Request to create a job made by {}", principal.getName());
8989
Job job = jobMapper.toJob(createJobRequest);
9090
job = jobService.saveJob(job);
9191
return jobMapper.toJobResponse(job);
@@ -95,8 +95,8 @@ public JobResponse createJob(@Valid @RequestBody CreateJobRequest createJobReque
9595
summary = "Delete a job",
9696
security = {@SecurityRequirement(name = BEARER_KEY_SECURITY_SCHEME)})
9797
@DeleteMapping("/{id}")
98-
public JobResponse deleteJob(@PathVariable String id, JwtAuthenticationToken token) {
99-
log.info("Request to delete a job with id {} made by {}", id, token.getName());
98+
public JobResponse deleteJob(@PathVariable String id, Principal principal) {
99+
log.info("Request to delete a job with id {} made by {}", id, principal.getName());
100100
Job job = jobService.validateAndGetJobById(id);
101101
jobService.deleteJob(job);
102102
return jobMapper.toJobResponse(job);
@@ -107,8 +107,8 @@ public JobResponse deleteJob(@PathVariable String id, JwtAuthenticationToken tok
107107
security = {@SecurityRequirement(name = BEARER_KEY_SECURITY_SCHEME)})
108108
@PutMapping("/{id}")
109109
public JobResponse updateJob(@PathVariable String id,
110-
@Valid @RequestBody UpdateJobRequest updateJobRequest, JwtAuthenticationToken token) {
111-
log.info("Request to update a job with id {} made by {}", id, token.getName());
110+
@Valid @RequestBody UpdateJobRequest updateJobRequest, Principal principal) {
111+
log.info("Request to update a job with id {} made by {}", id, principal.getName());
112112
Job job = jobService.validateAndGetJobById(id);
113113
jobMapper.updateJobFromRequest(updateJobRequest, job);
114114
jobService.saveJob(job);
@@ -121,8 +121,8 @@ public JobResponse updateJob(@PathVariable String id,
121121
@PutMapping("/search")
122122
public Page<Job> searchJobs(@Valid @RequestBody SearchRequest searchRequest,
123123
@ParameterObject @PageableDefault(sort = {"createDate"}, direction = Direction.DESC) Pageable pageable,
124-
JwtAuthenticationToken token) {
125-
log.info("Request to search a job with text {} made by {}", searchRequest.getText(), token.getName());
124+
Principal principal) {
125+
log.info("Request to search a job with text {} made by {}", searchRequest.getText(), principal.getName());
126126
return jobService.search(searchRequest.getText(), pageable);
127127
}
128128
}

jobs-api/src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ okta:
88
oauth2:
99
issuer: https://${OKTA_DOMAIN}/oauth2/default
1010
clientId: ${OKTA_CLIENT_ID}
11-
rolesClaim: groups
11+
groupsClaim: groups
1212

1313
management:
1414
endpoints:

0 commit comments

Comments
 (0)