Add Coroutine Support

Closes gh-12080
This commit is contained in:
Josh Cummings
2023-11-15 11:48:37 -07:00
parent cad6689659
commit 97516727a4
7 changed files with 214 additions and 24 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -80,7 +80,7 @@ public class EnableAuthorizationManagerReactiveMethodSecurityTests {
.withMessage("The returnType class java.lang.String on public abstract java.lang.String "
+ "org.springframework.security.config.annotation.method.configuration.ReactiveMessageService"
+ ".notPublisherPreAuthorizeFindById(long) must return an instance of org.reactivestreams"
+ ".Publisher (for example, a Mono or Flux) in order to support Reactor Context");
+ ".Publisher (for example, a Mono or Flux) or the function must be a Kotlin coroutine in order to support Reactor Context");
}
@Test

View File

@@ -78,7 +78,7 @@ public class EnableReactiveMethodSecurityTests {
.withMessage("The returnType class java.lang.String on public abstract java.lang.String "
+ "org.springframework.security.config.annotation.method.configuration.ReactiveMessageService"
+ ".notPublisherPreAuthorizeFindById(long) must return an instance of org.reactivestreams"
+ ".Publisher (for example, a Mono or Flux) in order to support Reactor Context");
+ ".Publisher (for example, a Mono or Flux) or the function must be a Kotlin coroutine in order to support Reactor Context");
}
@Test

View File

@@ -41,8 +41,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension
@ExtendWith(SpringExtension::class)
@ContextConfiguration
// no authorization manager due to https://github.com/spring-projects/spring-security/issues/12080
class KotlinEnableReactiveMethodSecurityNoAuthorizationManagerTests {
class KotlinEnableReactiveMethodSecurityTests {
private lateinit var delegate: KotlinReactiveMessageService
@@ -138,6 +137,39 @@ class KotlinEnableReactiveMethodSecurityNoAuthorizationManagerTests {
coVerify(exactly = 1) { delegate.suspendingPreAuthorizeHasRole() }
}
@Test
@WithMockUser
fun `suspendingPrePostAuthorizeHasRoleContainsName when not pre authorized then delegate not called`() {
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy {
runBlocking {
messageService!!.suspendingPrePostAuthorizeHasRoleContainsName()
}
}
verify { delegate wasNot Called }
}
@Test
@WithMockUser(authorities = ["ROLE_ADMIN"])
fun `suspendingPrePostAuthorizeHasRoleContainsName when not post authorized then exception`() {
coEvery { delegate.suspendingPrePostAuthorizeHasRoleContainsName() } returns "wrong"
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy {
runBlocking {
messageService!!.suspendingPrePostAuthorizeHasRoleContainsName()
}
}
coVerify(exactly = 1) { delegate.suspendingPrePostAuthorizeHasRoleContainsName() }
}
@Test
@WithMockUser(authorities = ["ROLE_ADMIN"])
fun `suspendingPrePostAuthorizeHasRoleContainsName when authorized then success`() {
coEvery { delegate.suspendingPrePostAuthorizeHasRoleContainsName() } returns "user"
runBlocking {
assertThat(messageService!!.suspendingPrePostAuthorizeHasRoleContainsName()).contains("user")
}
coVerify(exactly = 1) { delegate.suspendingPrePostAuthorizeHasRoleContainsName() }
}
@Test
@WithMockUser(authorities = ["ROLE_ADMIN"])
fun `suspendingFlowPreAuthorize when user has role then success`() {
@@ -181,6 +213,33 @@ class KotlinEnableReactiveMethodSecurityNoAuthorizationManagerTests {
verify { delegate wasNot Called }
}
@Test
fun `suspendingFlowPrePostAuthorizeBean when not pre authorized then delegate not called`() {
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy {
runBlocking {
messageService!!.suspendingFlowPrePostAuthorizeBean(true).collect()
}
}
}
@Test
@WithMockUser(roles = ["ADMIN"])
fun `suspendingFlowPrePostAuthorizeBean when not post authorized then denied`() {
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy {
runBlocking {
messageService!!.suspendingFlowPrePostAuthorizeBean(false).collect()
}
}
}
@Test
@WithMockUser(roles = ["ADMIN"])
fun `suspendingFlowPrePostAuthorizeBean when authorized then success`() {
runBlocking {
assertThat(messageService!!.suspendingFlowPrePostAuthorizeBean(true).toList()).containsExactly(1, 2, 3)
}
}
@Test
@WithMockUser(authorities = ["ROLE_ADMIN"])
fun `suspendingFlowPreAuthorizeDelegate when user has role then delegate called`() {
@@ -244,8 +303,35 @@ class KotlinEnableReactiveMethodSecurityNoAuthorizationManagerTests {
coVerify(exactly = 1) { delegate.flowPreAuthorize() }
}
@Test
fun `flowPrePostAuthorize when not pre authorized then denied`() {
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy {
runBlocking {
messageService!!.flowPrePostAuthorize(true).collect()
}
}
}
@Test
@WithMockUser(roles = ["ADMIN"])
fun `flowPrePostAuthorize when not post authorized then denied`() {
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy {
runBlocking {
messageService!!.flowPrePostAuthorize(false).collect()
}
}
}
@Test
@WithMockUser(roles = ["ADMIN"])
fun `flowPrePostAuthorize when authorized then success`() {
runBlocking {
assertThat(messageService!!.flowPrePostAuthorize(true).toList()).containsExactly(1, 2, 3)
}
}
@Configuration
@EnableReactiveMethodSecurity(useAuthorizationManager = false)
@EnableReactiveMethodSecurity
open class Config {
var delegate = mockk<KotlinReactiveMessageService>()

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,15 +30,21 @@ interface KotlinReactiveMessageService {
suspend fun suspendingPreAuthorizeDelegate(): String
suspend fun suspendingPrePostAuthorizeHasRoleContainsName(): String
suspend fun suspendingFlowPreAuthorize(): Flow<Int>
suspend fun suspendingFlowPostAuthorize(id: Boolean): Flow<Int>
suspend fun suspendingFlowPreAuthorizeDelegate(): Flow<Int>
suspend fun suspendingFlowPrePostAuthorizeBean(id: Boolean): Flow<Int>
fun flowPreAuthorize(): Flow<Int>
fun flowPostAuthorize(id: Boolean): Flow<Int>
fun flowPreAuthorizeDelegate(): Flow<Int>
fun flowPrePostAuthorize(id: Boolean): Flow<Int>
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -47,6 +47,12 @@ class KotlinReactiveMessageServiceImpl(val delegate: KotlinReactiveMessageServic
return "user"
}
@PreAuthorize("hasRole('ADMIN')")
@PostAuthorize("returnObject?.contains(authentication?.name)")
override suspend fun suspendingPrePostAuthorizeHasRoleContainsName(): String {
return delegate.suspendingPrePostAuthorizeHasRoleContainsName()
}
@PreAuthorize("hasRole('ADMIN')")
override suspend fun suspendingPreAuthorizeDelegate(): String {
return delegate.suspendingPreAuthorizeHasRole()
@@ -80,6 +86,18 @@ class KotlinReactiveMessageServiceImpl(val delegate: KotlinReactiveMessageServic
return delegate.flowPreAuthorize()
}
@PreAuthorize("hasRole('ADMIN')")
@PostAuthorize("@authz.check(#id)")
override suspend fun suspendingFlowPrePostAuthorizeBean(id: Boolean): Flow<Int> {
delay(1)
return flow {
for (i in 1..3) {
delay(1)
emit(i)
}
}
}
@PreAuthorize("hasRole('ADMIN')")
override fun flowPreAuthorize(): Flow<Int> {
return flow {
@@ -104,4 +122,15 @@ class KotlinReactiveMessageServiceImpl(val delegate: KotlinReactiveMessageServic
override fun flowPreAuthorizeDelegate(): Flow<Int> {
return delegate.flowPreAuthorize()
}
@PreAuthorize("hasRole('ADMIN')")
@PostAuthorize("@authz.check(#id)")
override fun flowPrePostAuthorize(id: Boolean): Flow<Int> {
return flow {
for (i in 1..3) {
delay(1)
emit(i)
}
}
}
}