Skip to content

Commit 40f6abe

Browse files
committed
Add coroutine support to pre/post authorize
Closes gh-8143
1 parent 3641756 commit 40f6abe

File tree

9 files changed

+524
-12
lines changed

9 files changed

+524
-12
lines changed

config/spring-security-config.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ dependencies {
8585
testImplementation ('org.springframework.data:spring-data-jpa') {
8686
exclude group: 'org.aspectj', module: 'aspectjrt'
8787
}
88+
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core'
89+
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-reactor'
8890

8991
testRuntimeOnly 'org.hsqldb:hsqldb'
9092
}

config/src/test/java/org/springframework/security/config/annotation/method/configuration/EnableReactiveMethodSecurityTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -77,7 +77,8 @@ public void notPublisherPreAuthorizeFindByIdThenThrowsIllegalStateException() {
7777
.withMessage("The returnType class java.lang.String on public abstract java.lang.String "
7878
+ "org.springframework.security.config.annotation.method.configuration.ReactiveMessageService"
7979
+ ".notPublisherPreAuthorizeFindById(long) must return an instance of org.reactivestreams"
80-
+ ".Publisher (i.e. Mono / Flux) in order to support Reactor Context");
80+
+ ".Publisher (i.e. Mono / Flux) or the function must be a Kotlin coroutine "
81+
+ "function in order to support Reactor Context");
8182
}
8283

8384
@Test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.config.annotation.method.configuration
18+
19+
import kotlinx.coroutines.flow.collect
20+
import kotlinx.coroutines.flow.toList
21+
import kotlinx.coroutines.runBlocking
22+
import org.assertj.core.api.Assertions.assertThat
23+
import org.assertj.core.api.Assertions.assertThatExceptionOfType
24+
import org.junit.Test
25+
import org.junit.runner.RunWith
26+
import org.springframework.beans.factory.annotation.Autowired
27+
import org.springframework.context.annotation.Bean
28+
import org.springframework.context.annotation.Configuration
29+
import org.springframework.security.access.AccessDeniedException
30+
import org.springframework.security.test.context.support.WithMockUser
31+
import org.springframework.test.context.ContextConfiguration
32+
import org.springframework.test.context.junit4.SpringRunner
33+
34+
@RunWith(SpringRunner::class)
35+
@ContextConfiguration
36+
class KotlinEnableReactiveMethodSecurityTests {
37+
38+
@Autowired
39+
var messageService: KotlinReactiveMessageService? = null
40+
41+
@Test
42+
fun suspendingGetResultWhenPermitAllThenSuccess() {
43+
runBlocking {
44+
assertThat(messageService!!.suspendingNoAuth()).isEqualTo("success")
45+
}
46+
}
47+
48+
@Test
49+
@WithMockUser(authorities = ["ROLE_ADMIN"])
50+
fun suspendingPreAuthorizeHasRoleWhenGrantedThenSuccess() {
51+
runBlocking {
52+
assertThat(messageService!!.suspendingPreAuthorizeHasRole()).isEqualTo("admin")
53+
}
54+
}
55+
56+
@Test
57+
fun suspendingPreAuthorizeHasRoleWhenNoAuthenticationThenDenied() {
58+
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy {
59+
runBlocking {
60+
messageService!!.suspendingPreAuthorizeHasRole()
61+
}
62+
}
63+
}
64+
65+
@Test
66+
@WithMockUser
67+
fun suspendingPreAuthorizeBeanWhenGrantedThenSuccess() {
68+
runBlocking {
69+
assertThat(messageService!!.suspendingPreAuthorizeBean(true)).isEqualTo("check")
70+
}
71+
}
72+
73+
@Test
74+
fun suspendingPreAuthorizeBeanWhenNotAuthorizedThenDenied() {
75+
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy {
76+
runBlocking {
77+
messageService!!.suspendingPreAuthorizeBean(false)
78+
}
79+
}
80+
}
81+
82+
@Test
83+
@WithMockUser("user")
84+
fun suspendingPostAuthorizeWhenAuthorizedThenSuccess() {
85+
runBlocking {
86+
assertThat(messageService!!.suspendingPostAuthorizeContainsName()).isEqualTo("user")
87+
}
88+
}
89+
90+
@Test
91+
@WithMockUser("other-user")
92+
fun suspendingPostAuthorizeWhenNotAuthorizedThenDenied() {
93+
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy {
94+
runBlocking {
95+
messageService!!.suspendingPostAuthorizeContainsName()
96+
}
97+
}
98+
}
99+
100+
@Test
101+
@WithMockUser(authorities = ["ROLE_ADMIN"])
102+
fun suspendingFlowPreAuthorizeHasRoleWhenGrantedThenSuccess() {
103+
runBlocking {
104+
assertThat(messageService!!.suspendingFlowPreAuthorize().toList()).containsExactly(1, 2, 3)
105+
}
106+
}
107+
108+
@Test
109+
fun suspendingFlowPreAuthorizeHasRoleWhenNoAuthenticationThenDenied() {
110+
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy {
111+
runBlocking {
112+
messageService!!.suspendingFlowPreAuthorize().collect()
113+
}
114+
}
115+
}
116+
117+
@Test
118+
fun suspendingFlowPostAuthorizeWhenAuthorizedThenSuccess() {
119+
runBlocking {
120+
assertThat(messageService!!.suspendingFlowPostAuthorize(true).toList()).containsExactly(1, 2, 3)
121+
}
122+
}
123+
124+
@Test
125+
fun suspendingFlowPostAuthorizeWhenNotAuthorizedThenDenied() {
126+
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy {
127+
runBlocking {
128+
messageService!!.suspendingFlowPostAuthorize(false).collect()
129+
}
130+
}
131+
}
132+
133+
@Test
134+
@WithMockUser(authorities = ["ROLE_ADMIN"])
135+
fun flowPreAuthorizeHasRoleWhenGrantedThenSuccess() {
136+
runBlocking {
137+
assertThat(messageService!!.flowPreAuthorize().toList()).containsExactly(1, 2, 3)
138+
}
139+
}
140+
141+
@Test
142+
fun flowPreAuthorizeHasRoleWhenNoAuthenticationThenDenied() {
143+
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy {
144+
runBlocking {
145+
messageService!!.flowPreAuthorize().collect()
146+
}
147+
}
148+
}
149+
150+
@Test
151+
fun flowPostAuthorizeWhenAuthorizedThenSuccess() {
152+
runBlocking {
153+
assertThat(messageService!!.flowPostAuthorize(true).toList()).containsExactly(1, 2, 3)
154+
}
155+
}
156+
157+
@Test
158+
fun flowPostAuthorizeWhenNotAuthorizedThenDenied() {
159+
assertThatExceptionOfType(AccessDeniedException::class.java).isThrownBy {
160+
runBlocking {
161+
messageService!!.flowPostAuthorize(false).collect()
162+
}
163+
}
164+
}
165+
166+
@EnableReactiveMethodSecurity
167+
@Configuration
168+
open class Config {
169+
170+
@Bean
171+
open fun messageService(): KotlinReactiveMessageServiceImpl {
172+
return KotlinReactiveMessageServiceImpl()
173+
}
174+
175+
@Bean
176+
open fun authz(): Authz {
177+
return Authz()
178+
}
179+
180+
open class Authz {
181+
fun check(r: Boolean): Boolean {
182+
return r
183+
}
184+
}
185+
}
186+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.config.annotation.method.configuration
18+
19+
import kotlinx.coroutines.flow.Flow
20+
21+
interface KotlinReactiveMessageService {
22+
23+
suspend fun suspendingNoAuth(): String
24+
25+
suspend fun suspendingPreAuthorizeHasRole(): String
26+
27+
suspend fun suspendingPreAuthorizeBean(id: Boolean): String
28+
29+
suspend fun suspendingPostAuthorizeContainsName(): String
30+
31+
suspend fun suspendingFlowPreAuthorize(): Flow<Int>
32+
33+
suspend fun suspendingFlowPostAuthorize(id: Boolean): Flow<Int>
34+
35+
fun flowPreAuthorize(): Flow<Int>
36+
37+
fun flowPostAuthorize(id: Boolean): Flow<Int>
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.config.annotation.method.configuration
18+
19+
import kotlinx.coroutines.delay
20+
import kotlinx.coroutines.flow.Flow
21+
import kotlinx.coroutines.flow.flow
22+
import org.springframework.security.access.prepost.PostAuthorize
23+
import org.springframework.security.access.prepost.PreAuthorize
24+
25+
class KotlinReactiveMessageServiceImpl : KotlinReactiveMessageService {
26+
27+
override suspend fun suspendingNoAuth(): String {
28+
delay(1)
29+
return "success"
30+
}
31+
32+
@PreAuthorize("hasRole('ADMIN')")
33+
override suspend fun suspendingPreAuthorizeHasRole(): String {
34+
delay(1)
35+
return "admin"
36+
}
37+
38+
@PreAuthorize("@authz.check(#id)")
39+
override suspend fun suspendingPreAuthorizeBean(id: Boolean): String {
40+
delay(1)
41+
return "check"
42+
}
43+
44+
@PostAuthorize("returnObject?.contains(authentication?.name)")
45+
override suspend fun suspendingPostAuthorizeContainsName(): String {
46+
delay(1)
47+
return "user"
48+
}
49+
50+
@PreAuthorize("hasRole('ADMIN')")
51+
override suspend fun suspendingFlowPreAuthorize(): Flow<Int> {
52+
delay(1)
53+
return flow {
54+
for (i in 1..3) {
55+
delay(1)
56+
emit(i)
57+
}
58+
}
59+
}
60+
61+
@PostAuthorize("@authz.check(#id)")
62+
override suspend fun suspendingFlowPostAuthorize(id: Boolean): Flow<Int> {
63+
delay(1)
64+
return flow {
65+
for (i in 1..3) {
66+
delay(1)
67+
emit(i)
68+
}
69+
}
70+
}
71+
72+
@PreAuthorize("hasRole('ADMIN')")
73+
override fun flowPreAuthorize(): Flow<Int> {
74+
return flow {
75+
for (i in 1..3) {
76+
delay(1)
77+
emit(i)
78+
}
79+
}
80+
}
81+
82+
@PostAuthorize("@authz.check(#id)")
83+
override fun flowPostAuthorize(id: Boolean): Flow<Int> {
84+
return flow {
85+
for (i in 1..3) {
86+
delay(1)
87+
emit(i)
88+
}
89+
}
90+
}
91+
}

core/spring-security-core.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dependencies {
2626
optional 'org.aspectj:aspectjrt'
2727
optional 'org.springframework:spring-jdbc'
2828
optional 'org.springframework:spring-tx'
29+
optional 'org.jetbrains.kotlinx:kotlinx-coroutines-reactor'
2930

3031
testImplementation powerMock2Dependencies
3132
testImplementation 'commons-collections:commons-collections'

0 commit comments

Comments
 (0)