Fixed spring configuration

This commit is contained in:
slon565 2023-08-11 13:15:29 +04:00
parent 140edcbb6d
commit 638c360975
5 changed files with 32 additions and 34 deletions

View file

@ -21,7 +21,7 @@ repositories {
val exposedVersion = "0.41.1"
val postgresVersion = "42.5.4"
val telegramBotVersion = "6.5.0"
val springBootVersion = "3.1.0"
val springBootVersion = "2.7.14"
val serializationVersion = "1.5.0"
val loggingVersion = "3.0.5"
val securityTestVersion = "6.0.2"
@ -34,16 +34,9 @@ dependencies {
implementation("org.telegram:telegrambotsextensions:$telegramBotVersion")
implementation("org.telegram:telegrambots-spring-boot-starter:$telegramBotVersion")
implementation("org.springframework.boot:spring-boot-starter:$springBootVersion") {
exclude("org.springframework.boot:spring-boot-starter-tomcat")
}
implementation("org.springframework.boot:spring-boot-starter-web:$springBootVersion") {
exclude("org.springframework.boot:spring-boot-starter-tomcat")
}
implementation("org.springframework.boot:spring-boot-starter-security:$springBootVersion") {
exclude("org.springframework.boot:spring-boot-starter-tomcat")
}
implementation("org.springframework.boot:spring-boot-starter-jetty:$springBootVersion")
implementation("org.springframework.boot:spring-boot-starter:$springBootVersion")
implementation("org.springframework.boot:spring-boot-starter-web:$springBootVersion")
implementation("org.springframework.boot:spring-boot-starter-security:$springBootVersion")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion")
implementation("org.jetbrains.kotlin:kotlin-reflect")
@ -58,16 +51,6 @@ dependencies {
testImplementation("org.springframework.boot:spring-boot-starter-test:$springBootVersion")
}
configurations.all {
resolutionStrategy {
eachDependency {
if (requested.group == "jakarta.servlet") {
useVersion("5.0.0")
}
}
}
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")

View file

@ -1,7 +1,5 @@
package ru.sicamp.sicamphelper.api.controller
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.convertValue
import mu.KLogging
import org.springframework.web.bind.annotation.*
import ru.sicamp.sicamphelper.model.dto.UserDto
@ -11,7 +9,6 @@ import ru.sicamp.sicamphelper.service.UserService
@RestController
class UsersController(
private val userService: UserService,
private val objectMapper: ObjectMapper,
) {
@PostMapping("/user/create")
fun create(@RequestParam dto: UserDto): UserInfo {

View file

@ -1,15 +1,16 @@
package ru.sicamp.sicamphelper.repository
import mu.KLogging
import org.jetbrains.exposed.dao.load
import org.jetbrains.exposed.dao.with
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.transactions.transaction
import org.springframework.security.core.userdetails.UsernameNotFoundException
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.stereotype.Repository
import ru.sicamp.sicamphelper.db.entity.User
import ru.sicamp.sicamphelper.db.table.Users
import ru.sicamp.sicamphelper.model.dto.UserDto
import ru.sicamp.sicamphelper.util.badArgument
import java.time.Duration
import java.time.LocalDateTime
@ -21,7 +22,10 @@ class UserRepository(
User.find {
(Users.tgId eq id) and
(Users.tgUsername eq userName)
}.firstOrNull()
}.with(
User::supervisor,
User::group
).firstOrNull()
}
fun findUserByUsername(username: String) = transaction {
@ -30,11 +34,14 @@ class UserRepository(
}.with(
User::supervisor,
User::group
).firstOrNull() ?: badArgument("Not found user with username $username")
).firstOrNull() ?: throw UsernameNotFoundException("Not found user with username $username")
}
fun findUserById(id: Long) = transaction {
User.findById(id)
User.findById(id)?.load(
User::supervisor,
User::group
)
}
fun createUser(userDto: UserDto): User = transaction {

View file

@ -0,0 +1,16 @@
package ru.sicamp.sicamphelper.security
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.stereotype.Service
import ru.sicamp.sicamphelper.db.entity.User
import ru.sicamp.sicamphelper.repository.UserRepository
import ru.sicamp.sicamphelper.util.badArgument
@Service
class UserDetailsServiceImpl(
private val userRepository: UserRepository
) : UserDetailsService {
override fun loadUserByUsername(username: String?): User = userRepository.findUserByUsername(
username = username ?: badArgument("Username shouldn't be null")
)
}

View file

@ -1,6 +1,5 @@
package ru.sicamp.sicamphelper.service
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.stereotype.Service
import ru.sicamp.sicamphelper.db.entity.User
import ru.sicamp.sicamphelper.model.dto.UserDto
@ -12,16 +11,12 @@ import ru.sicamp.sicamphelper.util.badArgument
@Service
class UserService(
private val userRepository: UserRepository
) : UserDetailsService {
) {
fun findUserByTgUser(user: TgUser): User? = userRepository.findUserByTgUsernameAndId(
id = user.id,
userName = user.userName
)
override fun loadUserByUsername(username: String?): User = userRepository.findUserByUsername(
username = username ?: badArgument("Username shouldn't be null")
)
fun findUserInfoByLogin(login: String) = UserInfo.from(
userRepository.findUserByUsername(login)
) ?: badArgument("Not found user with login=$login")