Skip to content

Commit 89a7145

Browse files
committed
Polishing contribution
Closes gh-367
1 parent a927159 commit 89a7145

File tree

10 files changed

+58
-15
lines changed

10 files changed

+58
-15
lines changed

samples/webflux-security/src/main/java/io/spring/sample/graphql/SalaryController.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ public Mono<BigDecimal> salary(Employee employee) {
4949
}
5050

5151
@MutationMapping
52-
public void updateSalary(@Argument("input") SalaryInput salaryInput) {
52+
public Mono<Void> updateSalary(@Argument("input") SalaryInput salaryInput) {
5353
String employeeId = salaryInput.getEmployeeId();
5454
BigDecimal salary = salaryInput.getNewSalary();
55-
this.salaryService.updateSalary(employeeId, salary);
55+
return this.salaryService.updateSalary(employeeId, salary);
5656
}
5757

5858
}

samples/webflux-security/src/main/java/io/spring/sample/graphql/SalaryInput.java

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public class SalaryInput {
2323

2424
private BigDecimal newSalary;
2525

26+
public SalaryInput(String employeeId, BigDecimal newSalary) {
27+
this.employeeId = employeeId;
28+
this.newSalary = newSalary;
29+
}
30+
2631
public String getEmployeeId() {
2732
return employeeId;
2833
}

samples/webflux-security/src/main/java/io/spring/sample/graphql/SalaryService.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public Mono<BigDecimal> getSalaryForEmployee(Employee employee) {
3232
return Mono.just(new BigDecimal("42"));
3333
}
3434

35-
@Secured({ "ROLE_HR" })
36-
public void updateSalary(String employeeId, BigDecimal newSalary) {
37-
// empty
35+
@Secured("ROLE_HR")
36+
public Mono<Void> updateSalary(String employeeId, BigDecimal newSalary) {
37+
return Mono.empty();
3838
}
3939

4040
}

samples/webflux-security/src/main/java/io/spring/sample/graphql/SecurityConfig.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,7 +35,7 @@ public class SecurityConfig {
3535
@Bean
3636
SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) throws Exception {
3737
return http
38-
.csrf(spec -> spec.disable())
38+
.csrf(c -> c.disable())
3939
// Demonstrate that method security works
4040
// Best practice to use both for defense in depth
4141
.authorizeExchange(requests -> requests.anyExchange().permitAll())

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type Employee {
1414

1515
input UpdateSalaryInput {
1616
employeeId: ID!
17-
salary: String!
17+
newSalary: String!
1818
}
1919
type UpdateSalaryPayload {
2020
success: Boolean!

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

+17
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
*/
1616
package io.spring.sample.graphql;
1717

18+
import java.math.BigDecimal;
1819
import java.net.URI;
1920
import java.time.Duration;
2021

2122
import org.junit.jupiter.api.AfterEach;
2223
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Disabled;
2325
import org.junit.jupiter.api.Test;
2426

2527
import org.springframework.boot.test.context.SpringBootTest;
@@ -105,6 +107,21 @@ void canNotQuerySalary() {
105107
});
106108
}
107109

110+
@Disabled // This does not work currently
111+
@Test
112+
void canNotMutateUpdateSalary() {
113+
SalaryInput salaryInput = new SalaryInput("1", BigDecimal.valueOf(44));
114+
115+
this.graphQlTester.documentName("updateSalary")
116+
.variable("salaryInput", salaryInput)
117+
.execute()
118+
.errors()
119+
.satisfy(errors -> {
120+
assertThat(errors).hasSize(1);
121+
assertThat(errors.get(0).getErrorType()).isEqualTo(ErrorType.UNAUTHORIZED);
122+
});
123+
}
124+
108125
@Test
109126
void canQuerySalaryAsAdmin() {
110127

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
mutation updateSalary($salaryInput: UpdateSalaryInput!) {
2+
updateSalary(input: $salaryInput) {
3+
success
4+
employee {
5+
id
6+
name
7+
}
8+
}
9+
}

samples/webmvc-http-security/src/main/java/io/spring/sample/graphql/SalaryService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public BigDecimal getSalaryForEmployee(Employee employee) {
1515
return new BigDecimal("42");
1616
}
1717

18-
@Secured({ "ROLE_HR" })
18+
@Secured("ROLE_HR")
1919
public void updateSalary(String employeeId, BigDecimal newSalary) {
2020

2121
}

samples/webmvc-http-security/src/main/java/io/spring/sample/graphql/SecurityConfig.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
*/
116
package io.spring.sample.graphql;
217

318
import org.springframework.context.annotation.Bean;
@@ -23,9 +38,7 @@ DefaultSecurityFilterChain springWebFilterChain(HttpSecurity http) throws Except
2338
.csrf(c -> c.disable())
2439
// Demonstrate that method security works
2540
// Best practice to use both for defense in depth
26-
.authorizeRequests(requests -> requests
27-
.anyRequest().permitAll()
28-
)
41+
.authorizeRequests(requests -> requests.anyRequest().permitAll())
2942
.httpBasic(withDefaults())
3043
.build();
3144
}

samples/webmvc-http-security/src/test/java/io/spring/sample/graphql/WebMvcHttpSecuritySampleTests.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,10 @@ void canNotQuerySalary() {
7575
}
7676

7777
@Test
78-
void canNotMutationUpdateSalary() {
79-
WebGraphQlTester tester = this.graphQlTester.mutate().build();
78+
void canNotMutateUpdateSalary() {
8079
SalaryInput salaryInput = new SalaryInput("1", BigDecimal.valueOf(44));
8180

82-
tester.documentName("updateSalary")
81+
this.graphQlTester.documentName("updateSalary")
8382
.variable("salaryInput", salaryInput)
8483
.execute()
8584
.errors()

0 commit comments

Comments
 (0)