Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process HTTP session data on websocket handshake and load SecurityContext into Subscriptions #814

Merged
merged 2 commits into from Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions graphql-dgs-subscriptions-websockets/build.gradle.kts
Expand Up @@ -23,6 +23,8 @@ dependencies {
implementation("org.springframework:spring-websocket")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")

compileOnly("org.springframework.security:spring-security-core")

testImplementation("io.projectreactor:reactor-core")
testImplementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
}
Expand Up @@ -22,10 +22,10 @@ import org.springframework.http.server.ServerHttpRequest
import org.springframework.http.server.ServerHttpResponse
import org.springframework.web.socket.WebSocketHandler
import org.springframework.web.socket.WebSocketHttpHeaders
import org.springframework.web.socket.server.HandshakeInterceptor
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor
import java.lang.Exception

class DgsHandshakeInterceptor : HandshakeInterceptor {
class DgsHandshakeInterceptor : HttpSessionHandshakeInterceptor() {
private val logger = LoggerFactory.getLogger(DgsHandshakeInterceptor::class.java)
override fun beforeHandshake(
request: ServerHttpRequest,
Expand All @@ -36,7 +36,7 @@ class DgsHandshakeInterceptor : HandshakeInterceptor {
if (request.headers[WebSocketHttpHeaders.SEC_WEBSOCKET_PROTOCOL].isNullOrEmpty()) {
request.headers.set(WebSocketHttpHeaders.SEC_WEBSOCKET_PROTOCOL, GRAPHQL_SUBSCRIPTIONS_WS_PROTOCOL)
}
return true
return super.beforeHandshake(request, response, wsHandler, attributes)
}

override fun afterHandshake(
Expand Down
Expand Up @@ -24,6 +24,9 @@ import org.reactivestreams.Publisher
import org.reactivestreams.Subscriber
import org.reactivestreams.Subscription
import org.slf4j.LoggerFactory
import org.springframework.security.core.context.SecurityContext
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.util.ClassUtils
import org.springframework.web.socket.SubProtocolCapable
import org.springframework.web.socket.TextMessage
import org.springframework.web.socket.WebSocketSession
Expand Down Expand Up @@ -52,6 +55,7 @@ class DgsWebSocketHandler(private val dgsQueryExecutor: DgsQueryExecutor) : Text

public override fun handleTextMessage(session: WebSocketSession, message: TextMessage) {
val (type, payload, id) = objectMapper.readValue(message.payload, OperationMessage::class.java)
loadSecurityContextFromSession(session)
when (type) {
GQL_CONNECTION_INIT -> {
logger.info("Initialized connection for {}", session.id)
Expand Down Expand Up @@ -84,6 +88,15 @@ class DgsWebSocketHandler(private val dgsQueryExecutor: DgsQueryExecutor) : Text
}
}

private fun loadSecurityContextFromSession(session: WebSocketSession) {
if (springSecurityAvailable) {
val securityContext = session.attributes["SPRING_SECURITY_CONTEXT"] as? SecurityContext
if (securityContext != null) {
SecurityContextHolder.setContext(securityContext)
}
}
}

private fun cleanupSubscriptionsForSession(session: WebSocketSession) {
logger.info("Cleaning up for session {}", session.id)
subscriptions[session.id]?.values?.forEach { it.cancel() }
Expand Down Expand Up @@ -143,6 +156,13 @@ class DgsWebSocketHandler(private val dgsQueryExecutor: DgsQueryExecutor) : Text
private companion object {
val logger = LoggerFactory.getLogger(DgsWebSocketHandler::class.java)
val objectMapper = jacksonObjectMapper()

private val springSecurityAvailable: Boolean by lazy {
ClassUtils.isPresent(
"org.springframework.security.core.context.SecurityContextHolder",
DgsWebSocketHandler::class.java.classLoader
)
}
}

override fun getSubProtocols(): List<String> = listOf(GRAPHQL_SUBSCRIPTIONS_WS_PROTOCOL)
Expand Down