[58] | 1 | /* Copyright 2006-2009 the original author or authors. |
---|
| 2 | * |
---|
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 4 | * you may not use this file except in compliance with the License. |
---|
| 5 | * You may obtain a copy of the License at |
---|
| 6 | * |
---|
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 8 | * |
---|
| 9 | * Unless required by applicable law or agreed to in writing, software |
---|
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 12 | * See the License for the specific language governing permissions and |
---|
| 13 | * limitations under the License. |
---|
| 14 | */ |
---|
| 15 | package org.codehaus.groovy.grails.plugins.springsecurity; |
---|
| 16 | |
---|
| 17 | import java.util.Collection; |
---|
| 18 | import java.util.Collections; |
---|
| 19 | import java.util.HashMap; |
---|
| 20 | import java.util.Map; |
---|
| 21 | |
---|
| 22 | import org.apache.log4j.Logger; |
---|
| 23 | import org.springframework.beans.factory.InitializingBean; |
---|
| 24 | import org.springframework.security.ConfigAttributeDefinition; |
---|
| 25 | import org.springframework.security.intercept.web.FilterInvocation; |
---|
| 26 | import org.springframework.security.intercept.web.FilterInvocationDefinitionSource; |
---|
| 27 | import org.springframework.security.util.AntUrlPathMatcher; |
---|
| 28 | import org.springframework.security.util.UrlMatcher; |
---|
| 29 | import org.springframework.util.Assert; |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * @author <a href='mailto:beckwithb@studentsonly.com'>Burt Beckwith</a> |
---|
| 33 | */ |
---|
| 34 | public abstract class AbstractFilterInvocationDefinition |
---|
| 35 | implements FilterInvocationDefinitionSource, InitializingBean { |
---|
| 36 | |
---|
| 37 | private UrlMatcher _urlMatcher; |
---|
| 38 | private boolean _rejectIfNoRule; |
---|
| 39 | private boolean _stripQueryStringFromUrls = true; |
---|
| 40 | |
---|
| 41 | protected static final ConfigAttributeDefinition DENY = |
---|
| 42 | new ConfigAttributeDefinition(Collections.emptyList()); |
---|
| 43 | |
---|
| 44 | protected final Map<Object, ConfigAttributeDefinition> _compiled = |
---|
| 45 | new HashMap<Object, ConfigAttributeDefinition>(); |
---|
| 46 | |
---|
| 47 | protected final Logger _log = Logger.getLogger(getClass()); |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * {@inheritDoc} |
---|
| 51 | * @see org.springframework.security.intercept.ObjectDefinitionSource#getAttributes(java.lang.Object) |
---|
| 52 | */ |
---|
| 53 | public ConfigAttributeDefinition getAttributes(Object object) { |
---|
| 54 | if (object == null || !supports(object.getClass())) { |
---|
| 55 | throw new IllegalArgumentException("Object must be a FilterInvocation"); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | FilterInvocation filterInvocation = (FilterInvocation)object; |
---|
| 59 | |
---|
| 60 | String url = determineUrl(filterInvocation); |
---|
| 61 | |
---|
| 62 | ConfigAttributeDefinition configAttribute = findConfigAttribute(url); |
---|
| 63 | if (configAttribute == null && _rejectIfNoRule) { |
---|
| 64 | return DENY; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | return configAttribute; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | protected abstract String determineUrl(FilterInvocation filterInvocation); |
---|
| 71 | |
---|
| 72 | private ConfigAttributeDefinition findConfigAttribute(final String url) { |
---|
| 73 | |
---|
| 74 | initialize(); |
---|
| 75 | |
---|
| 76 | ConfigAttributeDefinition configAttribute = null; |
---|
| 77 | Object configAttributePattern = null; |
---|
| 78 | |
---|
| 79 | for (Map.Entry<Object, ConfigAttributeDefinition> entry : _compiled.entrySet()) { |
---|
| 80 | Object pattern = entry.getKey(); |
---|
| 81 | if (_urlMatcher.pathMatchesUrl(pattern, url)) { |
---|
| 82 | // TODO this assumes Ant matching, not valid for regex |
---|
| 83 | if (configAttribute == null || _urlMatcher.pathMatchesUrl(configAttributePattern, (String)pattern)) { |
---|
| 84 | configAttribute = entry.getValue(); |
---|
| 85 | configAttributePattern = pattern; |
---|
| 86 | if (_log.isTraceEnabled()) { |
---|
| 87 | _log.trace("new candidate for '" + url + "': '" + pattern |
---|
| 88 | + "':" + configAttribute.getConfigAttributes()); |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | if (_log.isTraceEnabled()) { |
---|
| 95 | if (configAttribute == null) { |
---|
| 96 | _log.trace("no config for '" + url + "'"); |
---|
| 97 | } |
---|
| 98 | else { |
---|
| 99 | _log.trace("config for '" + url + "' is '" + configAttributePattern |
---|
| 100 | + "':" + configAttribute.getConfigAttributes()); |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | return configAttribute; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | protected void initialize() { |
---|
| 108 | // override if necessary |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * {@inheritDoc} |
---|
| 113 | * @see org.springframework.security.intercept.ObjectDefinitionSource#supports(java.lang.Class) |
---|
| 114 | */ |
---|
| 115 | @SuppressWarnings("unchecked") |
---|
| 116 | public boolean supports(final Class clazz) { |
---|
| 117 | return FilterInvocation.class.isAssignableFrom(clazz); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | * {@inheritDoc} |
---|
| 122 | * @see org.springframework.security.intercept.ObjectDefinitionSource#getConfigAttributeDefinitions() |
---|
| 123 | */ |
---|
| 124 | @SuppressWarnings("unchecked") |
---|
| 125 | public Collection getConfigAttributeDefinitions() { |
---|
| 126 | return null; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | /** |
---|
| 130 | * Dependency injection for the url matcher. |
---|
| 131 | * @param urlMatcher the matcher |
---|
| 132 | */ |
---|
| 133 | public void setUrlMatcher(final UrlMatcher urlMatcher) { |
---|
| 134 | _urlMatcher = urlMatcher; |
---|
| 135 | _stripQueryStringFromUrls = _urlMatcher instanceof AntUrlPathMatcher; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | * Dependency injection for whether to reject if there's no matching rule. |
---|
| 140 | * @param reject if true, reject access unless there's a pattern for the specified resource |
---|
| 141 | */ |
---|
| 142 | public void setRejectIfNoRule(final boolean reject) { |
---|
| 143 | _rejectIfNoRule = reject; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | protected String lowercaseAndStringQuerystring(final String url) { |
---|
| 147 | |
---|
| 148 | String fixed = url; |
---|
| 149 | |
---|
| 150 | if (getUrlMatcher().requiresLowerCaseUrl()) { |
---|
| 151 | fixed = fixed.toLowerCase(); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | if (_stripQueryStringFromUrls) { |
---|
| 155 | int firstQuestionMarkIndex = fixed.indexOf("?"); |
---|
| 156 | if (firstQuestionMarkIndex != -1) { |
---|
| 157 | fixed = fixed.substring(0, firstQuestionMarkIndex); |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | return fixed; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | protected UrlMatcher getUrlMatcher() { |
---|
| 165 | return _urlMatcher; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | /** |
---|
| 169 | * For debugging. |
---|
| 170 | * @return an unmodifiable map of {@link AnnotationFilterInvocationDefinition}ConfigAttributeDefinition |
---|
| 171 | * keyed by compiled patterns |
---|
| 172 | */ |
---|
| 173 | public Map<Object, ConfigAttributeDefinition> getConfigAttributeMap() { |
---|
| 174 | return Collections.unmodifiableMap(_compiled); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | /** |
---|
| 178 | * {@inheritDoc} |
---|
| 179 | * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() |
---|
| 180 | */ |
---|
| 181 | public void afterPropertiesSet() { |
---|
| 182 | Assert.notNull(_urlMatcher, "url matcher is required"); |
---|
| 183 | } |
---|
| 184 | } |
---|