16
16
17
17
package org.springframework.security.config.web.servlet
18
18
19
+ import org.assertj.core.api.Assertions
19
20
import org.junit.Rule
20
21
import org.junit.Test
21
22
import org.mockito.Mockito.*
23
+ import org.springframework.beans.factory.BeanCreationException
22
24
import org.springframework.beans.factory.annotation.Autowired
23
25
import org.springframework.context.annotation.Bean
26
+ import org.springframework.security.authentication.AuthenticationManager
27
+ import org.springframework.security.authentication.AuthenticationManagerResolver
24
28
import org.springframework.security.config.annotation.web.builders.HttpSecurity
25
29
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
26
30
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
27
31
import org.springframework.security.config.test.SpringTestRule
28
32
import org.springframework.security.oauth2.core.oidc.IdTokenClaimNames.SUB
29
33
import org.springframework.security.oauth2.jwt.Jwt
30
34
import org.springframework.security.oauth2.jwt.JwtDecoder
35
+ import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken
31
36
import org.springframework.security.oauth2.server.resource.web.BearerTokenResolver
32
37
import org.springframework.security.web.AuthenticationEntryPoint
33
38
import org.springframework.security.web.access.AccessDeniedHandler
34
39
import org.springframework.test.web.servlet.MockMvc
35
40
import org.springframework.test.web.servlet.get
41
+ import javax.servlet.http.HttpServletRequest
36
42
37
43
/* *
38
44
* Tests for [OAuth2ResourceServerDsl]
@@ -47,6 +53,11 @@ class OAuth2ResourceServerDslTests {
47
53
@Autowired
48
54
lateinit var mockMvc: MockMvc
49
55
56
+ private val JWT : Jwt = Jwt .withTokenValue(" token" )
57
+ .header(" alg" , " none" )
58
+ .claim(SUB , " user" )
59
+ .build()
60
+
50
61
@Test
51
62
fun `oauth2Resource server when custom entry point then entry point used` () {
52
63
this .spring.register(EntryPointConfig ::class .java).autowire()
@@ -116,11 +127,7 @@ class OAuth2ResourceServerDslTests {
116
127
@Test
117
128
fun `oauth2Resource server when custom access denied handler then handler used` () {
118
129
this .spring.register(AccessDeniedHandlerConfig ::class .java).autowire()
119
- `when `(AccessDeniedHandlerConfig .DECODER .decode(anyString())).thenReturn(
120
- Jwt .withTokenValue(" token" )
121
- .header(" alg" , " none" )
122
- .claim(SUB , " user" )
123
- .build())
130
+ `when `(AccessDeniedHandlerConfig .DECODER .decode(anyString())).thenReturn(JWT )
124
131
125
132
this .mockMvc.get(" /" ) {
126
133
header(" Authorization" , " Bearer token" )
@@ -153,4 +160,61 @@ class OAuth2ResourceServerDslTests {
153
160
return DECODER
154
161
}
155
162
}
163
+
164
+ @Test
165
+ fun `oauth2Resource server when custom authentication manager resolver then resolver used` () {
166
+ this .spring.register(AuthenticationManagerResolverConfig ::class .java).autowire()
167
+ `when `(AuthenticationManagerResolverConfig .RESOLVER .resolve(any())).thenReturn(
168
+ AuthenticationManager {
169
+ JwtAuthenticationToken (JWT )
170
+ }
171
+ )
172
+
173
+ this .mockMvc.get(" /" ) {
174
+ header(" Authorization" , " Bearer token" )
175
+ }
176
+
177
+ verify(AuthenticationManagerResolverConfig .RESOLVER ).resolve(any())
178
+ }
179
+
180
+ @EnableWebSecurity
181
+ open class AuthenticationManagerResolverConfig : WebSecurityConfigurerAdapter () {
182
+ companion object {
183
+ var RESOLVER : AuthenticationManagerResolver <* > = mock(AuthenticationManagerResolver ::class .java)
184
+ }
185
+
186
+ override fun configure (http : HttpSecurity ) {
187
+ http {
188
+ authorizeRequests {
189
+ authorize(anyRequest, authenticated)
190
+ }
191
+ oauth2ResourceServer {
192
+ authenticationManagerResolver = RESOLVER as AuthenticationManagerResolver <HttpServletRequest >
193
+ }
194
+ }
195
+ }
196
+ }
197
+
198
+ @Test
199
+ fun `oauth2Resource server when custom authentication manager resolver and opaque then exception` () {
200
+ Assertions .assertThatExceptionOfType(BeanCreationException ::class .java)
201
+ .isThrownBy { spring.register(AuthenticationManagerResolverAndOpaqueConfig ::class .java).autowire() }
202
+ .withMessageContaining(" authenticationManagerResolver" )
203
+ }
204
+
205
+ @EnableWebSecurity
206
+ open class AuthenticationManagerResolverAndOpaqueConfig : WebSecurityConfigurerAdapter () {
207
+ override fun configure (http : HttpSecurity ) {
208
+ http {
209
+ authorizeRequests {
210
+ authorize(anyRequest, authenticated)
211
+ }
212
+ oauth2ResourceServer {
213
+ authenticationManagerResolver = mock(AuthenticationManagerResolver ::class .java)
214
+ as AuthenticationManagerResolver <HttpServletRequest >
215
+ opaqueToken { }
216
+ }
217
+ }
218
+ }
219
+ }
156
220
}
0 commit comments