Migrated to webflux. Matrix lib disabled.

This commit is contained in:
2021-03-29 12:48:07 +02:00
parent e245bd8f82
commit 2549d91720
7 changed files with 55 additions and 54 deletions

View File

@@ -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<KotlinCompile> {

View File

@@ -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<String>) {

View File

@@ -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<User>,
var departureDate: String,
var origin: String,

View File

@@ -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)
}
}

View File

@@ -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)
}
}

View File

@@ -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")

View File

@@ -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<User>, 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)
}
}