Skip to content

Commit bd10b31

Browse files
author
Matt Raible
committed
Integrate Okta and try to test
1 parent 7bd1ab1 commit bd10b31

File tree

6 files changed

+357
-93
lines changed

6 files changed

+357
-93
lines changed
Lines changed: 92 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,100 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
5-
<parent>
6-
<groupId>org.springframework.boot</groupId>
7-
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>2.1.6.RELEASE</version>
9-
<relativePath/> <!-- lookup parent from repository -->
10-
</parent>
11-
<groupId>com.example</groupId>
12-
<artifactId>car-service</artifactId>
13-
<version>0.0.1-SNAPSHOT</version>
14-
<name>car-service</name>
15-
<description>Demo project for Spring Boot</description>
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.6.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.example</groupId>
12+
<artifactId>car-service</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>car-service</name>
15+
<description>Demo project for Spring Boot</description>
1616

17-
<properties>
18-
<java.version>1.8</java.version>
19-
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
20-
</properties>
17+
<properties>
18+
<java.version>11</java.version>
19+
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
20+
</properties>
2121

22-
<dependencies>
23-
<dependency>
24-
<groupId>org.springframework.boot</groupId>
25-
<artifactId>spring-boot-starter-actuator</artifactId>
26-
</dependency>
27-
<dependency>
28-
<groupId>org.springframework.boot</groupId>
29-
<artifactId>spring-boot-starter-data-jpa</artifactId>
30-
</dependency>
31-
<dependency>
32-
<groupId>org.springframework.boot</groupId>
33-
<artifactId>spring-boot-starter-data-rest</artifactId>
34-
</dependency>
35-
<dependency>
36-
<groupId>org.springframework.boot</groupId>
37-
<artifactId>spring-boot-starter-webflux</artifactId>
38-
</dependency>
39-
<dependency>
40-
<groupId>org.springframework.cloud</groupId>
41-
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
42-
</dependency>
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-actuator</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-webflux</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.cloud</groupId>
37+
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>com.okta.spring</groupId>
41+
<artifactId>okta-spring-boot-starter</artifactId>
42+
<version>1.2.1</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-devtools</artifactId>
47+
<scope>runtime</scope>
48+
<optional>true</optional>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.projectlombok</groupId>
52+
<artifactId>lombok</artifactId>
53+
<optional>true</optional>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.springframework.boot</groupId>
57+
<artifactId>spring-boot-starter-test</artifactId>
58+
<scope>test</scope>
59+
</dependency>
60+
<!-- note: need to add for @WithMockUser -->
61+
<dependency>
62+
<groupId>org.springframework.security</groupId>
63+
<artifactId>spring-security-test</artifactId>
64+
<scope>test</scope>
65+
</dependency>
66+
<dependency>
67+
<groupId>de.flapdoodle.embed</groupId>
68+
<artifactId>de.flapdoodle.embed.mongo</artifactId>
69+
<!--<scope>test</scope>-->
70+
</dependency>
71+
<dependency>
72+
<groupId>io.projectreactor</groupId>
73+
<artifactId>reactor-test</artifactId>
74+
<scope>test</scope>
75+
</dependency>
76+
</dependencies>
4377

44-
<dependency>
45-
<groupId>org.springframework.boot</groupId>
46-
<artifactId>spring-boot-devtools</artifactId>
47-
<scope>runtime</scope>
48-
<optional>true</optional>
49-
</dependency>
50-
<dependency>
51-
<groupId>com.h2database</groupId>
52-
<artifactId>h2</artifactId>
53-
<scope>runtime</scope>
54-
</dependency>
55-
<dependency>
56-
<groupId>org.projectlombok</groupId>
57-
<artifactId>lombok</artifactId>
58-
<optional>true</optional>
59-
</dependency>
60-
<dependency>
61-
<groupId>org.springframework.boot</groupId>
62-
<artifactId>spring-boot-starter-test</artifactId>
63-
<scope>test</scope>
64-
</dependency>
65-
<dependency>
66-
<groupId>io.projectreactor</groupId>
67-
<artifactId>reactor-test</artifactId>
68-
<scope>test</scope>
69-
</dependency>
70-
</dependencies>
78+
<dependencyManagement>
79+
<dependencies>
80+
<dependency>
81+
<groupId>org.springframework.cloud</groupId>
82+
<artifactId>spring-cloud-dependencies</artifactId>
83+
<version>${spring-cloud.version}</version>
84+
<type>pom</type>
85+
<scope>import</scope>
86+
</dependency>
87+
</dependencies>
88+
</dependencyManagement>
7189

72-
<dependencyManagement>
73-
<dependencies>
74-
<dependency>
75-
<groupId>org.springframework.cloud</groupId>
76-
<artifactId>spring-cloud-dependencies</artifactId>
77-
<version>${spring-cloud.version}</version>
78-
<type>pom</type>
79-
<scope>import</scope>
80-
</dependency>
81-
</dependencies>
82-
</dependencyManagement>
83-
84-
<build>
85-
<plugins>
86-
<plugin>
87-
<groupId>org.springframework.boot</groupId>
88-
<artifactId>spring-boot-maven-plugin</artifactId>
89-
</plugin>
90-
</plugins>
91-
</build>
90+
<build>
91+
<defaultGoal>spring-boot:run</defaultGoal>
92+
<plugins>
93+
<plugin>
94+
<groupId>org.springframework.boot</groupId>
95+
<artifactId>spring-boot-maven-plugin</artifactId>
96+
</plugin>
97+
</plugins>
98+
</build>
9299

93100
</project>
Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,102 @@
11
package com.example.carservice;
22

3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.boot.ApplicationRunner;
38
import org.springframework.boot.SpringApplication;
49
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.data.annotation.Id;
13+
import org.springframework.data.mongodb.core.mapping.Document;
14+
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
15+
import org.springframework.http.HttpStatus;
16+
import org.springframework.http.ResponseEntity;
17+
import org.springframework.web.bind.annotation.*;
18+
import reactor.core.publisher.Flux;
19+
import reactor.core.publisher.Mono;
520

21+
import java.time.LocalDate;
22+
import java.time.Month;
23+
import java.util.Set;
24+
import java.util.UUID;
25+
26+
@EnableEurekaClient
627
@SpringBootApplication
28+
@Slf4j
729
public class CarServiceApplication {
830

9-
public static void main(String[] args) {
10-
SpringApplication.run(CarServiceApplication.class, args);
11-
}
31+
public static void main(String[] args) {
32+
SpringApplication.run(CarServiceApplication.class, args);
33+
}
34+
35+
@Bean
36+
ApplicationRunner init(CarRepository repository) {
37+
38+
// Names from https://www.vw.com/electric-concepts/
39+
// Dates from https://www.motor1.com/features/346407/volkswagen-id-price-on-sale/
40+
Car ID = new Car(UUID.randomUUID(), "ID.", LocalDate.of(2019, Month.DECEMBER, 1));
41+
Car ID_CROZZ = new Car(UUID.randomUUID(), "ID. CROZZ", LocalDate.of(2021, Month.MAY, 1));
42+
Car ID_VIZZION = new Car(UUID.randomUUID(), "ID. VIZZION", LocalDate.of(2021, Month.DECEMBER, 1));
43+
Car ID_BUZZ = new Car(UUID.randomUUID(), "ID. BUZZ", LocalDate.of(2021, Month.DECEMBER, 1));
44+
Set<Car> vwConcepts = Set.of(ID, ID_BUZZ, ID_CROZZ, ID_VIZZION);
45+
46+
return args -> {
47+
repository
48+
.deleteAll()
49+
.thenMany(
50+
Flux
51+
.just(vwConcepts)
52+
.flatMap(repository::saveAll)
53+
)
54+
.thenMany(repository.findAll())
55+
.subscribe(car -> log.info("saving " + car.toString()));
56+
};
57+
}
58+
}
59+
60+
@Document
61+
@Data
62+
@NoArgsConstructor
63+
@AllArgsConstructor
64+
class Car {
65+
@Id
66+
private UUID id;
67+
private String name;
68+
private LocalDate releaseDate;
69+
}
70+
71+
interface CarRepository extends ReactiveMongoRepository<Car, UUID> {
72+
}
73+
74+
@RestController
75+
class CarController {
76+
77+
private CarRepository carRepository;
78+
79+
public CarController(CarRepository carRepository) {
80+
this.carRepository = carRepository;
81+
}
82+
83+
@PostMapping("/cars")
84+
@ResponseStatus(HttpStatus.CREATED)
85+
public Mono<Car> addCar(@RequestBody Car car) {
86+
return carRepository.save(car);
87+
}
88+
89+
@GetMapping("/cars")
90+
public Flux<Car> getCars() {
91+
return carRepository.findAll();
92+
}
1293

94+
@DeleteMapping("/cars/{id}")
95+
public Mono<ResponseEntity<Void>> deleteCar(@PathVariable("id") UUID id) {
96+
return carRepository.findById(id)
97+
.flatMap(car -> carRepository.delete(car)
98+
.then(Mono.just(new ResponseEntity<Void>(HttpStatus.OK)))
99+
)
100+
.defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND));
101+
}
13102
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.example.carservice;
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+
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
1+
server.port=8081
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

0 commit comments

Comments
 (0)