From b252371de72eb5d9025e9be00304ba3ba99b9803 Mon Sep 17 00:00:00 2001 From: Josh Cummings Date: Wed, 6 Feb 2019 18:14:54 -0700 Subject: [PATCH] Resource Server Static Sample Uses @Value For Key Issue: gh-6494 --- ...h2ResourceServerSecurityConfiguration.java | 19 +++++-------------- .../src/main/resources/application.yml | 6 ++++++ .../src/main/resources/simple.pub | 7 +++++++ 3 files changed, 18 insertions(+), 14 deletions(-) create mode 100644 samples/boot/oauth2resourceserver-static/src/main/resources/application.yml create mode 100644 samples/boot/oauth2resourceserver-static/src/main/resources/simple.pub diff --git a/samples/boot/oauth2resourceserver-static/src/main/java/sample/OAuth2ResourceServerSecurityConfiguration.java b/samples/boot/oauth2resourceserver-static/src/main/java/sample/OAuth2ResourceServerSecurityConfiguration.java index a9c6ef889a..135b6b5b24 100644 --- a/samples/boot/oauth2resourceserver-static/src/main/java/sample/OAuth2ResourceServerSecurityConfiguration.java +++ b/samples/boot/oauth2resourceserver-static/src/main/java/sample/OAuth2ResourceServerSecurityConfiguration.java @@ -15,11 +15,9 @@ */ package sample; -import java.security.KeyFactory; import java.security.interfaces.RSAPublicKey; -import java.security.spec.X509EncodedKeySpec; -import java.util.Base64; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; @@ -33,6 +31,9 @@ import org.springframework.security.oauth2.jwt.NimbusJwtDecoder; @EnableWebSecurity public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfigurerAdapter { + @Value("${spring.security.oauth2.resourceserver.jwt.key-value}") + RSAPublicKey key; + @Override protected void configure(HttpSecurity http) throws Exception { // @formatter:off @@ -49,16 +50,6 @@ public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfig @Bean JwtDecoder jwtDecoder() throws Exception { - return NimbusJwtDecoder.withPublicKey(key()).build(); - } - - private RSAPublicKey key() throws Exception { - String encoded = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdlatRjRjogo3WojgGHFHYLugd" + - "UWAY9iR3fy4arWNA1KoS8kVw33cJibXr8bvwUAUparCwlvdbH6dvEOfou0/gCFQs" + - "HUfQrSDv+MuSUMAe8jzKE4qW+jK+xQU9a03GUnKHkkle+Q0pX/g6jXZ7r1/xAK5D" + - "o2kQ+X5xK9cipRgEKwIDAQAB"; - byte[] bytes = Base64.getDecoder().decode(encoded.getBytes()); - return (RSAPublicKey) KeyFactory.getInstance("RSA") - .generatePublic(new X509EncodedKeySpec(bytes)); + return NimbusJwtDecoder.withPublicKey(this.key).build(); } } diff --git a/samples/boot/oauth2resourceserver-static/src/main/resources/application.yml b/samples/boot/oauth2resourceserver-static/src/main/resources/application.yml new file mode 100644 index 0000000000..123d342ead --- /dev/null +++ b/samples/boot/oauth2resourceserver-static/src/main/resources/application.yml @@ -0,0 +1,6 @@ +spring: + security: + oauth2: + resourceserver: + jwt: + key-value: classpath:simple.pub diff --git a/samples/boot/oauth2resourceserver-static/src/main/resources/simple.pub b/samples/boot/oauth2resourceserver-static/src/main/resources/simple.pub new file mode 100644 index 0000000000..a25c08779e --- /dev/null +++ b/samples/boot/oauth2resourceserver-static/src/main/resources/simple.pub @@ -0,0 +1,7 @@ +-----BEGIN PUBLIC KEY----- +MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdlatRjRjogo3WojgGHFHYLugd +UWAY9iR3fy4arWNA1KoS8kVw33cJibXr8bvwUAUparCwlvdbH6dvEOfou0/gCFQs +HUfQrSDv+MuSUMAe8jzKE4qW+jK+xQU9a03GUnKHkkle+Q0pX/g6jXZ7r1/xAK5D +o2kQ+X5xK9cipRgEKwIDAQAB +-----END PUBLIC KEY----- +