|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 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.security.config.annotation.web.builders.HttpSecurity |
| 20 | +import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer |
| 21 | +import org.springframework.security.web.util.matcher.AnyRequestMatcher |
| 22 | +import org.springframework.security.web.util.matcher.RequestMatcher |
| 23 | +import org.springframework.util.ClassUtils |
| 24 | + |
| 25 | +/** |
| 26 | + * A Kotlin DSL to configure [HttpSecurity] request authorization using idiomatic Kotlin code. |
| 27 | + * |
| 28 | + * @author Eleftheria Stein |
| 29 | + * @since 5.3 |
| 30 | + */ |
| 31 | +class AuthorizeRequestsDsl : AbstractRequestMatcherDsl() { |
| 32 | + private val authorizationRules = mutableListOf<AuthorizationRule>() |
| 33 | + |
| 34 | + private val HANDLER_MAPPING_INTROSPECTOR = "org.springframework.web.servlet.handler.HandlerMappingIntrospector" |
| 35 | + private val MVC_PRESENT = ClassUtils.isPresent( |
| 36 | + HANDLER_MAPPING_INTROSPECTOR, |
| 37 | + AuthorizeRequestsDsl::class.java.classLoader) |
| 38 | + |
| 39 | + /** |
| 40 | + * Adds a request authorization rule. |
| 41 | + * |
| 42 | + * @param matches the [RequestMatcher] to match incoming requests against |
| 43 | + * @param access the SpEL expression to secure the matching request |
| 44 | + * (i.e. "hasAuthority('ROLE_USER') and hasAuthority('ROLE_SUPER')") |
| 45 | + */ |
| 46 | + fun authorize(matches: RequestMatcher = AnyRequestMatcher.INSTANCE, |
| 47 | + access: String = "authenticated") { |
| 48 | + authorizationRules.add(MatcherAuthorizationRule(matches, access)) |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Adds a request authorization rule for an endpoint matching the provided |
| 53 | + * pattern. |
| 54 | + * If Spring MVC is on the classpath, it will use an MVC matcher. |
| 55 | + * If Spring MVC is not an the classpath, it will use an ant matcher. |
| 56 | + * The MVC will use the same rules that Spring MVC uses for matching. |
| 57 | + * For example, often times a mapping of the path "/path" will match on |
| 58 | + * "/path", "/path/", "/path.html", etc. |
| 59 | + * If the current request will not be processed by Spring MVC, a reasonable default |
| 60 | + * using the pattern as an ant pattern will be used. |
| 61 | + * |
| 62 | + * @param pattern the pattern to match incoming requests against. |
| 63 | + * @param access the SpEL expression to secure the matching request |
| 64 | + * (i.e. "hasAuthority('ROLE_USER') and hasAuthority('ROLE_SUPER')") |
| 65 | + */ |
| 66 | + fun authorize(pattern: String, access: String = "authenticated") { |
| 67 | + if (MVC_PRESENT) { |
| 68 | + authorizationRules.add(PatternAuthorizationRule(pattern, PatternType.MVC, null, access)) |
| 69 | + } else { |
| 70 | + authorizationRules.add(PatternAuthorizationRule(pattern, PatternType.ANT, null, access)) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Adds a request authorization rule for an endpoint matching the provided |
| 76 | + * pattern. |
| 77 | + * If Spring MVC is on the classpath, it will use an MVC matcher. |
| 78 | + * If Spring MVC is not an the classpath, it will use an ant matcher. |
| 79 | + * The MVC will use the same rules that Spring MVC uses for matching. |
| 80 | + * For example, often times a mapping of the path "/path" will match on |
| 81 | + * "/path", "/path/", "/path.html", etc. |
| 82 | + * If the current request will not be processed by Spring MVC, a reasonable default |
| 83 | + * using the pattern as an ant pattern will be used. |
| 84 | + * |
| 85 | + * @param pattern the pattern to match incoming requests against. |
| 86 | + * @param servletPath the servlet path to match incoming requests against. This |
| 87 | + * only applies when using an MVC pattern matcher. |
| 88 | + * @param access the SpEL expression to secure the matching request |
| 89 | + * (i.e. "hasAuthority('ROLE_USER') and hasAuthority('ROLE_SUPER')") |
| 90 | + */ |
| 91 | + fun authorize(pattern: String, servletPath: String, access: String = "authenticated") { |
| 92 | + if (MVC_PRESENT) { |
| 93 | + authorizationRules.add(PatternAuthorizationRule(pattern, PatternType.MVC, servletPath, access)) |
| 94 | + } else { |
| 95 | + authorizationRules.add(PatternAuthorizationRule(pattern, PatternType.ANT, servletPath, access)) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Specify that URLs require a particular authority. |
| 101 | + * |
| 102 | + * @param authority the authority to require (i.e. ROLE_USER, ROLE_ADMIN, etc). |
| 103 | + * @return the SpEL expression "hasAuthority" with the given authority as a |
| 104 | + * parameter |
| 105 | + */ |
| 106 | + fun hasAuthority(authority: String) = "hasAuthority('$authority')" |
| 107 | + |
| 108 | + /** |
| 109 | + * Specify that URLs are allowed by anyone. |
| 110 | + */ |
| 111 | + val permitAll = "permitAll" |
| 112 | + |
| 113 | + /** |
| 114 | + * Specify that URLs are allowed by anonymous users. |
| 115 | + */ |
| 116 | + val anonymous = "anonymous" |
| 117 | + |
| 118 | + /** |
| 119 | + * Specify that URLs are allowed by users that have been remembered. |
| 120 | + */ |
| 121 | + val rememberMe = "rememberMe" |
| 122 | + |
| 123 | + /** |
| 124 | + * Specify that URLs are not allowed by anyone. |
| 125 | + */ |
| 126 | + val denyAll = "denyAll" |
| 127 | + |
| 128 | + /** |
| 129 | + * Specify that URLs are allowed by any authenticated user. |
| 130 | + */ |
| 131 | + val authenticated = "authenticated" |
| 132 | + |
| 133 | + /** |
| 134 | + * Specify that URLs are allowed by users who have authenticated and were not |
| 135 | + * "remembered". |
| 136 | + */ |
| 137 | + val fullyAuthenticated = "fullyAuthenticated" |
| 138 | + |
| 139 | + internal fun get(): (ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry) -> Unit { |
| 140 | + return { requests -> |
| 141 | + authorizationRules.forEach { rule -> |
| 142 | + when (rule) { |
| 143 | + is MatcherAuthorizationRule -> requests.requestMatchers(rule.matcher).access(rule.rule) |
| 144 | + is PatternAuthorizationRule -> { |
| 145 | + when (rule.patternType) { |
| 146 | + PatternType.ANT -> requests.antMatchers(rule.pattern).access(rule.rule) |
| 147 | + PatternType.MVC -> { |
| 148 | + val mvcMatchersAuthorizeUrl = requests.mvcMatchers(rule.pattern) |
| 149 | + rule.servletPath?.also { mvcMatchersAuthorizeUrl.servletPath(rule.servletPath) } |
| 150 | + mvcMatchersAuthorizeUrl.access(rule.rule) |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
0 commit comments