Comments (3)
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.
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.
SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL)
from spring-kotlin-coroutine.
Related Issues (20)
- Handling Spring ThreadLocal use HOT 1
- Too early initialization warning
- Handling @Cacheable condition
- Getting NullPointerException Question HOT 2
- Bean post processing with null bean name HOT 1
- java.lang.ClassCastException when using @Cacheable with suspend function
- Could not resovle "ServerRequest" as a parameter type of a controller method HOT 1
- java.lang.ClassNotFoundException: kotlinx.coroutines.experimental.Dispatchers HOT 2
- Issues using with Kotlin Coroutine 0.30.1
- Bean validation does not work on a coroutine controller HOT 5
- Support for Kotlin 1.3.0 HOT 7
- Support for request scope
- Publish in Maven Central HOT 2
- Using spring-kotlin-coroutine with opentracing
- Webflux coroutine controller responds with "COROUTINE_SUSPENDED" string
- the Java method #CoroutineUtils.runCoroutine doesn't work
- fixedDelayString error - embeddedValueResolver null in DefaultSchedulingPolicyProvider
- Bintray sunset, can't download dependency HOT 8
- Any update of this project? HOT 1
- CoroutineCrudRepository throws PropertyReferenceException for count method HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spring-kotlin-coroutine.