Skip to content

Commit 2d76792

Browse files
committed
AuthenticationPrincipal example
1 parent dd95923 commit 2d76792

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

samples/webflux-security/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ repositories {
1515
}
1616

1717
dependencies {
18-
implementation 'org.springframework.boot:spring-boot-starter-graphql'
18+
implementation project(":spring-graphql")
1919
implementation 'org.springframework.boot:spring-boot-starter-webflux'
2020
implementation 'org.springframework.boot:spring-boot-starter-security'
2121
implementation 'org.springframework.boot:spring-boot-starter-actuator'
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.spring.sample.graphql;
17+
18+
import org.springframework.graphql.data.method.annotation.QueryMapping;
19+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
20+
import org.springframework.security.core.userdetails.UserDetails;
21+
import org.springframework.stereotype.Controller;
22+
import reactor.core.publisher.Mono;
23+
24+
@Controller
25+
public class CurrentUserController {
26+
27+
@QueryMapping
28+
Mono<String> username(@AuthenticationPrincipal Mono<UserDetails> userDetails) {
29+
return userDetails.map(UserDetails::getUsername);
30+
}
31+
32+
}

samples/webflux-security/src/main/resources/graphql/schema.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
type Query {
22
employees: [Employee]
3+
username: String
34
}
45
type Mutation {
56
# restricted
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
query {
2+
username
3+
}

samples/webflux-security/src/test/java/io/spring/sample/graphql/SampleApplicationTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.boot.test.context.SpringBootTest;
2323
import org.springframework.graphql.execution.ErrorType;
2424
import org.springframework.graphql.test.tester.WebGraphQlTester;
25+
import org.springframework.security.test.context.support.WithMockUser;
2526

2627
import static org.assertj.core.api.Assertions.assertThat;
2728
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -71,6 +72,14 @@ void canQueryName() {
7172
.path("employees[0].name").entity(String.class).isEqualTo("Andi");
7273
}
7374

75+
@Test
76+
@WithMockUser("rob")
77+
void canQueryUsername() {
78+
this.graphQlTester.queryName("username")
79+
.execute()
80+
.path("username").entity(String.class).isEqualTo("rob");
81+
}
82+
7483
@Test
7584
void canNotQuerySalary() {
7685
this.graphQlTester.queryName("employeesNamesAndSalaries")

0 commit comments

Comments
 (0)