Skip to content

Commit 17da9c7

Browse files
Sauli Ketolakrisztian-toth
Sauli Ketola
authored andcommitted
Add Resource Server Sample
Fixes spring-projectsgh-4
1 parent 48cc835 commit 17da9c7

File tree

5 files changed

+147
-0
lines changed

5 files changed

+147
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apply plugin: 'io.spring.convention.spring-sample-boot'
2+
3+
dependencies {
4+
implementation 'org.springframework.boot:spring-boot-starter-web'
5+
implementation 'org.springframework.security:spring-security-config'
6+
implementation 'org.springframework.security:spring-security-oauth2-resource-server'
7+
implementation 'org.springframework.security:spring-security-oauth2-jose'
8+
testImplementation('org.springframework.boot:spring-boot-starter-test') {
9+
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
10+
}
11+
}
12+
13+
test {
14+
useJUnitPlatform()
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2020 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 sample;
17+
18+
import org.springframework.web.bind.annotation.GetMapping;
19+
import org.springframework.web.bind.annotation.RestController;
20+
21+
@RestController
22+
public class ResourceController {
23+
24+
@GetMapping("/")
25+
public String resource() {
26+
return "resource";
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2020 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 sample;
17+
18+
import org.springframework.boot.SpringApplication;
19+
import org.springframework.boot.autoconfigure.SpringBootApplication;
20+
21+
@SpringBootApplication
22+
public class ResourceServerApplication {
23+
24+
public static void main(String[] args) {
25+
SpringApplication.run(ResourceServerApplication.class, args);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
spring:
2+
security:
3+
oauth2:
4+
resourceserver:
5+
jwt:
6+
jwk-set-uri: https://localhost:8090/oauth2/keys
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2020 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 sample;
17+
18+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
19+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
20+
21+
import java.time.Instant;
22+
import java.util.HashMap;
23+
import java.util.Map;
24+
25+
import org.junit.jupiter.api.Test;
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
28+
import org.springframework.boot.test.context.SpringBootTest;
29+
import org.springframework.boot.test.context.TestConfiguration;
30+
import org.springframework.context.annotation.Bean;
31+
import org.springframework.security.oauth2.jwt.Jwt;
32+
import org.springframework.security.oauth2.jwt.JwtDecoder;
33+
import org.springframework.test.web.servlet.MockMvc;
34+
35+
@SpringBootTest
36+
@AutoConfigureMockMvc
37+
public class ResourceControllerTests {
38+
39+
@Autowired
40+
private MockMvc mockMvc;
41+
42+
@Test
43+
public void shouldReturnOkWithToken() throws Exception {
44+
this.mockMvc.perform(get("/").header("Authorization", "Bearer TOKEN"))
45+
.andExpect(status().isOk());
46+
}
47+
48+
@Test
49+
public void shouldReturnUnauthorizedWithoutToken() throws Exception {
50+
this.mockMvc.perform(get("/"))
51+
.andExpect(status().isUnauthorized());
52+
}
53+
54+
@TestConfiguration
55+
static class ResourceControllerTestConfiguration {
56+
@Bean
57+
public JwtDecoder jwtDecoder() {
58+
return (token) -> {
59+
Map<String, Object> headers = new HashMap<>();
60+
headers.put("alg", "RS256");
61+
headers.put("typ", "JWT");
62+
63+
Map<String, Object> claims = new HashMap<>();
64+
claims.put("sub", "1234567");
65+
claims.put("name", "John Doe");
66+
return new Jwt(token, Instant.now(), Instant.now().plusMillis(5000), headers, claims);
67+
};
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)