16
16
17
17
package org.springframework.security.config.annotation.web
18
18
19
+ import org.springframework.context.ApplicationContext
19
20
import org.springframework.http.HttpMethod
20
21
import org.springframework.security.authorization.AuthenticatedAuthorizationManager
21
22
import org.springframework.security.authorization.AuthorityAuthorizationManager
22
23
import org.springframework.security.authorization.AuthorizationDecision
23
24
import org.springframework.security.authorization.AuthorizationManager
24
25
import org.springframework.security.config.annotation.web.builders.HttpSecurity
25
26
import org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer
27
+ import org.springframework.security.config.core.GrantedAuthorityDefaults
26
28
import org.springframework.security.core.Authentication
29
+ import org.springframework.security.web.access.IpAddressAuthorizationManager
27
30
import org.springframework.security.web.access.intercept.AuthorizationFilter
28
31
import org.springframework.security.web.access.intercept.RequestAuthorizationContext
29
- import org.springframework.security.web.access.IpAddressAuthorizationManager
30
32
import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher
31
33
import org.springframework.security.web.util.matcher.AnyRequestMatcher
32
34
import org.springframework.security.web.util.matcher.RequestMatcher
@@ -41,7 +43,7 @@ import java.util.function.Supplier
41
43
* @since 5.7
42
44
* @property shouldFilterAllDispatcherTypes whether the [AuthorizationFilter] should filter all dispatcher types
43
45
*/
44
- class AuthorizeHttpRequestsDsl : AbstractRequestMatcherDsl () {
46
+ class AuthorizeHttpRequestsDsl : AbstractRequestMatcherDsl {
45
47
@Deprecated("""
46
48
Add authorization rules to DispatcherType directly.
47
49
@@ -62,6 +64,7 @@ class AuthorizeHttpRequestsDsl : AbstractRequestMatcherDsl() {
62
64
var shouldFilterAllDispatcherTypes: Boolean? = null
63
65
64
66
private val authorizationRules = mutableListOf<AuthorizationManagerRule >()
67
+ private val rolePrefix: String
65
68
66
69
private val HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME = " mvcHandlerMappingIntrospector"
67
70
private val HANDLER_MAPPING_INTROSPECTOR = " org.springframework.web.servlet.handler.HandlerMappingIntrospector"
@@ -227,7 +230,7 @@ class AuthorizeHttpRequestsDsl : AbstractRequestMatcherDsl() {
227
230
* @return the [AuthorizationManager] with the provided role
228
231
*/
229
232
fun hasRole (role : String ): AuthorizationManager <RequestAuthorizationContext > {
230
- return AuthorityAuthorizationManager .hasRole( role)
233
+ return AuthorityAuthorizationManager .hasAnyRole( this .rolePrefix, arrayOf( role) )
231
234
}
232
235
233
236
/* *
@@ -237,7 +240,7 @@ class AuthorizeHttpRequestsDsl : AbstractRequestMatcherDsl() {
237
240
* @return the [AuthorizationManager] with the provided roles
238
241
*/
239
242
fun hasAnyRole (vararg roles : String ): AuthorizationManager <RequestAuthorizationContext > {
240
- return AuthorityAuthorizationManager .hasAnyRole(* roles)
243
+ return AuthorityAuthorizationManager .hasAnyRole(this .rolePrefix, arrayOf( * roles) )
241
244
}
242
245
243
246
/* *
@@ -290,4 +293,18 @@ class AuthorizeHttpRequestsDsl : AbstractRequestMatcherDsl() {
290
293
}
291
294
}
292
295
}
296
+
297
+ constructor () {
298
+ this .rolePrefix = " ROLE_"
299
+ }
300
+
301
+ constructor (context: ApplicationContext ) {
302
+ val beanNames = context.getBeanNamesForType(GrantedAuthorityDefaults ::class .java)
303
+ if (beanNames.size > 0 ) {
304
+ val grantedAuthorityDefaults = context.getBean(GrantedAuthorityDefaults ::class .java);
305
+ this .rolePrefix = grantedAuthorityDefaults.rolePrefix
306
+ } else {
307
+ this .rolePrefix = " ROLE_"
308
+ }
309
+ }
293
310
}
0 commit comments