Skip to content

Commit 4ee0f81

Browse files
committed
passing authenticated username to controller
1 parent a9bb9b1 commit 4ee0f81

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
HELP.md
2+
/src/main/resources/data.sql
23
.gradle
34
build/
45
!gradle/wrapper/gradle-wrapper.jar

src/main/java/pl/robocikd/restapi/config/Config.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.springframework.context.annotation.Bean;
44
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
56
import org.springframework.web.bind.annotation.RestController;
67
import springfox.documentation.builders.ApiInfoBuilder;
78
import springfox.documentation.builders.RequestHandlerSelectors;
@@ -15,13 +16,13 @@
1516
import java.util.Collections;
1617
import java.util.List;
1718

18-
1919
@Configuration
2020
public class Config {
2121

2222
@Bean
2323
public Docket docket() {
2424
return new Docket(DocumentationType.OAS_30)
25+
.ignoredParameterTypes(UsernamePasswordAuthenticationToken.class)
2526
.apiInfo(new ApiInfoBuilder()
2627
.title("RobocikD")
2728
.description("demo API based on nullpointerexception.pl")

src/main/java/pl/robocikd/restapi/controller/PostController.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import lombok.RequiredArgsConstructor;
44
import org.springframework.data.domain.Sort;
5+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
6+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
57
import org.springframework.web.bind.annotation.*;
68
import pl.robocikd.restapi.controller.dto.PostDto;
79
import pl.robocikd.restapi.model.Post;
@@ -16,7 +18,7 @@ public class PostController {
1618
private final PostService postService;
1719

1820
@GetMapping("/posts")
19-
public List<PostDto> getPosts(@RequestParam(required = false) Integer page, Sort.Direction sort) {
21+
public List<PostDto> getPosts(@RequestParam(required = false) Integer page, Sort.Direction sort, @AuthenticationPrincipal UsernamePasswordAuthenticationToken user) {
2022
int pageNumber = page != null && page > 0 ? page : 0;
2123
Sort.Direction sortDir = sort != null ? sort : Sort.Direction.ASC;
2224
return PostDtoMapper.mapToPostDtos(postService.getPosts(pageNumber, sortDir));

0 commit comments

Comments
 (0)