Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save abhi2495/a7dbe58a99344430389855b37b7a0523 to your computer and use it in GitHub Desktop.
Save abhi2495/a7dbe58a99344430389855b37b7a0523 to your computer and use it in GitHub Desktop.
##################################################################################
##################################################################################
######### IF YOU FOUND THIS GIST USEFUL, PLEASE LEAVE A STAR. THANKS. ############
##################################################################################
##################################################################################
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: <ISSUER URI of the OIDC supported IAM Provider>
OR
spring:
security:
oauth2:
resourceserver:
jwt:
jwk-set-uri: <JWK SET URI of the OIDC supported IAM Provider>
plugins {
id 'org.springframework.boot' version '2.3.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
//Relevant dependencies
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
}
@RestController
public class DemoController {
@GetMapping(value = "/greet")
public Mono<String> greet() {
return Mono.just("Hello from Demo Project");
}
}

To access the api in this example, first we have to procure the Auth Token (using one of the OAuth2 Flows) containing a scope "canGreet".

Assumption is that the Authorization Server supports OpenId Connect 1.0 specifications.

@EnableWebFluxSecurity
public class WebSecurityConfiguration {
private static final String ACTUATOR_ENDPOINT_PATTERN = "/actuator/*";
@Bean
SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeExchange()
.pathMatchers(ACTUATOR_ENDPOINT_PATTERN)
.permitAll()
.pathMatchers("/greet")
.hasAuthority("SCOPE_canGreet")
.anyExchange().authenticated()
.and()
.oauth2ResourceServer()
.jwt();
return http.build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment