Skip to content

Commit

Permalink
fix :: some websocket hack (Sec-WebSocket-Protocol)
Browse files Browse the repository at this point in the history
  • Loading branch information
jombidev committed Sep 12, 2024
1 parent d85dc3f commit f37fd05
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
29 changes: 24 additions & 5 deletions src/main/kotlin/com/teamapi/palette/config/SessionConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,35 @@ package com.teamapi.palette.config
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.session.data.redis.config.annotation.web.server.EnableRedisWebSession
import org.springframework.web.server.session.HeaderWebSessionIdResolver
import org.springframework.web.server.ServerWebExchange
import org.springframework.web.server.session.WebSessionIdResolver

@Configuration
@EnableRedisWebSession(maxInactiveIntervalInSeconds = 60 * 60 * 3) // 3h
class SessionConfig {
companion object {
private const val SESSION_HEADER = "x-auth-token"
}
@Bean // change or no
fun webSessionIdResolver(): WebSessionIdResolver {
val sessionIdResolver = HeaderWebSessionIdResolver()
sessionIdResolver.headerName = "X-AUTH-Token"
val sessionIdResolver = object : WebSessionIdResolver {

override fun resolveSessionIds(exchange: ServerWebExchange): MutableList<String> {
val headers = exchange.request.headers
val h = headers.getOrDefault(SESSION_HEADER, headers.getOrEmpty("Sec-WebSocket-Protocol" /* WS Hack */))
println(h)
return h
}

override fun setSessionId(exchange: ServerWebExchange, sessionId: String) {
exchange.response.headers.set(SESSION_HEADER, sessionId)
}

override fun expireSession(exchange: ServerWebExchange) {
setSessionId(exchange, "")
}

}
return sessionIdResolver
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class CustomWebSocketHandler(

@OptIn(ObsoleteCoroutinesApi::class)
suspend fun handleSuspend(session: WebSocketSession) {
log.info("SubProtocol info: {}", session.handshakeInfo.subProtocol)
val principal = session.handshakeInfo.principal.awaitSingle() as Authentication

val roomId = getRoomIdFromUrl(session.handshakeInfo.uri)
Expand Down

0 comments on commit f37fd05

Please sign in to comment.