mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-06-01 09:42:13 +00:00
Resource Server Static Sample Uses @Value For Key
Issue: gh-6494
This commit is contained in:
parent
1c25fe26c9
commit
b252371de7
@ -15,11 +15,9 @@
|
|||||||
*/
|
*/
|
||||||
package sample;
|
package sample;
|
||||||
|
|
||||||
import java.security.KeyFactory;
|
|
||||||
import java.security.interfaces.RSAPublicKey;
|
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.context.annotation.Bean;
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
@ -33,6 +31,9 @@ import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
|
|||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfigurerAdapter {
|
public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
|
@Value("${spring.security.oauth2.resourceserver.jwt.key-value}")
|
||||||
|
RSAPublicKey key;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(HttpSecurity http) throws Exception {
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
// @formatter:off
|
// @formatter:off
|
||||||
@ -49,16 +50,6 @@ public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfig
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
JwtDecoder jwtDecoder() throws Exception {
|
JwtDecoder jwtDecoder() throws Exception {
|
||||||
return NimbusJwtDecoder.withPublicKey(key()).build();
|
return NimbusJwtDecoder.withPublicKey(this.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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,6 @@
|
|||||||
|
spring:
|
||||||
|
security:
|
||||||
|
oauth2:
|
||||||
|
resourceserver:
|
||||||
|
jwt:
|
||||||
|
key-value: classpath:simple.pub
|
@ -0,0 +1,7 @@
|
|||||||
|
-----BEGIN PUBLIC KEY-----
|
||||||
|
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdlatRjRjogo3WojgGHFHYLugd
|
||||||
|
UWAY9iR3fy4arWNA1KoS8kVw33cJibXr8bvwUAUparCwlvdbH6dvEOfou0/gCFQs
|
||||||
|
HUfQrSDv+MuSUMAe8jzKE4qW+jK+xQU9a03GUnKHkkle+Q0pX/g6jXZ7r1/xAK5D
|
||||||
|
o2kQ+X5xK9cipRgEKwIDAQAB
|
||||||
|
-----END PUBLIC KEY-----
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user