Skip to content

Commit 6f99c34

Browse files
committed
Resource Server Static Sample Uses @value For Key
Issue: gh-6494
1 parent a255b3e commit 6f99c34

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

samples/boot/oauth2resourceserver-static/src/main/java/sample/OAuth2ResourceServerSecurityConfiguration.java

+5-14
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
*/
1616
package sample;
1717

18-
import java.security.KeyFactory;
1918
import java.security.interfaces.RSAPublicKey;
20-
import java.security.spec.X509EncodedKeySpec;
21-
import java.util.Base64;
2219

20+
import org.springframework.beans.factory.annotation.Value;
2321
import org.springframework.context.annotation.Bean;
2422
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
2523
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@@ -33,6 +31,9 @@
3331
@EnableWebSecurity
3432
public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfigurerAdapter {
3533

34+
@Value("${spring.security.oauth2.resourceserver.jwt.key-value}")
35+
RSAPublicKey key;
36+
3637
@Override
3738
protected void configure(HttpSecurity http) throws Exception {
3839
// @formatter:off
@@ -49,16 +50,6 @@ protected void configure(HttpSecurity http) throws Exception {
4950

5051
@Bean
5152
JwtDecoder jwtDecoder() throws Exception {
52-
return NimbusJwtDecoder.withPublicKey(key()).build();
53-
}
54-
55-
private RSAPublicKey key() throws Exception {
56-
String encoded = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdlatRjRjogo3WojgGHFHYLugd" +
57-
"UWAY9iR3fy4arWNA1KoS8kVw33cJibXr8bvwUAUparCwlvdbH6dvEOfou0/gCFQs" +
58-
"HUfQrSDv+MuSUMAe8jzKE4qW+jK+xQU9a03GUnKHkkle+Q0pX/g6jXZ7r1/xAK5D" +
59-
"o2kQ+X5xK9cipRgEKwIDAQAB";
60-
byte[] bytes = Base64.getDecoder().decode(encoded.getBytes());
61-
return (RSAPublicKey) KeyFactory.getInstance("RSA")
62-
.generatePublic(new X509EncodedKeySpec(bytes));
53+
return NimbusJwtDecoder.withPublicKey(this.key).build();
6354
}
6455
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
spring:
2+
security:
3+
oauth2:
4+
resourceserver:
5+
jwt:
6+
key-value: classpath:simple.pub
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdlatRjRjogo3WojgGHFHYLugd
3+
UWAY9iR3fy4arWNA1KoS8kVw33cJibXr8bvwUAUparCwlvdbH6dvEOfou0/gCFQs
4+
HUfQrSDv+MuSUMAe8jzKE4qW+jK+xQU9a03GUnKHkkle+Q0pX/g6jXZ7r1/xAK5D
5+
o2kQ+X5xK9cipRgEKwIDAQAB
6+
-----END PUBLIC KEY-----
7+

0 commit comments

Comments
 (0)