|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 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.web.servlet |
| 18 | + |
| 19 | +import org.springframework.http.HttpMethod |
| 20 | +import org.springframework.security.authorization.AuthenticatedAuthorizationManager |
| 21 | +import org.springframework.security.authorization.AuthorityAuthorizationManager |
| 22 | +import org.springframework.security.authorization.AuthorizationDecision |
| 23 | +import org.springframework.security.authorization.AuthorizationManager |
| 24 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity |
| 25 | +import org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer |
| 26 | +import org.springframework.security.core.Authentication |
| 27 | +import org.springframework.security.web.access.intercept.RequestAuthorizationContext |
| 28 | +import org.springframework.security.web.util.matcher.AnyRequestMatcher |
| 29 | +import org.springframework.security.web.util.matcher.RequestMatcher |
| 30 | +import org.springframework.util.ClassUtils |
| 31 | +import java.util.function.Supplier |
| 32 | + |
| 33 | +/** |
| 34 | + * A Kotlin DSL to configure [HttpSecurity] request authorization using idiomatic Kotlin code. |
| 35 | + * |
| 36 | + * @author Yuriy Savchenko |
| 37 | + * @since 5.7 |
| 38 | + */ |
| 39 | +class AuthorizeHttpRequestsDsl : AbstractRequestMatcherDsl() { |
| 40 | + private val authorizationRules = mutableListOf<AuthorizationManagerRule>() |
| 41 | + |
| 42 | + private val HANDLER_MAPPING_INTROSPECTOR = "org.springframework.web.servlet.handler.HandlerMappingIntrospector" |
| 43 | + private val MVC_PRESENT = ClassUtils.isPresent( |
| 44 | + HANDLER_MAPPING_INTROSPECTOR, |
| 45 | + AuthorizeHttpRequestsDsl::class.java.classLoader) |
| 46 | + private val PATTERN_TYPE = if (MVC_PRESENT) PatternType.MVC else PatternType.ANT |
| 47 | + |
| 48 | + /** |
| 49 | + * Adds a request authorization rule. |
| 50 | + * |
| 51 | + * @param matches the [RequestMatcher] to match incoming requests against |
| 52 | + * @param access the [AuthorizationManager] to secure the matching request |
| 53 | + * (i.e. created via hasAuthority("ROLE_USER")) |
| 54 | + */ |
| 55 | + fun authorize(matches: RequestMatcher = AnyRequestMatcher.INSTANCE, |
| 56 | + access: AuthorizationManager<RequestAuthorizationContext>) { |
| 57 | + authorizationRules.add(MatcherAuthorizationManagerRule(matches, access)) |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Adds a request authorization rule for an endpoint matching the provided |
| 62 | + * pattern. |
| 63 | + * If Spring MVC is on the classpath, it will use an MVC matcher. |
| 64 | + * If Spring MVC is not on the classpath, it will use an ant matcher. |
| 65 | + * The MVC will use the same rules that Spring MVC uses for matching. |
| 66 | + * For example, often times a mapping of the path "/path" will match on |
| 67 | + * "/path", "/path/", "/path.html", etc. |
| 68 | + * If the current request will not be processed by Spring MVC, a reasonable default |
| 69 | + * using the pattern as an ant pattern will be used. |
| 70 | + * |
| 71 | + * @param pattern the pattern to match incoming requests against. |
| 72 | + * @param access the [AuthorizationManager] to secure the matching request |
| 73 | + * (i.e. created via hasAuthority("ROLE_USER")) |
| 74 | + */ |
| 75 | + fun authorize(pattern: String, |
| 76 | + access: AuthorizationManager<RequestAuthorizationContext>) { |
| 77 | + authorizationRules.add( |
| 78 | + PatternAuthorizationManagerRule( |
| 79 | + pattern = pattern, |
| 80 | + patternType = PATTERN_TYPE, |
| 81 | + rule = access |
| 82 | + ) |
| 83 | + ) |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Adds a request authorization rule for an endpoint matching the provided |
| 88 | + * pattern. |
| 89 | + * If Spring MVC is on the classpath, it will use an MVC matcher. |
| 90 | + * If Spring MVC is not on the classpath, it will use an ant matcher. |
| 91 | + * The MVC will use the same rules that Spring MVC uses for matching. |
| 92 | + * For example, often times a mapping of the path "/path" will match on |
| 93 | + * "/path", "/path/", "/path.html", etc. |
| 94 | + * If the current request will not be processed by Spring MVC, a reasonable default |
| 95 | + * using the pattern as an ant pattern will be used. |
| 96 | + * |
| 97 | + * @param method the HTTP method to match the income requests against. |
| 98 | + * @param pattern the pattern to match incoming requests against. |
| 99 | + * @param access the [AuthorizationManager] to secure the matching request |
| 100 | + * (i.e. created via hasAuthority("ROLE_USER")) |
| 101 | + */ |
| 102 | + fun authorize(method: HttpMethod, |
| 103 | + pattern: String, |
| 104 | + access: AuthorizationManager<RequestAuthorizationContext>) { |
| 105 | + authorizationRules.add( |
| 106 | + PatternAuthorizationManagerRule( |
| 107 | + pattern = pattern, |
| 108 | + patternType = PATTERN_TYPE, |
| 109 | + httpMethod = method, |
| 110 | + rule = access |
| 111 | + ) |
| 112 | + ) |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Adds a request authorization rule for an endpoint matching the provided |
| 117 | + * pattern. |
| 118 | + * If Spring MVC is on the classpath, it will use an MVC matcher. |
| 119 | + * If Spring MVC is not on the classpath, it will use an ant matcher. |
| 120 | + * The MVC will use the same rules that Spring MVC uses for matching. |
| 121 | + * For example, often times a mapping of the path "/path" will match on |
| 122 | + * "/path", "/path/", "/path.html", etc. |
| 123 | + * If the current request will not be processed by Spring MVC, a reasonable default |
| 124 | + * using the pattern as an ant pattern will be used. |
| 125 | + * |
| 126 | + * @param pattern the pattern to match incoming requests against. |
| 127 | + * @param servletPath the servlet path to match incoming requests against. This |
| 128 | + * only applies when using an MVC pattern matcher. |
| 129 | + * @param access the [AuthorizationManager] to secure the matching request |
| 130 | + * (i.e. created via hasAuthority("ROLE_USER")) |
| 131 | + */ |
| 132 | + fun authorize(pattern: String, |
| 133 | + servletPath: String, |
| 134 | + access: AuthorizationManager<RequestAuthorizationContext>) { |
| 135 | + authorizationRules.add( |
| 136 | + PatternAuthorizationManagerRule( |
| 137 | + pattern = pattern, |
| 138 | + patternType = PATTERN_TYPE, |
| 139 | + servletPath = servletPath, |
| 140 | + rule = access |
| 141 | + ) |
| 142 | + ) |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Adds a request authorization rule for an endpoint matching the provided |
| 147 | + * pattern. |
| 148 | + * If Spring MVC is on the classpath, it will use an MVC matcher. |
| 149 | + * If Spring MVC is not on the classpath, it will use an ant matcher. |
| 150 | + * The MVC will use the same rules that Spring MVC uses for matching. |
| 151 | + * For example, often times a mapping of the path "/path" will match on |
| 152 | + * "/path", "/path/", "/path.html", etc. |
| 153 | + * If the current request will not be processed by Spring MVC, a reasonable default |
| 154 | + * using the pattern as an ant pattern will be used. |
| 155 | + * |
| 156 | + * @param method the HTTP method to match the income requests against. |
| 157 | + * @param pattern the pattern to match incoming requests against. |
| 158 | + * @param servletPath the servlet path to match incoming requests against. This |
| 159 | + * only applies when using an MVC pattern matcher. |
| 160 | + * @param access the [AuthorizationManager] to secure the matching request |
| 161 | + * (i.e. created via hasAuthority("ROLE_USER")) |
| 162 | + */ |
| 163 | + fun authorize(method: HttpMethod, |
| 164 | + pattern: String, |
| 165 | + servletPath: String, |
| 166 | + access: AuthorizationManager<RequestAuthorizationContext>) { |
| 167 | + authorizationRules.add( |
| 168 | + PatternAuthorizationManagerRule( |
| 169 | + pattern = pattern, |
| 170 | + patternType = PATTERN_TYPE, |
| 171 | + servletPath = servletPath, |
| 172 | + httpMethod = method, |
| 173 | + rule = access |
| 174 | + ) |
| 175 | + ) |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Specify that URLs require a particular authority. |
| 180 | + * |
| 181 | + * @param authority the authority to require (i.e. ROLE_USER, ROLE_ADMIN, etc). |
| 182 | + * @return the [AuthorizationManager] with the provided authority |
| 183 | + */ |
| 184 | + fun hasAuthority(authority: String): AuthorizationManager<RequestAuthorizationContext> { |
| 185 | + return AuthorityAuthorizationManager.hasAuthority(authority) |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Specify that URLs require any of the provided authorities. |
| 190 | + * |
| 191 | + * @param authorities the authorities to require (i.e. ROLE_USER, ROLE_ADMIN, etc). |
| 192 | + * @return the [AuthorizationManager] with the provided authorities |
| 193 | + */ |
| 194 | + fun hasAnyAuthority(vararg authorities: String): AuthorizationManager<RequestAuthorizationContext> { |
| 195 | + return AuthorityAuthorizationManager.hasAnyAuthority(*authorities) |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Specify that URLs require a particular role. |
| 200 | + * |
| 201 | + * @param role the role to require (i.e. USER, ADMIN, etc). |
| 202 | + * @return the [AuthorizationManager] with the provided role |
| 203 | + */ |
| 204 | + fun hasRole(role: String): AuthorizationManager<RequestAuthorizationContext> { |
| 205 | + return AuthorityAuthorizationManager.hasRole(role) |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Specify that URLs require any of the provided roles. |
| 210 | + * |
| 211 | + * @param roles the roles to require (i.e. USER, ADMIN, etc). |
| 212 | + * @return the [AuthorizationManager] with the provided roles |
| 213 | + */ |
| 214 | + fun hasAnyRole(vararg roles: String): AuthorizationManager<RequestAuthorizationContext> { |
| 215 | + return AuthorityAuthorizationManager.hasAnyRole(*roles) |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * Specify that URLs are allowed by anyone. |
| 220 | + */ |
| 221 | + val permitAll: AuthorizationManager<RequestAuthorizationContext> = |
| 222 | + AuthorizationManager { _: Supplier<Authentication>, _: RequestAuthorizationContext -> AuthorizationDecision(true) } |
| 223 | + |
| 224 | + /** |
| 225 | + * Specify that URLs are not allowed by anyone. |
| 226 | + */ |
| 227 | + val denyAll: AuthorizationManager<RequestAuthorizationContext> = |
| 228 | + AuthorizationManager { _: Supplier<Authentication>, _: RequestAuthorizationContext -> AuthorizationDecision(false) } |
| 229 | + |
| 230 | + /** |
| 231 | + * Specify that URLs are allowed by any authenticated user. |
| 232 | + */ |
| 233 | + val authenticated: AuthorizationManager<RequestAuthorizationContext> = |
| 234 | + AuthenticatedAuthorizationManager.authenticated() |
| 235 | + |
| 236 | + internal fun get(): (AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizationManagerRequestMatcherRegistry) -> Unit { |
| 237 | + return { requests -> |
| 238 | + authorizationRules.forEach { rule -> |
| 239 | + when (rule) { |
| 240 | + is MatcherAuthorizationManagerRule -> requests.requestMatchers(rule.matcher).access(rule.rule) |
| 241 | + is PatternAuthorizationManagerRule -> { |
| 242 | + when (rule.patternType) { |
| 243 | + PatternType.ANT -> requests.antMatchers(rule.httpMethod, rule.pattern).access(rule.rule) |
| 244 | + PatternType.MVC -> requests.mvcMatchers(rule.httpMethod, rule.pattern) |
| 245 | + .apply { if (rule.servletPath != null) servletPath(rule.servletPath) } |
| 246 | + .access(rule.rule) |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | +} |
0 commit comments