Giter VIP home page Giter VIP logo

Comments (3)

userquin avatar userquin commented on June 15, 2024

ok, a first version could be (can be integrated in the project?):

const val SECURED_COMMON_POOL = "SecuredCommonPool"
const val SECURED_UNCONFINED = "SecuredUnconfined"

internal open class SpringSecurityCoroutineContext(
        private val dispatcher: ContinuationInterceptor
): AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor {
    private var securityContext: SecurityContext = SecurityContextHolder.getContext()
    override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> = dispatcher.interceptContinuation(Wrapper(continuation))
    inner class Wrapper<T>(private val continuation: Continuation<T>): Continuation<T> {
        private inline fun wrap(block: () -> Unit) {
            try {
                SecurityContextHolder.setContext(securityContext)
                block()
            } finally {
                securityContext = SecurityContextHolder.getContext()
                SecurityContextHolder.clearContext()
            }
        }

        override val context: CoroutineContext get() = continuation.context
        override fun resume(value: T) = wrap { continuation.resume(value) }
        override fun resumeWithException(exception: Throwable) = wrap { continuation.resumeWithException(exception) }
    }

}

internal open class SpringSecurityCoroutineContextResolver: CoroutineContextResolver {
    override fun resolveContext(beanName: String, bean: Any?): CoroutineContext? = when(beanName) {
        SECURED_COMMON_POOL -> SpringSecurityCoroutineContext(CommonPool)
        SECURED_UNCONFINED -> SpringSecurityCoroutineContext(Unconfined)
        else -> bean as? CoroutineContext
    }
}

@Configuration
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
open class CoroutineContextResolverConfiguration {
  ...
  ..
  @Bean
  @ConditionalOnClass("org.springframework.security.core.context.SecurityContext")
  open fun springSecurityCoroutineContextResolver(): CoroutineContextResolver = 
    SpringSecurityCoroutineContextResolver()
}

from spring-kotlin-coroutine.

userquin avatar userquin commented on June 15, 2024

Previous code will not work just because the context is cached by CoroutineMethodInterceptor, so all calls will use the same SpringSecurityCoroutineContext.

val contextKey = coroutine.context to coroutine.name
val context = contextMap[contextKey] ?: getContext(contextKey).apply { contextMap[contextKey] = this }

so the solution is to capture SecurityContext in the interceptor:

Just move private var securityContext: SecurityContext = SecurityContextHolder.getContext() from SpringSecurityCoroutineContext to Wrapper.

const val SECURED_COMMON_POOL = "SecuredCommonPool"
const val SECURED_UNCONFINED = "SecuredUnconfined"

internal open class SpringSecurityCoroutineContext(
        private val dispatcher: ContinuationInterceptor
): AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor {
    override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> = dispatcher.interceptContinuation(Wrapper(continuation))
    inner class Wrapper<T>(private val continuation: Continuation<T>): Continuation<T> {
        private var securityContext: SecurityContext = SecurityContextHolder.getContext()
        private inline fun wrap(block: () -> Unit) {
            try {
                SecurityContextHolder.setContext(securityContext)
                block()
            } finally {
                securityContext = SecurityContextHolder.getContext()
                SecurityContextHolder.clearContext()
            }
        }

        override val context: CoroutineContext get() = continuation.context
        override fun resume(value: T) = wrap { continuation.resume(value) }
        override fun resumeWithException(exception: Throwable) = wrap { continuation.resumeWithException(exception) }
    }

}

internal open class SpringSecurityCoroutineContextResolver: CoroutineContextResolver {
    override fun resolveContext(beanName: String, bean: Any?): CoroutineContext? = when(beanName) {
        SECURED_COMMON_POOL -> SpringSecurityCoroutineContext(CommonPool)
        SECURED_UNCONFINED -> SpringSecurityCoroutineContext(Unconfined)
        else -> bean as? CoroutineContext
    }
}

@Configuration
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
open class CoroutineContextResolverConfiguration {
  ...
  ..
  @Bean
  @ConditionalOnClass("org.springframework.security.core.context.SecurityContext")
  open fun springSecurityCoroutineContextResolver(): CoroutineContextResolver = 
    SpringSecurityCoroutineContextResolver()
}

from spring-kotlin-coroutine.

dndll avatar dndll commented on June 15, 2024

SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL)

from spring-kotlin-coroutine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.