Pivot Resource Server Sample

Changed sample to manage its own JwtDecoder, allowing the Nimbus
Jwt Decoder Builder API to evolve during milestone development.
This commit is contained in:
Josh Cummings 2019-08-17 00:22:16 -06:00
parent 0ecffb0840
commit 10a9207cd5
No known key found for this signature in database
GPG Key ID: 49EF60DD7FF83443

View File

@ -15,12 +15,15 @@
*/ */
package sample; package sample;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
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;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.http.HttpMethod; import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import static org.springframework.security.config.Customizer.withDefaults; import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
/** /**
* @author Josh Cummings * @author Josh Cummings
@ -28,6 +31,8 @@ import static org.springframework.security.config.Customizer.withDefaults;
@EnableWebSecurity @EnableWebSecurity
public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfigurerAdapter { public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Value("${spring.security.oauth2.resourceserver.jwt.jwk-set-uri}") String jwkSetUri;
@Override @Override
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
// @formatter:off // @formatter:off
@ -38,10 +43,12 @@ public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfig
.antMatchers(HttpMethod.POST, "/message/**").hasAuthority("SCOPE_message:write") .antMatchers(HttpMethod.POST, "/message/**").hasAuthority("SCOPE_message:write")
.anyRequest().authenticated() .anyRequest().authenticated()
) )
.oauth2ResourceServer(oauth2ResourceServer -> .oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
oauth2ResourceServer
.jwt(withDefaults())
);
// @formatter:on // @formatter:on
} }
@Bean
JwtDecoder jwtDecoder() {
return NimbusJwtDecoder.withJwkSetUri(this.jwkSetUri).build();
}
} }