From 2549d91720cafee816e51e78b04e36fa02f21068 Mon Sep 17 00:00:00 2001 From: Eneko Date: Mon, 29 Mar 2021 12:48:07 +0200 Subject: [PATCH] Migrated to webflux. Matrix lib disabled. --- build.gradle.kts | 9 ++-- .../{spring => }/OkupaMiCocheApplication.kt | 7 +-- .../eu/fosil/okupamicoche/entities/Travel.kt | 2 +- .../okupamicoche/spring/ServletInitializer.kt | 12 ----- .../spring/conf/WebSecurityConfig.kt | 47 +++++++++++-------- .../spring/controller/PublicRestController.kt | 1 - .../spring/services/MatrixService.kt | 31 ++++++------ 7 files changed, 55 insertions(+), 54 deletions(-) rename src/main/kotlin/eu/fosil/okupamicoche/{spring => }/OkupaMiCocheApplication.kt (72%) delete mode 100644 src/main/kotlin/eu/fosil/okupamicoche/spring/ServletInitializer.kt diff --git a/build.gradle.kts b/build.gradle.kts index 1ac132c..b660bab 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -20,18 +20,21 @@ repositories { dependencies { implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server") - implementation("org.springframework.boot:spring-boot-starter-web") + implementation("org.springframework.boot:spring-boot-starter-webflux") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") + implementation("io.projectreactor.kotlin:reactor-kotlin-extensions") implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor") implementation("io.github.microutils:kotlin-logging-jvm:2.0.6") // https://mvnrepository.com/artifact/net.folivo/matrix-spring-boot-bot - implementation ("net.folivo:matrix-spring-boot-bot:0.4.6") +// implementation ("net.folivo:matrix-spring-boot-bot:0.4.6") developmentOnly("org.springframework.boot:spring-boot-devtools") runtimeOnly("com.h2database:h2") - runtimeOnly("io.r2dbc:r2dbc-h2") +// runtimeOnly("io.r2dbc:r2dbc-h2") providedRuntime("org.springframework.boot:spring-boot-starter-tomcat") testImplementation("org.springframework.boot:spring-boot-starter-test") + testImplementation("io.projectreactor:reactor-test") } tasks.withType { diff --git a/src/main/kotlin/eu/fosil/okupamicoche/spring/OkupaMiCocheApplication.kt b/src/main/kotlin/eu/fosil/okupamicoche/OkupaMiCocheApplication.kt similarity index 72% rename from src/main/kotlin/eu/fosil/okupamicoche/spring/OkupaMiCocheApplication.kt rename to src/main/kotlin/eu/fosil/okupamicoche/OkupaMiCocheApplication.kt index 18bab4c..7ebdc9a 100644 --- a/src/main/kotlin/eu/fosil/okupamicoche/spring/OkupaMiCocheApplication.kt +++ b/src/main/kotlin/eu/fosil/okupamicoche/OkupaMiCocheApplication.kt @@ -1,4 +1,4 @@ -package eu.fosil.okupamicoche.spring +package eu.fosil.okupamicoche import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.autoconfigure.domain.EntityScan @@ -6,8 +6,9 @@ import org.springframework.boot.runApplication import org.springframework.data.jpa.repository.config.EnableJpaRepositories @SpringBootApplication -@EnableJpaRepositories("eu.fosil.okupamicoche.*") -@EntityScan("eu.fosil.okupamicoche.*") +@EnableJpaRepositories +//@EnableJpaRepositories("eu.fosil.okupamicoche.*") +//@EntityScan("eu.fosil.okupamicoche.*") class OkupaMiCocheApplication fun main(args: Array) { diff --git a/src/main/kotlin/eu/fosil/okupamicoche/entities/Travel.kt b/src/main/kotlin/eu/fosil/okupamicoche/entities/Travel.kt index 052c97a..ca13fc4 100644 --- a/src/main/kotlin/eu/fosil/okupamicoche/entities/Travel.kt +++ b/src/main/kotlin/eu/fosil/okupamicoche/entities/Travel.kt @@ -9,7 +9,7 @@ class Travel( @Id @GeneratedValue var id: TravelId? = null, @ManyToOne var driver: User, - @ManyToMany + @ManyToMany(fetch = FetchType.EAGER) // https://stackoverflow.com/questions/22821695/how-to-fix-hibernate-lazyinitializationexception-failed-to-lazily-initialize-a var travelers: MutableList, var departureDate: String, var origin: String, diff --git a/src/main/kotlin/eu/fosil/okupamicoche/spring/ServletInitializer.kt b/src/main/kotlin/eu/fosil/okupamicoche/spring/ServletInitializer.kt deleted file mode 100644 index 2bec19a..0000000 --- a/src/main/kotlin/eu/fosil/okupamicoche/spring/ServletInitializer.kt +++ /dev/null @@ -1,12 +0,0 @@ -package eu.fosil.okupamicoche.spring - -import org.springframework.boot.builder.SpringApplicationBuilder -import org.springframework.boot.web.servlet.support.SpringBootServletInitializer - -class ServletInitializer : SpringBootServletInitializer() { - - override fun configure(application: SpringApplicationBuilder): SpringApplicationBuilder { - return application.sources(OkupaMiCocheApplication::class.java) - } - -} diff --git a/src/main/kotlin/eu/fosil/okupamicoche/spring/conf/WebSecurityConfig.kt b/src/main/kotlin/eu/fosil/okupamicoche/spring/conf/WebSecurityConfig.kt index e09ea8e..b1fb31c 100644 --- a/src/main/kotlin/eu/fosil/okupamicoche/spring/conf/WebSecurityConfig.kt +++ b/src/main/kotlin/eu/fosil/okupamicoche/spring/conf/WebSecurityConfig.kt @@ -1,33 +1,40 @@ package eu.fosil.okupamicoche.spring.conf +import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration -import org.springframework.security.config.annotation.web.builders.HttpSecurity -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter -import org.springframework.web.servlet.config.annotation.CorsRegistry -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer +import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity +import org.springframework.security.config.web.server.ServerHttpSecurity +import org.springframework.security.web.server.SecurityWebFilterChain +import org.springframework.web.reactive.config.CorsRegistry +import org.springframework.web.reactive.config.EnableWebFlux +import org.springframework.web.reactive.config.WebFluxConfigurer - -@Configuration -class JWTSecurityConfig : WebSecurityConfigurerAdapter() { - @Throws(Exception::class) - override fun configure(http: HttpSecurity) {//@formatter:off - http.cors() +@EnableWebFluxSecurity +class SecurityConfig { + @Bean + fun configure(http: ServerHttpSecurity): SecurityWebFilterChain {//@formatter:off + return http.cors() .and() .csrf().disable() - .authorizeRequests() - .antMatchers("/api/public/**").permitAll() - .antMatchers("/api/user/**").authenticated() - .antMatchers("/api/travel/**").authenticated() - .anyRequest().denyAll() + .authorizeExchange() + .pathMatchers("/_matrix/**").permitAll() + .pathMatchers("/api/public/**").permitAll() + .pathMatchers("/api/user/**").authenticated() + .pathMatchers("/api/travel/**").authenticated() + .anyExchange().denyAll() .and() - .oauth2ResourceServer() - .jwt() + .oauth2ResourceServer().jwt() + .and().and().build() }//@formatter:on } @Configuration -class CorsConfigurer : WebMvcConfigurer { - override fun addCorsMappings(registry: CorsRegistry) { - registry.addMapping("/**").allowedOrigins("http://localhost:4200") +@EnableWebFlux +class CorsGlobalConfiguration : WebFluxConfigurer { + override fun addCorsMappings(corsRegistry: CorsRegistry) { + corsRegistry.addMapping("/**") + .allowedOrigins("http://localhost:4200") +// .allowedMethods("PUT") +// .maxAge(3600) } } diff --git a/src/main/kotlin/eu/fosil/okupamicoche/spring/controller/PublicRestController.kt b/src/main/kotlin/eu/fosil/okupamicoche/spring/controller/PublicRestController.kt index 2848ede..feb9e99 100644 --- a/src/main/kotlin/eu/fosil/okupamicoche/spring/controller/PublicRestController.kt +++ b/src/main/kotlin/eu/fosil/okupamicoche/spring/controller/PublicRestController.kt @@ -15,7 +15,6 @@ import org.springframework.web.bind.annotation.RestController @RestController @RequestMapping("/api/public") -@CrossOrigin(origins = ["http://localhost:4200"]) class PublicRestController(private val travelRepository: TravelRepository) : ApiRestController { @RequestMapping("/list") diff --git a/src/main/kotlin/eu/fosil/okupamicoche/spring/services/MatrixService.kt b/src/main/kotlin/eu/fosil/okupamicoche/spring/services/MatrixService.kt index f1963f8..51f94e0 100644 --- a/src/main/kotlin/eu/fosil/okupamicoche/spring/services/MatrixService.kt +++ b/src/main/kotlin/eu/fosil/okupamicoche/spring/services/MatrixService.kt @@ -3,30 +3,33 @@ package eu.fosil.okupamicoche.spring.services import eu.fosil.okupamicoche.entities.User import eu.fosil.okupamicoche.usecases.matrix.MatrixApi import mu.KotlinLogging -import net.folivo.matrix.core.model.MatrixId -import net.folivo.matrix.restclient.MatrixClient +//import net.folivo.matrix.core.model.MatrixId +//import net.folivo.matrix.restclient.MatrixClient import org.springframework.stereotype.Service import java.util.stream.Collectors @Service -class MatrixService(private val matrixClient: MatrixClient): MatrixApi { +class MatrixService( +// private val matrixClient: MatrixClient + ): MatrixApi { private val logger = KotlinLogging.logger {} override suspend fun createRoom(name: String, alias: String, usersToInvite: Set, topic: String?): String { logger.debug { "Creating room name=$name alias=$alias" } - val usersToInviteId = usersToInvite.stream().map { user -> MatrixId.UserId(user.matrixId) } - val roomId = matrixClient.roomsApi.createRoom( - name = name, - roomAliasId = MatrixId.RoomAliasId("#$alias:synapse"), -// invite = usersToInviteId.collect(Collectors.toSet()), - topic = topic - ) - return roomId.full +// val usersToInviteId = usersToInvite.stream().map { user -> MatrixId.UserId(user.matrixId) } +// val roomId = matrixClient.roomsApi.createRoom( +// name = name, +// roomAliasId = MatrixId.RoomAliasId("#$alias:synapse"), +//// invite = usersToInviteId.collect(Collectors.toSet()), +// topic = topic +// ) +// return roomId.full + return "-1" } override suspend fun inviteUser(roomId: String, user: User) { - val matrixUserId = MatrixId.UserId(user.matrixId) - logger.debug { "Invite user $matrixUserId to room $roomId" } - matrixClient.roomsApi.inviteUser(MatrixId.RoomId(roomId), matrixUserId) +// val matrixUserId = MatrixId.UserId(user.matrixId) +// logger.debug { "Invite user $matrixUserId to room $roomId" } +// matrixClient.roomsApi.inviteUser(MatrixId.RoomId(roomId), matrixUserId) } } \ No newline at end of file