Resource Server Static Sample Uses @Value For Key

Issue: gh-6494
This commit is contained in:
Josh Cummings 2019-02-06 18:14:54 -07:00
parent 1c25fe26c9
commit b252371de7
3 changed files with 18 additions and 14 deletions

View File

@ -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();
}
}

View File

@ -0,0 +1,6 @@
spring:
security:
oauth2:
resourceserver:
jwt:
key-value: classpath:simple.pub

View File

@ -0,0 +1,7 @@
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdlatRjRjogo3WojgGHFHYLugd
UWAY9iR3fy4arWNA1KoS8kVw33cJibXr8bvwUAUparCwlvdbH6dvEOfou0/gCFQs
HUfQrSDv+MuSUMAe8jzKE4qW+jK+xQU9a03GUnKHkkle+Q0pX/g6jXZ7r1/xAK5D
o2kQ+X5xK9cipRgEKwIDAQAB
-----END PUBLIC KEY-----