Pārlūkot izejas kodu

Add Resource Server Sample

Fixes gh-4
Sauli Ketola 5 gadi atpakaļ
vecāks
revīzija
b42ffecdf6

+ 15 - 0
samples/boot/oauth2resourceserver/spring-authorization-server-samples-boot-oauth2resourceserver.gradle

@@ -0,0 +1,15 @@
+apply plugin: 'io.spring.convention.spring-sample-boot'
+
+dependencies {
+	implementation 'org.springframework.boot:spring-boot-starter-web'
+	implementation 'org.springframework.security:spring-security-config'
+	implementation 'org.springframework.security:spring-security-oauth2-resource-server'
+	implementation 'org.springframework.security:spring-security-oauth2-jose'
+	testImplementation('org.springframework.boot:spring-boot-starter-test') {
+		exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
+	}
+}
+
+test {
+	useJUnitPlatform()
+}

+ 29 - 0
samples/boot/oauth2resourceserver/src/main/java/sample/ResourceController.java

@@ -0,0 +1,29 @@
+/*
+ * Copyright 2020 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sample;
+
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class ResourceController {
+
+	@GetMapping("/")
+	public String resource() {
+		return "resource";
+	}
+
+}

+ 27 - 0
samples/boot/oauth2resourceserver/src/main/java/sample/ResourceServerApplication.java

@@ -0,0 +1,27 @@
+/*
+ * Copyright 2020 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sample;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class ResourceServerApplication {
+
+	public static void main(String[] args) {
+		SpringApplication.run(ResourceServerApplication.class, args);
+	}
+}

+ 6 - 0
samples/boot/oauth2resourceserver/src/main/resources/application.yml

@@ -0,0 +1,6 @@
+spring:
+  security:
+    oauth2:
+      resourceserver:
+        jwt:
+          jwk-set-uri: https://localhost:8090/oauth2/keys

+ 70 - 0
samples/boot/oauth2resourceserver/src/test/java/sample/ResourceControllerTests.java

@@ -0,0 +1,70 @@
+/*
+ * Copyright 2020 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sample;
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+import java.time.Instant;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.context.TestConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.security.oauth2.jwt.Jwt;
+import org.springframework.security.oauth2.jwt.JwtDecoder;
+import org.springframework.test.web.servlet.MockMvc;
+
+@SpringBootTest
+@AutoConfigureMockMvc
+public class ResourceControllerTests {
+
+	@Autowired
+	private MockMvc mockMvc;
+
+	@Test
+	public void shouldReturnOkWithToken() throws Exception {
+		this.mockMvc.perform(get("/").header("Authorization", "Bearer TOKEN"))
+			.andExpect(status().isOk());
+	}
+
+	@Test
+	public void shouldReturnUnauthorizedWithoutToken() throws Exception {
+		this.mockMvc.perform(get("/"))
+			.andExpect(status().isUnauthorized());
+	}
+
+	@TestConfiguration
+	static class ResourceControllerTestConfiguration {
+		@Bean
+		public JwtDecoder jwtDecoder() {
+			return (token) -> {
+				Map<String, Object> headers = new HashMap<>();
+				headers.put("alg", "RS256");
+				headers.put("typ", "JWT");
+
+				Map<String, Object> claims = new HashMap<>();
+				claims.put("sub", "1234567");
+				claims.put("name", "John Doe");
+				return new Jwt(token, Instant.now(), Instant.now().plusMillis(5000), headers, claims);
+			};
+		}
+	}
+}