move jwt user and jwt authentication

This commit is contained in:
2025-08-12 15:09:07 +02:00
committed by swordsteel
parent e19e0e59bc
commit b070a22b0e
4 changed files with 9 additions and 6 deletions

View File

@@ -4,6 +4,8 @@ import io.github.oshai.kotlinlogging.KotlinLogging
import io.jsonwebtoken.JwtException
import java.util.UUID
import ltd.hlaeja.jwt.service.PublicJwtService
import ltd.hlaeja.security.user.JwtAuthentication
import ltd.hlaeja.security.user.JwtUserDetails
import org.springframework.http.HttpStatus.UNAUTHORIZED
import org.springframework.security.core.Authentication
import org.springframework.security.core.authority.SimpleGrantedAuthority
@@ -40,7 +42,7 @@ class JwtAuthenticationConverter(
}
private fun jwtAuthenticationToken(token: String) = publicJwtService.verify(token) { claims ->
JwtAuthenticationToken(
JwtAuthentication(
JwtUserDetails(
UUID.fromString(claims.payload["id"] as String),
claims.payload["username"] as String,

View File

@@ -1,5 +1,6 @@
package ltd.hlaeja.security
import ltd.hlaeja.security.user.JwtAuthentication
import org.springframework.security.authentication.ReactiveAuthenticationManager
import org.springframework.security.core.Authentication
import org.springframework.security.core.AuthenticationException
@@ -11,14 +12,14 @@ class JwtAuthenticationManager : ReactiveAuthenticationManager {
override fun authenticate(
authentication: Authentication,
): Mono<Authentication> = if (authentication is JwtAuthenticationToken) {
): Mono<Authentication> = if (authentication is JwtAuthentication) {
handleJwtToken(authentication)
} else {
Mono.error(object : AuthenticationException("Unsupported authentication type") {})
}
private fun handleJwtToken(
token: JwtAuthenticationToken,
token: JwtAuthentication,
): Mono<Authentication> = if (token.isAuthenticated) {
Mono.just(token)
} else {

View File

@@ -1,9 +1,9 @@
package ltd.hlaeja.security
package ltd.hlaeja.security.user
import org.springframework.security.core.Authentication
import org.springframework.security.core.GrantedAuthority
data class JwtAuthenticationToken(
data class JwtAuthentication(
private val jwtUserDetails: JwtUserDetails,
private val token: String,
private var authorities: MutableCollection<out GrantedAuthority>,

View File

@@ -1,4 +1,4 @@
package ltd.hlaeja.security
package ltd.hlaeja.security.user
import java.util.UUID