Switch to file-based H2 DB

Implement development specific config
This commit is contained in:
grimsi
2024-06-10 13:34:11 +02:00
parent 273e9b0a07
commit 042c326380
7 changed files with 55 additions and 28 deletions
+1
View File
@@ -46,3 +46,4 @@ out/
/docker/docker-compose.yml
/.gameyfin/
/frontend/generated
/db/
+1
View File
@@ -1,5 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="GameyfinApplication" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" nameIsGenerated="true">
<option name="ACTIVE_PROFILES" value="dev" />
<module name="gameyfin.main" />
<option name="SPRING_BOOT_MAIN_CLASS" value="de.grimsi.gameyfin.GameyfinApplication" />
<extension name="coverage">
@@ -3,18 +3,20 @@ package de.grimsi.gameyfin.config
import com.vaadin.flow.spring.security.VaadinWebSecurity
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.env.Environment
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.builders.WebSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.security.web.util.matcher.AntPathRequestMatcher
@EnableWebSecurity
@Configuration
class SecurityConfig : VaadinWebSecurity() {
class SecurityConfig(
private val environment: Environment
) : VaadinWebSecurity() {
@Throws(Exception::class)
override fun configure(http: HttpSecurity) {
@@ -31,7 +33,11 @@ class SecurityConfig : VaadinWebSecurity() {
@Throws(Exception::class)
public override fun configure(web: WebSecurity) {
super.configure(web)
web.ignoring().requestMatchers(AntPathRequestMatcher("/images/**"))
web.ignoring().requestMatchers("/images/**")
if ("dev" in environment.activeProfiles) {
web.ignoring().requestMatchers("/h2-console/**")
}
}
@Bean
@@ -29,33 +29,15 @@ class SetupDataLoader(
log.info { "We will now set up some data..." }
setupRoles()
setupUsers()
if ("dev" in env.activeProfiles) {
setupUsers()
}
log.info { "Setup completed..." }
log.info { "Visit http://${InetAddress.getLocalHost().hostName}:${env.getProperty("server.port")}/setup to complete the setup" }
}
fun setupUsers() {
log.info { "Setting up users..." }
val superadmin = User(
username = "admin",
password = "admin"
)
userService.registerUser(superadmin, Roles.SUPERADMIN)
val user = User(
username = "user",
password = "user"
)
userService.registerUser(user, Roles.USER)
log.info { "User setup completed." }
}
fun setupRoles() {
log.info { "Setting up roles..." }
@@ -78,4 +60,30 @@ class SetupDataLoader(
}
return role
}
fun setupUsers() {
log.info { "Setting up users..." }
val superadmin = User(
username = "admin",
password = "admin"
)
registerUserIfNotFound(superadmin, Roles.SUPERADMIN)
val user = User(
username = "user",
password = "user"
)
registerUserIfNotFound(user, Roles.USER)
log.info { "User setup completed." }
}
fun registerUserIfNotFound(user: User, role: Roles) {
if (userService.existsByUsername(user.username)) return
userService.registerUser(user, role)
}
}
@@ -33,11 +33,13 @@ class UserService(
user.enabled,
true,
true,
true,
user.enabled,
toAuthorities(user.roles)
)
}
fun existsByUsername(username: String): Boolean = userRepository.findByUsername(username) != null
fun registerUser(user: User, role: Roles): User {
return registerUser(user, listOf(role))
}
@@ -7,7 +7,7 @@ import de.grimsi.gameyfin.config.Roles
import org.springframework.security.core.userdetails.UserDetails
fun UserDetails.hasRole(role: Roles): Boolean {
return this.authorities.map { a -> a.authority }.contains(role.roleName)
return role.roleName in this.authorities.map { a -> a.authority }
}
fun UserDetails.isAdmin(): Boolean {
+10 -1
View File
@@ -19,10 +19,19 @@ spring:
# Workaround for https://github.com/vaadin/hilla/issues/842
devtools.restart.additional-exclude: dev/hilla/openapi.json
jpa:
defer-datasource-initialization: true
# defer-datasource-initialization: true
database-platform: org.hibernate.dialect.H2Dialect
hibernate:
ddl-auto: update
mustache:
check-template-location: false
sql.init.mode: always
datasource:
username: gfadmin
password: gameyfin
db-name: gameyfin_db
url: jdbc:h2:file:./db/${spring.datasource.db-name}
driverClassName: org.h2.Driver
application:
name: Gameyfin