Skip to content

Commit 45abdcd

Browse files
author
Sauli Ketola
committed
Add Resource Server Sample
1 parent 82be38c commit 45abdcd

File tree

6 files changed

+139
-0
lines changed

6 files changed

+139
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
plugins {
2+
id 'org.springframework.boot' version '2.2.2.RELEASE'
3+
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
4+
id 'java'
5+
}
6+
7+
group = 'sample'
8+
version = '0.0.1-SNAPSHOT'
9+
sourceCompatibility = '1.8'
10+
11+
repositories {
12+
mavenCentral()
13+
}
14+
15+
dependencies {
16+
implementation 'org.springframework.boot:spring-boot-starter-web'
17+
implementation 'org.springframework.security:spring-security-oauth2-resource-server:5.3.1.RELEASE'
18+
implementation 'org.springframework.security:spring-security-oauth2-jose:5.3.1.RELEASE'
19+
implementation 'org.springframework.security:spring-security-config:5.3.1.RELEASE'
20+
21+
testImplementation('org.springframework.boot:spring-boot-starter-test') {
22+
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
23+
}
24+
}
25+
26+
test {
27+
useJUnitPlatform()
28+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rootProject.name = 'resource-server-sample'
2+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
/**
22+
* @author Sauli Ketola
23+
*/
24+
@SpringBootApplication
25+
public class ResourceServerApplication {
26+
27+
public static void main(String[] args) {
28+
SpringApplication.run(ResourceServerApplication.class, args);
29+
}
30+
31+
}
Lines changed: 29 additions & 0 deletions
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 SampleResource {
23+
24+
@GetMapping("/")
25+
public String getSample() {
26+
return "sample";
27+
}
28+
29+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# See https://docs.spring.io/spring-security/site/docs/current/reference/html5/#oauth2resourceserver
2+
# for more configuration options
3+
spring:
4+
security:
5+
oauth2:
6+
resourceserver:
7+
jwt:
8+
issuer-uri: https://accounts.google.com
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.MockMvcResultHandlers.print;
20+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
25+
import org.springframework.boot.test.context.SpringBootTest;
26+
import org.springframework.test.web.servlet.MockMvc;
27+
28+
@SpringBootTest
29+
@AutoConfigureMockMvc
30+
public class SampleResourceTest {
31+
32+
@Autowired
33+
private MockMvc mockMvc;
34+
35+
@Test
36+
public void shouldReturnUnauthorized() throws Exception {
37+
this.mockMvc.perform(get("/").header("Authorization", "Bearer TOKEN")).andDo(print())
38+
.andExpect(status().isUnauthorized());
39+
}
40+
41+
}

0 commit comments

Comments
 (0)