File tree Expand file tree Collapse file tree 7 files changed +97
-8
lines changed
spring-cloud-gateway/api-gateway
java/com/example/apigateway
java/com/example/apigateway Expand file tree Collapse file tree 7 files changed +97
-8
lines changed Original file line number Diff line number Diff line change 1515 <description >Demo project for Spring Boot</description >
1616
1717 <properties >
18- <java .version>1.8 </java .version>
18+ <java .version>11 </java .version>
1919 <spring-cloud .version>Greenwich.SR2</spring-cloud .version>
2020 </properties >
2121
4444 <groupId >org.springframework.cloud</groupId >
4545 <artifactId >spring-cloud-starter-openfeign</artifactId >
4646 </dependency >
47-
47+ <dependency >
48+ <groupId >com.okta.spring</groupId >
49+ <artifactId >okta-spring-boot-starter</artifactId >
50+ <version >1.2.1</version >
51+ </dependency >
52+ <dependency >
53+ <groupId >org.springframework.cloud</groupId >
54+ <artifactId >spring-cloud-security</artifactId >
55+ </dependency >
4856 <dependency >
4957 <groupId >org.projectlombok</groupId >
5058 <artifactId >lombok</artifactId >
7583 </dependencyManagement >
7684
7785 <build >
86+ <defaultGoal >spring-boot:run</defaultGoal >
7887 <plugins >
7988 <plugin >
8089 <groupId >org.springframework.boot</groupId >
Original file line number Diff line number Diff line change 22
33import org .springframework .boot .SpringApplication ;
44import org .springframework .boot .autoconfigure .SpringBootApplication ;
5+ import org .springframework .cloud .gateway .route .RouteLocator ;
6+ import org .springframework .cloud .gateway .route .builder .RouteLocatorBuilder ;
7+ import org .springframework .cloud .security .oauth2 .gateway .TokenRelayGatewayFilterFactory ;
8+ import org .springframework .context .annotation .Bean ;
59
610@ SpringBootApplication
711public class ApiGatewayApplication {
812
9- public static void main (String [] args ) {
10- SpringApplication .run (ApiGatewayApplication .class , args );
11- }
13+ public static void main (String [] args ) {
14+ SpringApplication .run (ApiGatewayApplication .class , args );
15+ }
1216
17+ @ Bean
18+ public RouteLocator customRouteLocator (RouteLocatorBuilder builder , TokenRelayGatewayFilterFactory filterFactory ) {
19+ return builder .routes ()
20+ .route ("car-service" , r -> r .path ("/cars" )
21+ .filters (f -> f .filter (filterFactory .apply ()))
22+ .uri ("lb://car-service/cars" ))
23+ .build ();
24+ }
1325}
Original file line number Diff line number Diff line change 1+ package com .example .apigateway ;
2+
3+ import org .springframework .context .annotation .Configuration ;
4+ import org .springframework .web .reactive .config .CorsRegistry ;
5+ import org .springframework .web .reactive .config .EnableWebFlux ;
6+ import org .springframework .web .reactive .config .WebFluxConfigurer ;
7+
8+ @ Configuration
9+ @ EnableWebFlux
10+ public class CorsConfiguration implements WebFluxConfigurer {
11+
12+ @ Override
13+ public void addCorsMappings (CorsRegistry corsRegistry ) {
14+ corsRegistry .addMapping ("/**" )
15+ .allowCredentials (true )
16+ .allowedOrigins ("*" )
17+ .allowedMethods ("*" )
18+ .maxAge (3600 );
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ package com .example .apigateway ;
2+
3+ import org .springframework .context .annotation .Bean ;
4+ import org .springframework .security .config .annotation .method .configuration .EnableReactiveMethodSecurity ;
5+ import org .springframework .security .config .annotation .web .reactive .EnableWebFluxSecurity ;
6+ import org .springframework .security .config .web .server .ServerHttpSecurity ;
7+ import org .springframework .security .web .server .SecurityWebFilterChain ;
8+
9+ @ EnableWebFluxSecurity
10+ @ EnableReactiveMethodSecurity
11+ public class SecurityConfiguration {
12+
13+ @ Bean
14+ public SecurityWebFilterChain securityWebFilterChain (ServerHttpSecurity http ) {
15+ // @formatter:off
16+ http
17+ .authorizeExchange ()
18+ .anyExchange ().authenticated ()
19+ .and ()
20+ .oauth2Login ()
21+ .and ()
22+ .oauth2ResourceServer ()
23+ .jwt ();
24+ return http .build ();
25+ // @formatter:on
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ spring.application.name =gateway
2+ okta.oauth2.issuer =https://dev-133320.okta.com/oauth2/default
3+ okta.oauth2.client-id =0oazig0adjD1PgAjO356
4+ okta.oauth2.client-secret =iNxF-y6iJACN2eeY8MO-bJ7IdhcSEjt1YXrrNfc0
15
6+ logging.level.root =WARN
7+ logging.level.org.springframework =INFO
Original file line number Diff line number Diff line change 22
33import org .junit .Test ;
44import org .junit .runner .RunWith ;
5+ import org .springframework .beans .factory .annotation .Autowired ;
56import org .springframework .boot .test .context .SpringBootTest ;
67import org .springframework .test .context .junit4 .SpringRunner ;
8+ import org .springframework .test .web .reactive .server .WebTestClient ;
79
810@ RunWith (SpringRunner .class )
9- @ SpringBootTest
11+ @ SpringBootTest ( webEnvironment = SpringBootTest . WebEnvironment . RANDOM_PORT )
1012public class ApiGatewayApplicationTests {
1113
14+ @ Autowired
15+ WebTestClient webTestClient ;
16+
1217 @ Test
13- public void contextLoads () {
14- }
18+ public void testCorsConfiguration () {
19+ WebTestClient .ResponseSpec response = webTestClient .put ()
20+ .uri ("/" )
21+ .header ("Origin" , "http://example.com" )
22+ .exchange ();
1523
24+ response .expectHeader ().valueEquals ("Access-Control-Allow-Origin" , "*" );
25+ }
1626}
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <configuration >
3+ <include resource =" org/springframework/boot/logging/logback/base.xml" />
4+ <logger name =" org.springframework" level =" INFO" />
5+ </configuration >
You can’t perform that action at this time.
0 commit comments